Reputation: 549
How to render 2D shape of points in three.js? I didnt find any working geometry... I just need to make polygon of points that are in the same plane.
Shape isnt right for that as it supports only 2D coords and I need to use 3D points...
I am using the following code to create shape within the X,Y plane
var squareShape = new THREE.Shape();
squareShape.moveTo( 0,0 );
squareShape.lineTo( 0, 50 );
squareShape.lineTo( 20, 80 );
squareShape.lineTo( 50, 50 );
squareShape.lineTo( 0, 0 );
How to make in work it 3D world? Any solution? like:
squareShape.moveTo( 0,0,0 );
squareShape.lineTo( 0, 50, 50 );
etc
Upvotes: 3
Views: 25450
Reputation: 104763
You can create a polygon from 3D points like so:
// geometry
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 0, 5, 0 ) );
geometry.vertices.push( new THREE.Vector3( 5, -5, -2 ) );
geometry.vertices.push( new THREE.Vector3( -5, -5, 2 ) );
geometry.vertices.push( new THREE.Vector3( 0, 5, 0 ) ); // close the loop
// material
var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 1 } );
// line
var line = new THREE.Line( geometry, material );
scene.add( line );
three.js r.69
Upvotes: 11
Reputation: 1093
As of r51 there is a new type of geometry, THREE.ShapeGeometry
which is a two-dimensional shape constructed on one plane. An example of its use can be found here: http://mrdoob.github.com/three.js/examples/webgl_geometry_shapes.html
You can use THREE.Shape
to construct the 2d shape and then call shape.makeGeometry()
to convert it into a THREE.ShapeGeometry
. Here's a pseudo-code example that makes a circle:
var circle = new THREE.Shape();
var radius = 6;
for (var i = 0; i < 16; i++) {
var pct = (i + 1) / 16;
var theta = pct * Math.PI * 2.0;
var x = radius * Math.cos(theta);
var y = radius * Math.sin(theta);
if (i == 0) {
circle.moveTo(x, y);
} else {
circle.lineTo(x, y);
}
}
var geometry = circle.makeGeometry();
var material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
var mesh = new THREE.Mesh(geometry, material);
The mesh
has position
, rotation
, and scale
which can help you orient the object in three dimensional space.
Upvotes: 9
Reputation: 1647
You need to create a geometry with the shapes' vertices (after extraction) and use triangulation to make the faces.
The code should look something like this:
var geometry = new THREE.Geometry();
var shapePoints = shape.extractPoints();
var faces = THREE.Shape.Utils.triangulateShape(shapePoints.shape, shapePoints.holes);
for (var i = 0; i < shapePoints.shape.length; i++) {
geometry.vertices.push(new THREE.Vector3(shapePoints.shape[i].x, 0, shapePoints.shape[i].y));
}
for (var i = 0; i < faces.length ; i++) {
var a = faces[i][2] , b = faces[i][1] , c = faces[i][0] ;
var v1 = shapePoints.shape[a], v2 = shapePoints.shape[b], v3 = shapePoints.shape[c];
geometry.faces.push( new THREE.Face3(a, b, c) );
geometry.faceVertexUvs[0].push(
[ new THREE.UV(v1.x ,v1.y ), new THREE.UV(v2.x, v2.y), new THREE.UV(v3.x, v3.y)]);
}
geometry.computeCentroids();
geometry.computeFaceNormals();
Upvotes: 1