Three.js-添加纹理使其看起来呈黑色

时间:2019-11-04 07:24:30

标签: three.js geometry

我正在使用图像制作自定义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 Exporter。

enter image description here

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

注意:这是为此图像手动设置的偏移量,以获取结果。

texture.offset.set(0.188,0.934);

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

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

repeat.set(2, 3);     

在3次击落中重复2次

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

在.33向下重复0.5,换句话说,显示一半的纹理,向下1/3的纹理

offset将纹理移动到

  • 1 =将其向左移动100%(如果重复纹​​理,则由于您已将其移动100%,所以不会在1处发生变化)
  • 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>