我正在使用三个 JS 来开发 3d 图形。我想将图表的单位显示为THREE.SPRITE
. 对于创建SPRITE
,我首先创建了一个画布元素并向其添加了文本。然后我使用THREE.Texture
之前创建的画布元素创建。THREE.SpriteMaterial
使用纹理作为贴图创建,然后使用THREE.SPRITE
此精灵材质创建。将此精灵材质添加到场景中。当渲染器是实例时THREE.WebGLRenderer
,文本的大小非常小,而当渲染器是实例时THREE.CanvasRenderer
,文本的大小非常大。
以下是我用来创建 Sprite 的代码。
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
metrics = null,
textHeight = 100,
textWidth = 0,
actualFontSize = 20;
context.font = "normal " + textHeight + "px Arial";
metrics = context.measureText("Sample Text");
var textWidth = metrics.width;
canvas.width = textWidth;
canvas.height = textHeight;
context.font = "normal " + textHeight + "px Arial";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#ff0000";
context.fillText("Sample Text", textWidth / 2, textHeight / 2);
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
var material = new THREE.SpriteMaterial({ map: texture, useScreenCoordinates: false, alignment: THREE.SpriteAlignment.center });
material.transparent = true;
//var textObject = new THREE.Sprite(material);
var textObject = new THREE.Object3D();
var sprite = new THREE.Sprite(material);
textObject.textHeight = actualFontSize;
textObject.textWidth = (textWidth / textHeight) * textObject.textHeight;
//sprite.scale.set(textObject.textWidth / textWidth, textObject.textHeight / textHeight, 1);
textObject.add(sprite);
scene.add(textObject);
这是默认行为还是我做错了什么。我正在寻找一种在 Canvas 和 WebGL 渲染器中始终有效的修复程序。