DirectGeometry.js 5.4 KB

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