我正在使用Three JS开发一个3d图形。我想将图表的单位显示为THREE.SPRITE
。为了创建SPRITE
,我首先创建了一个canvas元素并向其添加了文本。然后我使用之前创建的canvas元素创建了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渲染器中一致的任何修复。
提前致谢。
答案 0 :(得分:6)
尝试了这么多组合之后,下面的代码就可以了。
var SCREEN_WIDTH = 400,
SCREEN_HEIGHT = 300,
VIEW_ANGLE = 45,
ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT,
NEAR = 0.1,
FAR = 20000,
webGLScene = new THREE.Scene(),
canvasScene = new THREE.Scene(),
webGLCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR),
canvasCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR),
webGLRenderer = new THREE.WebGLRenderer({ antialias: true }),
canvasRenderer = new THREE.CanvasRenderer();
webGLScene.add(webGLCamera);
canvasScene.add(canvasCamera);
webGLCamera.position.set(0, 0, 20);
webGLCamera.lookAt(webGLScene.position);
canvasCamera.position.set(0, 0, 20);
canvasCamera.lookAt(canvasScene.position);
webGLRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
canvasRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container = document.body;
container.appendChild(webGLRenderer.domElement);
container.appendChild(canvasRenderer.domElement);
makeSprite(webGLScene, "webgl");
makeSprite(canvasScene, "2d");
function makeSprite(scene, rendererType) {
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
metrics = null,
textHeight = 100,
textWidth = 0,
actualFontSize = 2;
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;
if (rendererType == "2d") {
sprite.scale.set(textObject.textWidth / textWidth, textObject.textHeight / textHeight, 1);
} else {
sprite.scale.set(textWidth / textHeight * actualFontSize, actualFontSize, 1);
}
textObject.add(sprite);
scene.add(textObject);
}
canvasRenderer.render(canvasScene, canvasCamera);
webGLRenderer.render(webGLScene, webGLCamera);
添加三个JS(版本62)链接并使用以下脚本。
希望这有助于其他有类似问题的人。
更新:以下是上述代码的jsfiddle。