如何在three.js中创建一个带有normal和UV的动态网格?

时间:2012-05-27 19:23:21

标签: javascript webgl three.js

我知道如何在three.js中动态创建网格,但不清楚如何在网格中添加自定义UV和法线。这里是如何创建自定义网格,我们如何以编程方式向此网格添加UV和法线?

谢谢!

var imgTexture = THREE.ImageUtils.loadTexture(image);
var avatarGeom = new THREE.Geometry();

avatarGeom.vertices.push(new THREE.Vector3(0.895813,0.732893,0));
avatarGeom.vertices.push(new THREE.Vector3(-1.007173,0.732893,0));
avatarGeom.vertices.push(new THREE.Vector3(0.895813,-1.390674,0));
avatarGeom.vertices.push(new THREE.Vector3(-1.007173,-1.390674,0));

var face = new THREE.Face3(2,3,1);
avatarGeom.faces.push(face);

face = new THREE.Face3(0,2,1);
avatarGeom.faces.push(face);

var avatar = new THREE.Mesh(avatarGeom,new THREE.MeshLambertMaterial(imgTexture));
avatar.doubleSided = true;
scene.add(avatar);

1 个答案:

答案 0 :(得分:7)

正常进入face。 UV会进入geometry

var imgTexture = THREE.ImageUtils.loadTexture(image);
var avatarGeom = new THREE.Geometry();

avatarGeom.vertices.push(new THREE.Vector3(0.895813,0.732893,0));
avatarGeom.vertices.push(new THREE.Vector3(-1.007173,0.732893,0));
avatarGeom.vertices.push(new THREE.Vector3(0.895813,-1.390674,0));
avatarGeom.vertices.push(new THREE.Vector3(-1.007173,-1.390674,0));

var face = new THREE.Face3(2,3,1);
face.normal.set(0,0,1); // normal
avatarGeom.faces.push(face);
avatarGeom.faceVertexUvs[0].push([new THREE.UV(1,1),new THREE.UV(0,1),new THREE.UV(1,0)]); // uvs

face = new THREE.Face3(0,2,1);
face.normal.set(0,0,1); // normal
avatarGeom.faces.push(face);
avatarGeom.faceVertexUvs[0].push([new THREE.UV(0,0),new THREE.UV(1,1),new THREE.UV(1,0)]); // uvs

var avatar = new THREE.Mesh(avatarGeom,new THREE.MeshLambertMaterial(imgTexture));
avatar.doubleSided = true;
scene.add(avatar);

UV进入几何体的原因是因为这样我们可以有不同的通道(相同几何体的不同UV组)。