DirectGeometry.js 5.8 KB

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