Skip to content

Instantly share code, notes, and snippets.

@r03ert0
Created July 22, 2017 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r03ert0/06a5f91571b9b1fc59c8b91e1376a7c5 to your computer and use it in GitHub Desktop.
Save r03ert0/06a5f91571b9b1fc59c8b91e1376a7c5 to your computer and use it in GitHub Desktop.
Draw a box with triangles in three.js
// the box geometry to initialise
var box=new THREE.Geometry();
// the trick to make all faces: vectors i, j and k have the coordinates of the triangles in the box
var i="011010111111100101000000001011110100".split('').map(c=>(c=='0')?0:1),
j="000000011010111111100101011010011010".split('').map(c=>(c=='0')?0:1),
k="001011001011001011001011000000111111".split('').map(c=>(c=='0')?0:1);
// this function multiplies each of the coordinates by a transformation matrix that turns them
// into an arbitrary box
i.map(function(v,n){
var vec=MRI.multMatVec(mat,[i[n],j[n],k[n]]);
box.vertices.push(newVector(vec))
});
// faces are added to the box
for(l=0;l<12;l++)
box.faces.push(new THREE.Face3(l*3,l*3+1,l*3+2));
var material=new THREE.MeshBasicMaterial({wireframe:true});
// the final object is added to the scene
mesh=new THREE.Mesh(box,material);
scene.add(mesh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment