How to create a custom mesh on THREE.JS?

前端 未结 3 1143
情深已故
情深已故 2020-12-01 02:36

I\'ve asked this and got the answer:

var geom = new THREE.Geometry(); 
var v1 = new THREE.Vector3(0,0,0);
var v2 = new         


        
3条回答
  •  Happy的楠姐
    2020-12-01 03:25

    You've added vertices, but forgot to put those vertices into a face and add that to the geometry:

    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    

    so your snippet becomes:

    var geom = new THREE.Geometry(); 
    var v1 = new THREE.Vector3(0,0,0);
    var v2 = new THREE.Vector3(0,500,0);
    var v3 = new THREE.Vector3(0,500,500);
    
    geom.vertices.push(v1);
    geom.vertices.push(v2);
    geom.vertices.push(v3);
    
    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    
    var object = new THREE.Mesh( geom, new THREE.MeshNormalMaterial() );
    scene.addObject(object);
    

    The idea is that a Face3 instance references 3 vertices(the x,y,z coords you've added previously to the geometry) by using the indices of the vertices in the list/array. Currently you only have 3 vertices and you want to connect them,so your face references index 0,1 and 2 in the vertices array.

    Since you're using a mesh normals material, you might want to compute normals for the geometry. Also, make sure your object can be visible (is not to big or to close to the camera to be clipped out, is facing the right direction - towards the camera, etc.) Since you're drawing in the YZ plane, to see your triangle, something like this should work:

    var geom = new THREE.Geometry(); 
    var v1 = new THREE.Vector3(0,0,0);
    var v2 = new THREE.Vector3(0,500,0);
    var v3 = new THREE.Vector3(0,500,500);
    
    geom.vertices.push(v1);
    geom.vertices.push(v2);
    geom.vertices.push(v3);
    
    geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
    geom.computeFaceNormals();
    
    var object = new THREE.Mesh( geom, new THREE.MeshNormalMaterial() );
    
    object.position.z = -100;//move a bit back - size of 500 is a bit big
    object.rotation.y = -Math.PI * .5;//triangle is pointing in depth, rotate it -90 degrees on Y
    
    scene.add(object);
    

提交回复
热议问题