Skip to content Skip to sidebar Skip to footer

How To Draw Walls In Threejs From Path Or 2d Array?

I need to draw a 3d house model (walls only) from a 2d path or array (explained later) I receive from FabricJS editor I've built. The type of data sent from 2d to 3d views doesn't

Solution 1:

You can manually populate the vertex and face arrays of a THREE.js mesh, so if you can export the closed path you need for example as an array of coordinates, you can iterate over it, and push needed information to your wall object.

Something like this

var coordArray = [...]; //Array of corner points of a closed shape from your source. Here assumed to be THREE.Vector2() for simplicity.var walls = newTHREE.Geometry();

for(var i = 0; i < coordArray.length(); i++){ //iterate over the coordinate array, pushing vertices to the geometryvar coordinates = coordArray[i];
  walls.vertices.push(newTHREE.Vector3(coordinates.x, coordinates.y, 0)); //vertex at floor level
  walls.vertices.push(newTHREE.Vector3(coordinates.x, coordinates.y, 10)); //vertex at the top part of the wall, directly above the last
}
var previousVertexIndex = walls.vertices.length - 2; // index of the vertex at the bottom of the wall, in the segment we are creating faces forfor(var i = 0; i < walls.vertices.length; i += 2){
  walls.faces.push(newTHREE.Face3(i, i + 1, previousVertexIndex));
  walls.faces.push(newTHREE.Face3(i + 1, previousVertexIndex + 1, previousVertexIndex));
  previousVertexIndex = i;
}
walls.computeVertexNormals();
walls.computeFaceNormals();

scene.add(newTHREE.Mesh(walls, newTHREE.MeshLambertMaterial());

Post a Comment for "How To Draw Walls In Threejs From Path Or 2d Array?"