【问题标题】:Add text with geometry in threejs在threejs中添加带有几何图形的文本
【发布时间】:2020-08-21 04:48:07
【问题描述】:

如下图所示,我想将文本附加到环的底部(切口所在的位置)。你可以看到我用来绘制环的代码。我想写出切口所在的半径(40m、30m、20m),并且我希望它们被合并,因为我将放大和缩小它们并希望它们保持与环的连接。

// Rings
// 40m ring
const geometry40m = new THREE.RingGeometry(35, 35.6, 30, 8, 4.85, 6);
geometry40m.lookAt(this.CAMERA_POSITION);
const ringMesh40m = new THREE.Mesh(geometry40m, whiteMaterial);

ringMesh40m.updateMatrix();
// geometry40m.mergeMesh(new THREE.Mesh(textGeometry, whiteMaterial));

// 30m ring
const geometry30m = new THREE.RingGeometry(26, 26.6, 30, 8, 4.85, 6);
geometry30m.lookAt(this.CAMERA_POSITION);

geometry30m.mergeMesh(ringMesh40m); // adding 40m and 30m to one mesh

const ringMesh40_30m = new THREE.Mesh(geometry30m, whiteMaterial);
ringMesh40_30m.updateMatrix();

// 20m ring
const geometry20m = new THREE.RingGeometry(16, 16.6, 30, 8, 4.85, 6);
geometry20m.lookAt(this.CAMERA_POSITION);

geometry20m.mergeMesh(ringMesh40_30m); // adding 40m, 30m and 20m to one mesh

const ringMesh40_30_20m = new THREE.Mesh(geometry20m, whiteMaterial);
this.rings = ringMesh40_30_20m;
this.rings.layers.set(15);
this.rings.visible = true;
this.scene.add(this.rings);

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    您可以添加带有 PlaneGeometry 的 Mesh 对象和带有 CanvasTexture 对象纹理的 Basic 材质。将它们放置在切口中并使用显示大小的文本更新纹理内容。

    如果您希望它们始终面向屏幕,您可以使用 Sprite 对象而不是 Mesh

    【讨论】:

      【解决方案2】:

      对于其他寻找答案的人,我最终使用此功能创建了文本标签:

      createTextMesh(text, font, size, mat) {
         var shapes = font.generateShapes(text, size);
      
         var geometry = new THREE.ShapeBufferGeometry(shapes);
      
         geometry.center();
         geometry.computeBoundingBox();
      
         return new THREE.Mesh(geometry, mat);
      }
      

      这就是我最终要做的,它对我有用:

      // Rings
      
      // 40m ring
      const geometry40m = new THREE.RingGeometry(
        RADIUS_40M,
        RADIUS_40M + 0.6,
        30,
        8,
        4.85,
        6);
      
      // 30m ring
      const geometry30m = new THREE.RingGeometry(
        RADIUS_30M,
        RADIUS_30M + 0.6,
        30,
        8,
        4.85,
        6);
      geometry30m.merge(geometry40m);
      
      // 20m ring
      const geometry20m = new THREE.RingGeometry(
        RADIUS_20M,
        RADIUS_20M + 0.6,
        30,
        8,
        4.85,
        6
      );
      geometry20m.merge(geometry30m);
      
      // adding 40m, 30m and 20m to one mesh
      const ringMesh40_30_20m = new THREE.Mesh(geometry20m, whiteMaterial);
      ringMesh40_30_20m.layers.set(16);
      ringMesh40_30_20m.visible = true;
      ringMesh40_30_20m.name = "rings";
      
      this.rings = new THREE.Object3D();
      this.rings.add(ringMesh40_30_20m);
      
      // Labels
      
      const fontJson = require("../../../assets/helvetiker_regular.typeface.json");
      const font = new THREE.Font(fontJson);
      
      let label20m = this.createTextMesh("20m", font, 0.5, whiteMaterial);
      label20m.layers.set(16);
      label20m.visible = true;
      label20m.name = "label20m";
      
      let label30m = this.createTextMesh("30m", font, 2.2, whiteMaterial);
      label30m.layers.set(16);
      label30m.visible = true;
      label30m.name = "label30m";
      
      let label40m = this.createTextMesh("40m", font, 2.5, whiteMaterial);
      label40m.layers.set(16);
      label40m.visible = true;
      label40m.name = "label40m";
      
      this.rings.add(label20m);
      this.rings.add(label30m);
      this.rings.add(label40m);
      
      this.rings.getObjectByName("label20m").position.y = -RADIUS_20M;
      this.rings.getObjectByName("label30m").position.y = -RADIUS_30M;
      this.rings.getObjectByName("label40m").position.y = -RADIUS_40M;
      this.rings.lookAt(this.CAMERA_POSITION);
      this.scene.add(this.rings);
      

      我从three.js official Repo得到字体

      【讨论】:

      • 如果你喜欢这个答案,请考虑投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2020-08-26
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2017-05-14
      相关资源
      最近更新 更多