DirectGeometry.js 5.3 KB

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