ShapeGeometry.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.computeCentroids();
  22. this.computeFaceNormals();
  23. };
  24. THREE.ShapeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  25. /**
  26. * Add an array of shapes to THREE.ShapeGeometry.
  27. */
  28. THREE.ShapeGeometry.prototype.addShapeList = function ( shapes, options ) {
  29. for ( var i = 0, l = shapes.length; i < l; i++ ) {
  30. this.addShape( shapes[ i ], options );
  31. }
  32. return this;
  33. };
  34. /**
  35. * Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry.
  36. */
  37. THREE.ShapeGeometry.prototype.addShape = function ( shape, options ) {
  38. if ( options === undefined ) options = {};
  39. var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  40. var material = options.material;
  41. var uvgen = options.UVGenerator === undefined ? THREE.ExtrudeGeometry.WorldUVGenerator : options.UVGenerator;
  42. var shapebb = this.shapebb;
  43. //
  44. var i, l, hole, s;
  45. var shapesOffset = this.vertices.length;
  46. var shapePoints = shape.extractPoints( curveSegments );
  47. var vertices = shapePoints.shape;
  48. var holes = shapePoints.holes;
  49. var reverse = !THREE.Shape.Utils.isClockWise( vertices );
  50. if ( reverse ) {
  51. vertices = vertices.reverse();
  52. // Maybe we should also check if holes are in the opposite direction, just to be safe...
  53. for ( i = 0, l = holes.length; i < l; i++ ) {
  54. hole = holes[ i ];
  55. if ( THREE.Shape.Utils.isClockWise( hole ) ) {
  56. holes[ i ] = hole.reverse();
  57. }
  58. }
  59. reverse = false;
  60. }
  61. var faces = THREE.Shape.Utils.triangulateShape( vertices, holes );
  62. // Vertices
  63. var contour = vertices;
  64. for ( i = 0, l = holes.length; i < l; i++ ) {
  65. hole = holes[ i ];
  66. vertices = vertices.concat( hole );
  67. }
  68. //
  69. var vert, vlen = vertices.length;
  70. var face, flen = faces.length;
  71. var cont, clen = contour.length;
  72. for ( i = 0; i < vlen; i++ ) {
  73. vert = vertices[ i ];
  74. this.vertices.push( new THREE.Vector3( vert.x, vert.y, 0 ) );
  75. }
  76. for ( i = 0; i < flen; i++ ) {
  77. face = faces[ i ];
  78. var a = face[ 0 ] + shapesOffset;
  79. var b = face[ 1 ] + shapesOffset;
  80. var c = face[ 2 ] + shapesOffset;
  81. this.faces.push( new THREE.Face3( a, b, c, null, null, material ) );
  82. this.faceVertexUvs[ 0 ].push( uvgen.generateBottomUV( this, shape, options, a, b, c ) );
  83. }
  84. };