1
0

DirectGeometry.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { Geometry } from './Geometry';
  2. import { EventDispatcher } from './EventDispatcher';
  3. import { Vector2 } from '../math/Vector2';
  4. import { _Math } from '../math/Math';
  5. import { GeometryIdCount } from './Geometry';
  6. /**
  7. * @author mrdoob / http://mrdoob.com/
  8. */
  9. function DirectGeometry () {
  10. this.isDirectGeometry = true;
  11. Object.defineProperty( this, 'id', { value: GeometryIdCount() } );
  12. this.uuid = _Math.generateUUID();
  13. this.name = '';
  14. this.type = 'DirectGeometry';
  15. this.indices = [];
  16. this.vertices = [];
  17. this.normals = [];
  18. this.colors = [];
  19. this.uvs = [];
  20. this.uvs2 = [];
  21. this.groups = [];
  22. this.morphTargets = {};
  23. this.skinWeights = [];
  24. this.skinIndices = [];
  25. // this.lineDistances = [];
  26. this.boundingBox = null;
  27. this.boundingSphere = null;
  28. // update flags
  29. this.verticesNeedUpdate = false;
  30. this.normalsNeedUpdate = false;
  31. this.colorsNeedUpdate = false;
  32. this.uvsNeedUpdate = false;
  33. this.groupsNeedUpdate = false;
  34. };
  35. Object.assign( DirectGeometry.prototype, EventDispatcher.prototype, {
  36. computeBoundingBox: Geometry.prototype.computeBoundingBox,
  37. computeBoundingSphere: Geometry.prototype.computeBoundingSphere,
  38. computeFaceNormals: function () {
  39. console.warn( 'THREE.DirectGeometry: computeFaceNormals() is not a method of this type of geometry.' );
  40. },
  41. computeVertexNormals: function () {
  42. console.warn( 'THREE.DirectGeometry: computeVertexNormals() is not a method of this type of geometry.' );
  43. },
  44. computeGroups: function ( geometry ) {
  45. var group;
  46. var groups = [];
  47. var materialIndex;
  48. var faces = geometry.faces;
  49. for ( var i = 0; i < faces.length; i ++ ) {
  50. var face = faces[ i ];
  51. // materials
  52. if ( face.materialIndex !== materialIndex ) {
  53. materialIndex = face.materialIndex;
  54. if ( group !== undefined ) {
  55. group.count = ( i * 3 ) - group.start;
  56. groups.push( group );
  57. }
  58. group = {
  59. start: i * 3,
  60. materialIndex: materialIndex
  61. };
  62. }
  63. }
  64. if ( group !== undefined ) {
  65. group.count = ( i * 3 ) - group.start;
  66. groups.push( group );
  67. }
  68. this.groups = groups;
  69. },
  70. fromGeometry: function ( geometry ) {
  71. var faces = geometry.faces;
  72. var vertices = geometry.vertices;
  73. var faceVertexUvs = geometry.faceVertexUvs;
  74. var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  75. var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
  76. // morphs
  77. var morphTargets = geometry.morphTargets;
  78. var morphTargetsLength = morphTargets.length;
  79. var morphTargetsPosition;
  80. if ( morphTargetsLength > 0 ) {
  81. morphTargetsPosition = [];
  82. for ( var i = 0; i < morphTargetsLength; i ++ ) {
  83. morphTargetsPosition[ i ] = [];
  84. }
  85. this.morphTargets.position = morphTargetsPosition;
  86. }
  87. var morphNormals = geometry.morphNormals;
  88. var morphNormalsLength = morphNormals.length;
  89. var morphTargetsNormal;
  90. if ( morphNormalsLength > 0 ) {
  91. morphTargetsNormal = [];
  92. for ( var i = 0; i < morphNormalsLength; i ++ ) {
  93. morphTargetsNormal[ i ] = [];
  94. }
  95. this.morphTargets.normal = morphTargetsNormal;
  96. }
  97. // skins
  98. var skinIndices = geometry.skinIndices;
  99. var skinWeights = geometry.skinWeights;
  100. var hasSkinIndices = skinIndices.length === vertices.length;
  101. var hasSkinWeights = skinWeights.length === vertices.length;
  102. //
  103. for ( var i = 0; i < faces.length; i ++ ) {
  104. var face = faces[ i ];
  105. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  106. var vertexNormals = face.vertexNormals;
  107. if ( vertexNormals.length === 3 ) {
  108. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  109. } else {
  110. var normal = face.normal;
  111. this.normals.push( normal, normal, normal );
  112. }
  113. var vertexColors = face.vertexColors;
  114. if ( vertexColors.length === 3 ) {
  115. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  116. } else {
  117. var color = face.color;
  118. this.colors.push( color, color, color );
  119. }
  120. if ( hasFaceVertexUv === true ) {
  121. var vertexUvs = faceVertexUvs[ 0 ][ i ];
  122. if ( vertexUvs !== undefined ) {
  123. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  124. } else {
  125. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
  126. this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
  127. }
  128. }
  129. if ( hasFaceVertexUv2 === true ) {
  130. var vertexUvs = faceVertexUvs[ 1 ][ i ];
  131. if ( vertexUvs !== undefined ) {
  132. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  133. } else {
  134. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
  135. this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
  136. }
  137. }
  138. // morphs
  139. for ( var j = 0; j < morphTargetsLength; j ++ ) {
  140. var morphTarget = morphTargets[ j ].vertices;
  141. morphTargetsPosition[ j ].push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  142. }
  143. for ( var j = 0; j < morphNormalsLength; j ++ ) {
  144. var morphNormal = morphNormals[ j ].vertexNormals[ i ];
  145. morphTargetsNormal[ j ].push( morphNormal.a, morphNormal.b, morphNormal.c );
  146. }
  147. // skins
  148. if ( hasSkinIndices ) {
  149. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  150. }
  151. if ( hasSkinWeights ) {
  152. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  153. }
  154. }
  155. this.computeGroups( geometry );
  156. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  157. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  158. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  159. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  160. this.groupsNeedUpdate = geometry.groupsNeedUpdate;
  161. return this;
  162. },
  163. dispose: function () {
  164. this.dispatchEvent( { type: 'dispose' } );
  165. }
  166. } );
  167. export { DirectGeometry };
粤ICP备19079148号