2
0

DirectGeometry.js 5.6 KB

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