ShapeGeometry.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @author jonobr1 / http://jonobr1.com
  3. *
  4. * Creates a one-sided polygonal geometry from a path shape. Similar to
  5. * ExtrudeGeometry.
  6. *
  7. * parameters = {
  8. *
  9. * curveSegments: <int>, // number of points on the curves. NOT USED AT THE MOMENT.
  10. *
  11. * material: <int> // material index for front and back faces
  12. * uvGenerator: <Object> // object that provides UV generator functions
  13. *
  14. * }
  15. **/
  16. THREE.ShapeGeometry = function ( shapes, options ) {
  17. THREE.Geometry.call( this );
  18. if ( shapes instanceof Array === false ) shapes = [ shapes ];
  19. this.shapebb = shapes[ shapes.length - 1 ].getBoundingBox();
  20. this.addShapeList( shapes, options );
  21. this.computeFaceNormals();
  22. };
  23. THREE.ShapeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  24. /**
  25. * Add an array of shapes to THREE.ShapeGeometry.
  26. */
  27. THREE.ShapeGeometry.prototype.addShapeList = function ( shapes, options ) {
  28. for ( var i = 0, l = shapes.length; i < l; i ++ ) {
  29. this.addShape( shapes[ i ], options );
  30. }
  31. return this;
  32. };
  33. /**
  34. * Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry.
  35. */
  36. THREE.ShapeGeometry.prototype.addShape = function ( shape, options ) {
  37. if ( options === undefined ) options = {};
  38. var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  39. var material = options.material;
  40. var uvgen = options.UVGenerator === undefined ? THREE.ExtrudeGeometry.WorldUVGenerator : options.UVGenerator;
  41. var shapebb = this.shapebb;
  42. //
  43. var i, l, hole, s;
  44. var shapesOffset = this.vertices.length;
  45. var shapePoints = shape.extractPoints( curveSegments );
  46. var vertices = shapePoints.shape;
  47. var holes = shapePoints.holes;
  48. var reverse = ! THREE.Shape.Utils.isClockWise( vertices );
  49. if ( reverse ) {
  50. vertices = vertices.reverse();
  51. // Maybe we should also check if holes are in the opposite direction, just to be safe...
  52. for ( i = 0, l = holes.length; i < l; i ++ ) {
  53. hole = holes[ i ];
  54. if ( THREE.Shape.Utils.isClockWise( hole ) ) {
  55. holes[ i ] = hole.reverse();
  56. }
  57. }
  58. reverse = false;
  59. }
  60. var faces = THREE.Shape.Utils.triangulateShape( vertices, holes );
  61. // Vertices
  62. var contour = vertices;
  63. for ( i = 0, l = holes.length; i < l; i ++ ) {
  64. hole = holes[ i ];
  65. vertices = vertices.concat( hole );
  66. }
  67. //
  68. var vert, vlen = vertices.length;
  69. var face, flen = faces.length;
  70. var cont, clen = contour.length;
  71. for ( i = 0; i < vlen; i ++ ) {
  72. vert = vertices[ i ];
  73. this.vertices.push( new THREE.Vector3( vert.x, vert.y, 0 ) );
  74. }
  75. for ( i = 0; i < flen; i ++ ) {
  76. face = faces[ i ];
  77. var a = face[ 0 ] + shapesOffset;
  78. var b = face[ 1 ] + shapesOffset;
  79. var c = face[ 2 ] + shapesOffset;
  80. this.faces.push( new THREE.Face3( a, b, c, null, null, material ) );
  81. this.faceVertexUvs[ 0 ].push( uvgen.generateBottomUV( this, shape, options, a, b, c ) );
  82. }
  83. };