【问题标题】:How to convert a shape of a face of a 3D model in three.js into a 2D SVG?如何将three.js中的3D模型的面部形状转换为2D SVG?
【发布时间】:2021-11-20 13:16:33
【问题描述】:

是否可以在 three.js 中生成具有模型特定面形状的 SVG?我们正在为 3D 模型开发交互式编辑器,您可以在其中在每面墙上进行绘制,但我们遇到了这个问题。

例如,对于这张脸:

我们想要一个这样的 SVG:

【问题讨论】:

    标签: svg three.js 3d collada


    【解决方案1】:

    在编程和数学交流之间存在这个问题的几个版本。我能够结合几个答案并翻译成 Three.js 的 Vector3Quaternion 类。

    假设您要渲染此tetrahedron OBJ 的第一面:

    const points3D = [
      Vector3(0.0, 0.0, 1.732051),
      Vector3(1.632993, 0.0, -0.5773503),
      Vector3(-0.8164966, 1.414214, -0.5773503),
    ];
    
    function pointsInPlaneToPoints2D (points3D) {
      // The normal is a unit vector that sticks out from face.
      // We're using the first 3 points, assuming that there
      // are at least 3, and they are not co-linear.
      const [p0, p1, p2] = points3D;
      const planeNormal =
        (new Vector3()).crossVectors(
          (new Vector3()).subVectors(p0, p1),
          (new Vector3()).subVectors(p1, p2)
        ).normalize();
      
      // Unit vector on the Z axis, where we want to rotate our face
      const zUnit = new Vector3(0, 0, 1);
      
      // Quaternion representing the rotation of the face plane to Z
      const qRot = (new Quaternion()).setFromUnitVectors(planeNormal, zUnit);
      
      // Rotate each point, assuming that they are all co-planar
      // with the first 3.
      return points3D.map(function (p) {
        const pRot = p.clone().applyQuaternion(qRot);
        // Output format [x, y] (ignoring z)
        return [pRot.x, pRot.y];
      });
    }
    
    const points2D = points3DInPlaneToPoints2D(points3D);
    // Draw those with your 2D drawing context of choice
    
    

    这是 truncated icosahedron 的面孔,用这个函数展平,并用 SVG 渲染:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2013-04-13
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多