how do I add a texture to a triangle in three.js?

帅比萌擦擦* 提交于 2019-12-22 10:44:49

问题


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.

Does anyone know what I'm doing wrong here?


回答1:


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...



来源:https://stackoverflow.com/questions/12238944/how-do-i-add-a-texture-to-a-triangle-in-three-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!