使用three.js創建大小不隨着場景變化的文字,需要以下兩步:
1、將文字繪制到畫布上。
2、創建着色器材質,把文字放到三維場景中。
優點:
1、跟用html實現文字相比,這些文字可以被模型遮擋,更具有三維效果。
2、不會隨着場景旋轉縮放改變尺寸,不存在遠處看不清的情況,適用於三維標注。
效果圖:
示例代碼1:https://github.com/tengge1/ShadowEditor/blob/master/ShadowEditor.Web/src/object/text/UnscaledText.js
示例代碼2:https://gitee.com/tengge1/ShadowEditor/blob/master/ShadowEditor.Web/src/object/text/UnscaledText.js
實現方法
1、使用canvas繪制文字,先用黑色繪制描邊,然后用白色繪制文字。黑色描邊主要為了讓文字在白色背景處能看清。
let context = canvas.getContext('2d'); context.imageSmoothingQuality = 'high'; context.textBaseline = 'middle'; context.textAlign = 'center'; context.lineWidth = 4; let halfWidth = canvas.width / 2; let halfHeight = canvas.height / 2; // 畫描邊 context.font = `16px "Microsoft YaHei"`; context.strokeStyle = '#000'; context.strokeText(text, halfWidth, halfHeight); // 畫文字 context.fillStyle = '#fff'; context.fillText(text, halfWidth, halfHeight);
2、 創建着色器材質,將文字正對屏幕,渲染到三維場景中。
let geometry = new THREE.PlaneBufferGeometry(); let material = new THREE.ShaderMaterial({ vertexShader: UnscaledTextVertexShader, fragmentShader: UnscaledTextFragmentShader, uniforms: { tDiffuse: { value: new THREE.CanvasTexture(canvas) }, width: { value: canvas.width }, height: { value: canvas.height }, domWidth: { value: renderer.domElement.width }, domHeight: { value: renderer.domElement.height } }, transparent: true }); let mesh = new THREE.Mesh(geometry, material);
說明:由於canvas上繪制的文字邊緣是半透明的,材質要設置成半透明才能實現文字邊緣平滑效果。
precision highp float; uniform float width; uniform float height; uniform float domWidth; uniform float domHeight; varying vec2 vUv; void main() { vUv = uv; vec4 proj = projectionMatrix * modelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0); gl_Position = vec4( proj.x / proj.w + position.x * width / domWidth * 2.0, proj.y / proj.w + position.y * height / domHeight * 2.0, proj.z / proj.w, 1.0 ); }
說明:
a、(0.0, 0.0, 0.0)是平面中心世界坐標,左乘modelViewMatrix和projectionMatrix后,得到屏幕坐標系中的坐標。
b、proj.x / proj.w + position.x * width / domWidth * 2.0的意思是把平板中心放到世界坐標系正確位置,讓平板顯示的寬度恰好等於屏幕上的像素數,避免文字縮放。
c、乘以2.0是因為three.js默認生成的平板寬度和高度是1,屏幕坐標系寬度和高度都是從-1到1,是2。
d、gl_Position.w為1.0時,是正投影,模型大小不隨着屏幕深度變化而改變。
precision highp float; uniform sampler2D tDiffuse; uniform float width; uniform float height; varying vec2 vUv; void main() { // 注意vUv一定要從畫布整數坐標取顏色,否則會導致文字模糊問題。 vec2 _uv = vec2( (floor(vUv.s * width) + 0.5) / width, (floor(vUv.t * height) + 0.5) / height ); gl_FragColor = texture2D( tDiffuse, _uv ); }
說明:
1、uv坐標一定要恰好對應畫布上的像素點,否則會導致文字模糊問題。
文字模糊的解決方法
vec2 _uv = vec2( (floor(vUv.s * width) + 0.5) / width, (floor(vUv.t * height) + 0.5) / height );