Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

three.js : face4 generates triangle instead of square

i'm trying to generate a square with tree.js custom Geometry. but this code

var cubeGeo = new THREE.Geometry();
cubeGeo.vertices.push( new THREE.Vector3( -25,  25, -25 ) );
cubeGeo.vertices.push( new THREE.Vector3(  25,  25, -25 ) );
cubeGeo.vertices.push( new THREE.Vector3( -25, -25, -25 ) );
cubeGeo.vertices.push( new THREE.Vector3(  25,  -25, -25 ) );
cubeGeo.faces.push( new THREE.Face4( 0, 1, 2, 3, new THREE.Vector3( 0,  0,  1 ), 0xffffff, 0) );

    var cube = new THREE.Mesh(
  cubeGeo,  
  //new THREE.CubeGeometry(50, 50, 50),
  new THREE.MeshPhongMaterial({color: 0x696969, emissive: 0x696969, specular:0x696969, shininess: 15})
);

generates triangle can somebody explain me why it happens?

like image 312
user1128677 Avatar asked May 11 '13 14:05

user1128677


3 Answers

The problem is with the THREE.Face4. It has been removed in the last version. In GitHub Three.js - Wiki - Migration we can read:

r59 -> r60

Face4 is removed. Use 2 Face3 to emulate it.

And the reason why you see a triangle instead of a square is that:

THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {

    return new THREE.Face3( a, b, c, normal, color, materialIndex );

};
like image 195
jcoll Avatar answered Oct 17 '22 22:10

jcoll


Three.Face4 is deprecated.

Here's how you use 2 Face3 to make a square:

function drawSquare(x1, y1, x2, y2) { 

    var square = new THREE.Geometry(); 

    //set 4 points
    square.vertices.push( new THREE.Vector3( x1,y2,0) );
    square.vertices.push( new THREE.Vector3( x1,y1,0) );
    square.vertices.push( new THREE.Vector3( x2,y1,0) );
    square.vertices.push( new THREE.Vector3( x2,y2,0) );

    //push 1 triangle
    square.faces.push( new THREE.Face3( 0,1,2) );

    //push another triangle
    square.faces.push( new THREE.Face3( 0,3,2) );

    //return the square object with BOTH faces
    return square;
}
like image 29
Kevin Miller Avatar answered Oct 17 '22 22:10

Kevin Miller


Actually it should be drawing something like a bow-tie. The vertex order is not correct. Swap the last two vertices.

like image 3
gaitat Avatar answered Oct 17 '22 21:10

gaitat