DirectGeometry.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. var group;
  28. var groups = [];
  29. var materialIndex = undefined;
  30. var faces = geometry.faces;
  31. for ( var i = 0; i < faces.length; i ++ ) {
  32. var 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. var faces = geometry.faces;
  54. var vertices = geometry.vertices;
  55. var faceVertexUvs = geometry.faceVertexUvs;
  56. var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  57. var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
  58. // morphs
  59. var morphTargets = geometry.morphTargets;
  60. var morphTargetsLength = morphTargets.length;
  61. var morphTargetsPosition;
  62. if ( morphTargetsLength > 0 ) {
  63. morphTargetsPosition = [];
  64. for ( var i = 0; i < morphTargetsLength; i ++ ) {
  65. morphTargetsPosition[ i ] = [];
  66. }
  67. this.morphTargets.position = morphTargetsPosition;
  68. }
  69. var morphNormals = geometry.morphNormals;
  70. var morphNormalsLength = morphNormals.length;
  71. var morphTargetsNormal;
  72. if ( morphNormalsLength > 0 ) {
  73. morphTargetsNormal = [];
  74. for ( var i = 0; i < morphNormalsLength; i ++ ) {
  75. morphTargetsNormal[ i ] = [];
  76. }
  77. this.morphTargets.normal = morphTargetsNormal;
  78. }
  79. // skins
  80. var skinIndices = geometry.skinIndices;
  81. var skinWeights = geometry.skinWeights;
  82. var hasSkinIndices = skinIndices.length === vertices.length;
  83. var hasSkinWeights = skinWeights.length === vertices.length;
  84. //
  85. for ( var i = 0; i < faces.length; i ++ ) {
  86. var face = faces[ i ];
  87. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  88. var vertexNormals = face.vertexNormals;
  89. if ( vertexNormals.length === 3 ) {
  90. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  91. } else {
  92. var normal = face.normal;
  93. this.normals.push( normal, normal, normal );
  94. }
  95. var vertexColors = face.vertexColors;
  96. if ( vertexColors.length === 3 ) {
  97. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  98. } else {
  99. var color = face.color;
  100. this.colors.push( color, color, color );
  101. }
  102. if ( hasFaceVertexUv === true ) {
  103. var vertexUvs = faceVertexUvs[ 0 ][ i ];
  104. if ( vertexUvs !== undefined ) {
  105. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  106. } else {
  107. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
  108. this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
  109. }
  110. }
  111. if ( hasFaceVertexUv2 === true ) {
  112. var vertexUvs = faceVertexUvs[ 1 ][ i ];
  113. if ( vertexUvs !== undefined ) {
  114. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  115. } else {
  116. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
  117. this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
  118. }
  119. }
  120. // morphs
  121. for ( var j = 0; j < morphTargetsLength; j ++ ) {
  122. var morphTarget = morphTargets[ j ].vertices;
  123. morphTargetsPosition[ j ].push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  124. }
  125. for ( var j = 0; j < morphNormalsLength; j ++ ) {
  126. var morphNormal = morphNormals[ j ].vertexNormals[ i ];
  127. morphTargetsNormal[ j ].push( morphNormal.a, morphNormal.b, morphNormal.c );
  128. }
  129. // skins
  130. if ( hasSkinIndices ) {
  131. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  132. }
  133. if ( hasSkinWeights ) {
  134. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  135. }
  136. }
  137. this.computeGroups( geometry );
  138. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  139. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  140. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  141. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  142. this.groupsNeedUpdate = geometry.groupsNeedUpdate;
  143. return this;
  144. }
  145. } );
  146. export { DirectGeometry };
粤ICP备19079148号