如何在three.js中为三角形添加纹理?

时间:2022-06-21 05:12:21

I've created a scene which has many cubes, spheres, etc, in it, and have been able to apply textures to those primitives, but when I want to add a texture to a simple triangle, I'm totally lost.

我已经创建了一个场景,其中包含许多立方体,球体等,并且能够将纹理应用于这些基元,但是当我想将纹理添加到一个简单的三角形时,我完全迷失了。

Here's the closest I've come to getting it right:

这是我最接近正确的方法:

  var texture=THREE.ImageUtils.loadTexture('/f/3d-images/texture-steel.jpg');
  var materialDecalRoof=new THREE.MeshBasicMaterial( {
    'map': texture,
    'wireframe': false,
    'overdraw': true
  } );
var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(tentX/2+1, tentY, tentZ/2+1));
geometry.vertices.push(new THREE.Vector3(0, tentT, 0));
geometry.vertices.push(new THREE.Vector3(0, tentT, 0));
geometry.vertices.push(new THREE.Vector3(-tentX/2-1, tentY, tentZ/2+1));
geometry.faces.push(new THREE.Face4(0, 1, 2, 3));
geometry.faceVertexUvs[0].push([
  new THREE.UV(0, 0),
  new THREE.UV(0, 0),
  new THREE.UV(0, 0),
  new THREE.UV(0, 0)
]);
geometry.computeFaceNormals();
geometry.computeCentroids();
geometry.computeVertexNormals();
var mesh= new THREE.Mesh( geometry, materialDecalRoof);
scene.add(mesh);

This does not work. All i get is a flickering triangle (when the scene is moved) where an image should be.

这不起作用。我得到的是一个闪烁的三角形(当场景移动时)图像应该是。

To test this, go to https://www.poptents.eu/3dFrame.php, upload an image in the Roof section, and drag the view to see the roof flicker.

要测试这一点,请访问https://www.poptents.eu/3dFrame.php,在Roof部分上传图像,然后拖动视图以查看屋顶闪烁。

Does anyone know what I'm doing wrong here?

有谁知道我在这里做错了什么?

1 个解决方案

#1


5  

You need to read a bit on UV Mapping. You can't initialize it with (0,0) to all 4 corners. You need to specify a number between 0 to 1 which is the percentage of the texture's width and height. Meaning, a full texture on a Face4 would be (0,0), (1,0), (1,1), (0,1) if the vertices are like (top-left), (top-right), (bottom-right), (bottom-left)... and so on...

你需要阅读一下UV Mapping。你不能用(0,0)到所有4个角来初始化它。您需要指定0到1之间的数字,这是纹理宽度和高度的百分比。意思是,如果顶点像(左上角),(右上角),Face4上的完整纹理将是(0,0),(1,0),(1,1),(0,1), (右下角),(左下角)......等等......

#1


5  

You need to read a bit on UV Mapping. You can't initialize it with (0,0) to all 4 corners. You need to specify a number between 0 to 1 which is the percentage of the texture's width and height. Meaning, a full texture on a Face4 would be (0,0), (1,0), (1,1), (0,1) if the vertices are like (top-left), (top-right), (bottom-right), (bottom-left)... and so on...

你需要阅读一下UV Mapping。你不能用(0,0)到所有4个角来初始化它。您需要指定0到1之间的数字,这是纹理宽度和高度的百分比。意思是,如果顶点像(左上角),(右上角),Face4上的完整纹理将是(0,0),(1,0),(1,1),(0,1), (右下角),(左下角)......等等......