【问题标题】:ThreeJS Turn TextureThreeJS 转向纹理
【发布时间】:2019-11-28 03:23:47
【问题描述】:

我用画布创建了纹理,但我没有什么问题。我无法像图像中那样转动我的纹理。我该怎么做?

   setTexture = (name, font = 20) => {    

      const canvas = window.document.createElement("canvas");
      canvas.width = 256;
      canvas.height = 128;
      const ctx = canvas.getContext("2d");

      ctx.fillStyle = "white";
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      ctx.font = `${font}pt Arial`;
      ctx.fillStyle = "black";
      ctx.textAlign = "center";
      ctx.textBaseline = "middle";
      ctx.fillText(
          name || "unnamed",
          canvas.width / 2,
          canvas.height / 2
      );

      const texture = new THREE.Texture(canvas);
      texture.wrapS = THREE.RepeatWrapping;
      texture.repeat.x = -1;
      texture.needsUpdate = true;
      return texture;
  };

【问题讨论】:

    标签: javascript reactjs three.js


    【解决方案1】:

    如果要旋转纹理,可以更新几何体中的 UV,也可以使用 texture.rotation 修改器。

    const texture = new THREE.Texture(canvas);
    texture.rotation = Math.PI; // 180 deg in radians
    

    如果旋转不是围绕您想要的点旋转,您可以将texture.center 设置为[0.0, 1.0] 范围内的另一个值。

    const texture = new THREE.Texture(canvas);
    texture.center = new THREE.Vector2(0.5, 0.7);
    texture.rotation = Math.PI;
    

    【讨论】:

    • 您可能还想在图像编辑器中旋转图像,保存并重新加载
    • @Hesha 他代码中的图片是通过canvas元素动态生成的。不涉及图像编辑器。
    • 自我说明:我应该在午夜过后停止使用 SO
    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 2021-11-03
    • 2015-06-03
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多