【问题标题】:Display texts at different-different coordinates using three.js使用three.js在不同坐标显示文本
【发布时间】:2018-10-10 15:06:26
【问题描述】:

我想使用three js 在不同的坐标显示不同的文本。我想在不同的位置显示如下图像文件中的文本。

我希望该函数应该在不同的位置显示文本,就像在链接的图像文件中一样

Javascript

var container, camera, scene, renderer, controls;
var text2;


init();
animate();

function init(){

    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 1000 );
    camera.position.z = 1 ;

    scene = new THREE.Scene();

    text2 = createLabel("Tower",30,90,30,10,50,"blue","white");
    text3 = createLabel("Hello",30,90,50,60,50,"black","white");
    text4 = createLabel("Tower",30,90,120,100,50,"red","white");

    console.log( text2 );

    scene.add( text2 );
    scene.add( text3 );
    scene.add( text4 );
    text2.lookAt( camera.position );

    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setClearColor(0xffffff);
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.append( renderer.domElement );

}

function createLabel(text, x, y, canvasPosition_x,canvasPosition_y, size, color, backGroundColor, backgroundMargin) {

    var canvas = document.createElement("canvas");

    // set resolution

    canvas.width = 512;
    canvas.height = 512;

    var context = canvas.getContext("2d");
    context.font = size + "pt Arial";

    context.font = size + "pt Calibri";
    context.fillStyle = "white";

    canvas.position = "absolute";

    context.fillRect( 0, 0, canvas.width, canvas.height );
    context.fillStyle = color;
    context.fillText( text, x, y );
    context.position = "absolute";

    var texture = new THREE.CanvasTexture( canvas );

    var material = new THREE.MeshBasicMaterial({
            map: texture,
    // color: 0xff0000 // useful for debugging. in this way you see, how much are of the plane is used for the text
});

    var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(), material);

     mesh.position.x = canvasPosition_x;
     mesh.position.y = canvasPosition_y;

    return mesh;

}

function animate()  {

    requestAnimationFrame( animate );
    renderer.render( scene, camera );


}

Here is link of fiddle

【问题讨论】:

标签: javascript three.js


【解决方案1】:

您可以使用网格的位置属性来更改位置。比如mesh.position.x = 0.5;

【讨论】:

  • 我试试这个,但它不起作用。所以你能帮我解决这个问题吗?
  • 您有一个距离为 1 的透视相机和 50 度的 FOV。这意味着垂直方向覆盖的距离为Math.tan(Math.PI/180*25),大致为0.466。所以你需要将你的位置保持在 [-0.4, 0.4] 范围内,这样相机才能看到物体。
猜你喜欢
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2012-07-20
  • 2017-05-30
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多