如何在threeJS中动态更改textGeometry的文本?

时间:2018-04-13 07:38:31

标签: three.js

我试过了:

var fontLoader = new THREE.FontLoader();

        fontLoader.load("/assets/fonts/helvetiker_bold.typeface.json",function(font){ 

            var  text2D = new THREE.TextGeometry(newText.textContent, {
                    size: 200,
                    height: 2,
                    curveSegments: 3,
                    font: font
            });
            var color = new THREE.Color( newText.color );
            var  textMaterial = new THREE.MeshBasicMaterial({ color: color });
            var  text = new THREE.Mesh(text2D , textMaterial);
            text.name = "2D Text 1";

            //Add the text to current scene
            current_scene.add(text);

            //set the elemnt as active element
            activeElement = current_scene.getObjectByName(text.name);
            console.log('activeElement:',activeElement);

        });

        function textData(changedValue){
            activeElement.geometry.parameters.text = changedValue;
        }
  

但我没有得到任何改变。这是正确的方法吗?或者需要Matrix参数的任何更新?

1 个答案:

答案 0 :(得分:0)

您将需要删除然后添加新的文本网格。

它具有巨大的性能损失,但不幸的是,这是目前的样子。

    //------------Add Text Geometry----------

    //Group to hold mesh
    group = new THREE.Group();
    this.scene.add(group);

   //add clock mesh
    startTime = new Date();
    earthClockMesh = this.getTextMesh(startTime.toLocaleString(), textMaterial);
    group.add(earthClockMesh);


      //------------Update Text  Mesh----------

      //remove old mesh
      group.remove(earthClockMesh);

      //add new mesh
      earthClockMesh = this.getTextMesh(
        new Date(timeNow).toLocaleString(),
        textMaterial
      );
      group.add(earthClockMesh);

创建新网格的功能

getTextMesh = (text, material) => {
    //Number
    var textgeometry = new TextGeometry(
      text,
      Object.assign(
        {},
        {
          font: helvatiker,
          bevelEnabled: false,
          curveSegments: 24,
          bevelThickness: 1,
          bevelSize: 0,
          height: 0.7,
          size: 5
        }
      )
    );
    let numberMesh = new THREE.Mesh(textgeometry, material);
    // wireframe
    var geo = new THREE.EdgesGeometry(numberMesh.geometry); // or WireframeGeometry

    var wireframe = new THREE.LineSegments(geo, wireFrameMaterial);
    numberMesh.add(wireframe);

    // Translate to Center
    return numberMesh;
  };