【问题标题】:Three.js - Adding texture makes it look blackThree.js - 添加纹理使其看起来是黑色的
【发布时间】:2020-02-29 11:57:21
【问题描述】:

我正在使用图像制作自定义 3D 对象。首先,我从图像中提取轮廓,然后在获得点后创建形状。然后我使用three.js 拉伸几何使其看起来像3D 对象。

问题是我使用的纹理显示完全黑色。我使用此代码来缩放纹理。

texture.wrapT = texture.wrapS = THREE.RepeatWrapping;
var rx = 1/img.width;
var ry = 1/img.height;
texture.repeat.set(rx,ry);

这给了我下图中的结果: 注意:我正在使用 GLTF 导出器。

它正在正确缩放纹理,但我无法设置偏移量。图像排列不正确。 我想动态设置偏移量,因为我的图像每次都会不同。我可以手动设置偏移量并获得如下图所示的结果。但我希望这是动态的。

注意:这是为该图像手动设置的偏移量。

texture.offset.set(0.188,0.934);

我真的需要帮助。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: three.js geometry


    【解决方案1】:

    目前尚不清楚您要做什么,但是...

    texture.repeat 设置纹理重复的次数,因此texture.repeat.set(2,3) 表示重复两次,向下重复三次。这意味着您的代码 texture.repeat.set(1 / img.width, 1 / img.height) 将扩展纹理,以便只有 1 个像素可见。

    repeat.set(2, 3);     
    

    在 3 下重复 2 次​​p>

    repeat.set(1/2, 1/3);  
    

    在 0.33 下重复 0.5,或者换句话说,显示一半的纹理和 1/3 的纹理

    offset 将纹理移动到哪里

    • 1 = 将其向左移动 100%(如果纹理重复,则 1 将不会发生变化,因为您已将其移动 100%)
    • 0.5 = 向左移动 50%
    • 0.25 = 向左移动 25%
    • -0.10 = 向右移动 -10%

    如果你想以像素为单位移动它,你可以在这里使用 img.width

    • 1/img.width = 向左移动一个像素

    见this page底部的例子

    body {
      margin: 0;
    }
    #c {
      width: 100vw;
      height: 100vh;
      display: block;
    }
    <canvas id="c"></canvas>
      
    <script type="module">
    import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r110/build/three.module.js';
    
    function main() {
      const canvas = document.querySelector('#c');
      const renderer = new THREE.WebGLRenderer({canvas});
    
      const fov = 75;
      const aspect = 2;  // the canvas default
      const near = 0.1;
      const far = 5;
      const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
      camera.position.z = 2;
    
      const scene = new THREE.Scene();
    
      const geometry = new THREE.PlaneGeometry(1, 1);
    
      const obs = [];  // just an array we can use to rotate the cubes
      const loader = new THREE.TextureLoader();
      for (let i = 0; i < 10; ++i) {
        const texture = loader.load('https://i.imgur.com/ZKMnXce.png');
    
        // expand the texture so only 40% of stretched across the plane
        texture.repeat.set(0.4, 0.4);
        
        // randomly offset the texture
        texture.offset.set(rand(1), rand(1));
        
        // make it repeat
        texture.wrapS = THREE.RepeatWrapping;
        texture.wrapT = THREE.RepeatWrapping;
        
        texture.magFilter = THREE.NearestFilter;
     
        const material = new THREE.MeshBasicMaterial({
          map: texture,
          side: THREE.DoubleSide,
        });
        const plane = new THREE.Mesh(geometry, material);
        plane.position.set(rand(-1, 1), rand(-1, 1), 0);
        plane.position.set(rand(-1, 1), rand(-1, 1), 0);
        scene.add(plane);
        obs.push(plane);  // add to our list of obs to rotate
      }
      
      
      
      function rand(min, max) {
        if (max === undefined) {
          max = min;
          min = 0;
        }
        return Math.random() * (max - min) + min;
      }
    
      function resizeRendererToDisplaySize(renderer) {
        const canvas = renderer.domElement;
        const width = canvas.clientWidth;
        const height = canvas.clientHeight;
        const needResize = canvas.width !== width || canvas.height !== height;
        if (needResize) {
          renderer.setSize(width, height, false);
        }
        return needResize;
      }
    
      function render(time) {
        time *= 0.001;
    
        if (resizeRendererToDisplaySize(renderer)) {
          const canvas = renderer.domElement;
          camera.aspect = canvas.clientWidth / canvas.clientHeight;
          camera.updateProjectionMatrix();
        }
    
        obs.forEach((obj, ndx) => {
          const speed = .2 + ndx * .1;
          const rot = time * speed;
          obj.rotation.z = rot;
        });
    
        renderer.render(scene, camera);
    
        requestAnimationFrame(render);
      }
    
      requestAnimationFrame(render);
    }
    
    main();
    </script>

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 2016-03-30
      • 2013-09-09
      • 2013-01-13
      • 2013-06-11
      • 2020-04-17
      • 1970-01-01
      • 2012-02-14
      • 2014-07-11
      相关资源
      最近更新 更多