content/hifi-content/dave/walk-tools/walkTools/libraries/three.js/extras/geometries/ShapeGeometry.js
2022-02-13 22:49:05 +01:00

133 lines
2.7 KiB
JavaScript

/**
* @author jonobr1 / http://jonobr1.com
*
* Creates a one-sided polygonal geometry from a path shape. Similar to
* ExtrudeGeometry.
*
* parameters = {
*
* curveSegments: <int>, // number of points on the curves. NOT USED AT THE MOMENT.
*
* material: <int> // material index for front and back faces
* uvGenerator: <Object> // object that provides UV generator functions
*
* }
**/
THREE.ShapeGeometry = function ( shapes, options ) {
THREE.Geometry.call( this );
this.type = 'ShapeGeometry';
if ( shapes instanceof Array === false ) shapes = [ shapes ];
this.addShapeList( shapes, options );
this.computeFaceNormals();
};
THREE.ShapeGeometry.prototype = Object.create( THREE.Geometry.prototype );
THREE.ShapeGeometry.prototype.constructor = THREE.ShapeGeometry;
/**
* Add an array of shapes to THREE.ShapeGeometry.
*/
THREE.ShapeGeometry.prototype.addShapeList = function ( shapes, options ) {
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
this.addShape( shapes[ i ], options );
}
return this;
};
/**
* Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry.
*/
THREE.ShapeGeometry.prototype.addShape = function ( shape, options ) {
if ( options === undefined ) options = {};
var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
var material = options.material;
var uvgen = options.UVGenerator === undefined ? THREE.ExtrudeGeometry.WorldUVGenerator : options.UVGenerator;
//
var i, l, hole;
var shapesOffset = this.vertices.length;
var shapePoints = shape.extractPoints( curveSegments );
var vertices = shapePoints.shape;
var holes = shapePoints.holes;
var reverse = ! THREE.Shape.Utils.isClockWise( vertices );
if ( reverse ) {
vertices = vertices.reverse();
// Maybe we should also check if holes are in the opposite direction, just to be safe...
for ( i = 0, l = holes.length; i < l; i ++ ) {
hole = holes[ i ];
if ( THREE.Shape.Utils.isClockWise( hole ) ) {
holes[ i ] = hole.reverse();
}
}
reverse = false;
}
var faces = THREE.Shape.Utils.triangulateShape( vertices, holes );
// Vertices
var contour = vertices;
for ( i = 0, l = holes.length; i < l; i ++ ) {
hole = holes[ i ];
vertices = vertices.concat( hole );
}
//
var vert, vlen = vertices.length;
var face, flen = faces.length;
for ( i = 0; i < vlen; i ++ ) {
vert = vertices[ i ];
this.vertices.push( new THREE.Vector3( vert.x, vert.y, 0 ) );
}
for ( i = 0; i < flen; i ++ ) {
face = faces[ i ];
var a = face[ 0 ] + shapesOffset;
var b = face[ 1 ] + shapesOffset;
var c = face[ 2 ] + shapesOffset;
this.faces.push( new THREE.Face3( a, b, c, null, null, material ) );
this.faceVertexUvs[ 0 ].push( uvgen.generateTopUV( this, a, b, c ) );
}
};