DirectGeometry.js 4.9 KB

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