Sam
Sam

Reputation: 2446

Drawing 3D Geometry from 2D Objects in ThreeJS

I am very to new to Three.js, and I been trying to implement the following:

Suppose that I have an array of x and y coordinates for a certain shape from the browser view-port. I would like to render these shapes in Three.JS such that I can add a height in the z direction.

The shape itself is actually a "building", where I have the array of its floorplan coordinates, and I would like to 3Dify it by adding a height in the Z-direction.

What Geometry/Material/Technique should I use to achieve this?

Upvotes: 3

Views: 5744

Answers (1)

Peter Holm
Peter Holm

Reputation: 159

Using the ExtrudeGeometry, you can extrude a 2D element (coordinates) to a 3D object in Three.js.

Example here: http://stemkoski.github.io/Three.js/Extrusion.html

Documentation here: http://threejs.org/docs/#Reference/Extras.Geometries/ExtrudeGeometry

Code from the example:

var starPoints = [];

starPoints.push( new THREE.Vector2 (   0,  50 ) );
starPoints.push( new THREE.Vector2 (  10,  10 ) );
starPoints.push( new THREE.Vector2 (  40,  10 ) );
starPoints.push( new THREE.Vector2 (  20, -10 ) );
starPoints.push( new THREE.Vector2 (  30, -50 ) );
starPoints.push( new THREE.Vector2 (   0, -20 ) );
starPoints.push( new THREE.Vector2 ( -30, -50 ) );
starPoints.push( new THREE.Vector2 ( -20, -10 ) );
starPoints.push( new THREE.Vector2 ( -40,  10 ) );
starPoints.push( new THREE.Vector2 ( -10,  10 ) );

var starShape = new THREE.Shape( starPoints );

var extrusionSettings = {
    size: 30, height: 4, curveSegments: 3,
    bevelThickness: 1, bevelSize: 2, bevelEnabled: false,
    material: 0, extrudeMaterial: 1
};

var starGeometry = new THREE.ExtrudeGeometry( starShape, extrusionSettings );

var materialFront = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
var materialSide = new THREE.MeshBasicMaterial( { color: 0xff8800 } );
var materialArray = [ materialFront, materialSide ];
var starMaterial = new THREE.MeshFaceMaterial(materialArray);

var star = new THREE.Mesh( starGeometry, starMaterial );
star.position.set(0,50,0);
scene.add(star);

// add a wireframe to model
var wireframeTexture = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } ); 
var star = new THREE.Mesh( starGeometry, wireframeTexture );
star.position.set(0,50,0);
scene.add(star);

Upvotes: 8

Related Questions