ShapeGeometry.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @author jonobr1 / http://jonobr1.com
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. import { Geometry } from '../core/Geometry.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. import { ShapeUtils } from '../extras/ShapeUtils.js';
  9. // ShapeGeometry
  10. function ShapeGeometry( shapes, curveSegments ) {
  11. Geometry.call( this );
  12. this.type = 'ShapeGeometry';
  13. if ( typeof curveSegments === 'object' ) {
  14. console.warn( 'THREE.ShapeGeometry: Options parameter has been removed.' );
  15. curveSegments = curveSegments.curveSegments;
  16. }
  17. this.parameters = {
  18. shapes: shapes,
  19. curveSegments: curveSegments
  20. };
  21. this.fromBufferGeometry( new ShapeBufferGeometry( shapes, curveSegments ) );
  22. this.mergeVertices();
  23. }
  24. ShapeGeometry.prototype = Object.create( Geometry.prototype );
  25. ShapeGeometry.prototype.constructor = ShapeGeometry;
  26. ShapeGeometry.prototype.toJSON = function () {
  27. var data = Geometry.prototype.toJSON.call( this );
  28. var shapes = this.parameters.shapes;
  29. return toJSON( shapes, data );
  30. };
  31. // ShapeBufferGeometry
  32. function ShapeBufferGeometry( shapes, curveSegments ) {
  33. BufferGeometry.call( this );
  34. this.type = 'ShapeBufferGeometry';
  35. this.parameters = {
  36. shapes: shapes,
  37. curveSegments: curveSegments
  38. };
  39. curveSegments = curveSegments || 12;
  40. // buffers
  41. var indices = [];
  42. var vertices = [];
  43. var normals = [];
  44. var uvs = [];
  45. // helper variables
  46. var groupStart = 0;
  47. var groupCount = 0;
  48. // allow single and array values for "shapes" parameter
  49. if ( Array.isArray( shapes ) === false ) {
  50. addShape( shapes );
  51. } else {
  52. for ( var i = 0; i < shapes.length; i ++ ) {
  53. addShape( shapes[ i ] );
  54. this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
  55. groupStart += groupCount;
  56. groupCount = 0;
  57. }
  58. }
  59. // build geometry
  60. this.setIndex( indices );
  61. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  62. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  63. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  64. // helper functions
  65. function addShape( shape ) {
  66. var i, l, shapeHole;
  67. var indexOffset = vertices.length / 3;
  68. var points = shape.extractPoints( curveSegments );
  69. var shapeVertices = points.shape;
  70. var shapeHoles = points.holes;
  71. // check direction of vertices
  72. if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
  73. shapeVertices = shapeVertices.reverse();
  74. // also check if holes are in the opposite direction
  75. for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
  76. shapeHole = shapeHoles[ i ];
  77. if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
  78. shapeHoles[ i ] = shapeHole.reverse();
  79. }
  80. }
  81. }
  82. var faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
  83. // join vertices of inner and outer paths to a single array
  84. for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
  85. shapeHole = shapeHoles[ i ];
  86. shapeVertices = shapeVertices.concat( shapeHole );
  87. }
  88. // vertices, normals, uvs
  89. for ( i = 0, l = shapeVertices.length; i < l; i ++ ) {
  90. var vertex = shapeVertices[ i ];
  91. vertices.push( vertex.x, vertex.y, 0 );
  92. normals.push( 0, 0, 1 );
  93. uvs.push( vertex.x, vertex.y ); // world uvs
  94. }
  95. // incides
  96. for ( i = 0, l = faces.length; i < l; i ++ ) {
  97. var face = faces[ i ];
  98. var a = face[ 0 ] + indexOffset;
  99. var b = face[ 1 ] + indexOffset;
  100. var c = face[ 2 ] + indexOffset;
  101. indices.push( a, b, c );
  102. groupCount += 3;
  103. }
  104. }
  105. }
  106. ShapeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  107. ShapeBufferGeometry.prototype.constructor = ShapeBufferGeometry;
  108. ShapeBufferGeometry.prototype.toJSON = function () {
  109. var data = BufferGeometry.prototype.toJSON.call( this );
  110. var shapes = this.parameters.shapes;
  111. return toJSON( shapes, data );
  112. };
  113. //
  114. function toJSON( shapes, data ) {
  115. data.shapes = [];
  116. if ( Array.isArray( shapes ) ) {
  117. for ( var i = 0, l = shapes.length; i < l; i ++ ) {
  118. var shape = shapes[ i ];
  119. data.shapes.push( shape.uuid );
  120. }
  121. } else {
  122. data.shapes.push( shapes.uuid );
  123. }
  124. return data;
  125. }
  126. export { ShapeGeometry, ShapeBufferGeometry };