import { Geometry } from '../core/Geometry'; import { Face3 } from '../core/Face3'; import { Vector3 } from '../math/Vector3'; import { ShapeUtils } from '../extras/ShapeUtils'; import { ExtrudeGeometry } from './ExtrudeGeometry'; /** * @author jonobr1 / http://jonobr1.com * * Creates a one-sided polygonal geometry from a path shape. Similar to * ExtrudeGeometry. * * parameters = { * * curveSegments: , // number of points on the curves. NOT USED AT THE MOMENT. * * material: // material index for front and back faces * uvGenerator: // object that provides UV generator functions * * } **/ function ShapeGeometry( shapes, options ) { Geometry.call( this ); this.type = 'ShapeGeometry'; if ( Array.isArray( shapes ) === false ) shapes = [ shapes ]; this.addShapeList( shapes, options ); this.computeFaceNormals(); } ShapeGeometry.prototype = Object.create( Geometry.prototype ); ShapeGeometry.prototype.constructor = ShapeGeometry; /** * Add an array of shapes to THREE.ShapeGeometry. */ 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. */ 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 ? 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 = ! ShapeUtils.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 ( ShapeUtils.isClockWise( hole ) ) { holes[ i ] = hole.reverse(); } } reverse = false; } var faces = ShapeUtils.triangulateShape( vertices, holes ); // 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 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 Face3( a, b, c, null, null, material ) ); this.faceVertexUvs[ 0 ].push( uvgen.generateTopUV( this, a, b, c ) ); } }; export { ShapeGeometry };