DirectGeometry.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.tangents = [];
  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.tangentsNeedUpdate = 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. return this;
  36. },
  37. computeVertexNormals: function () {
  38. console.warn( 'THREE.DirectGeometry: computeVertexNormals() is not a method of this type of geometry.' );
  39. return this;
  40. },
  41. computeTangents: function () {
  42. console.warn( 'THREE.DirectGeometry: computeTangents() is not a method of this type of geometry.' );
  43. return this;
  44. },
  45. fromGeometry: function ( geometry ) {
  46. var faces = geometry.faces;
  47. var vertices = geometry.vertices;
  48. var faceVertexUvs = geometry.faceVertexUvs;
  49. var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  50. var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
  51. var hasTangents = geometry.hasTangents;
  52. // morphs
  53. var morphTargets = geometry.morphTargets;
  54. var morphTargetsLength = morphTargets.length;
  55. if ( morphTargetsLength > 0 ) {
  56. var morphTargetsPosition = [];
  57. for ( var i = 0; i < morphTargetsLength; i ++ ) {
  58. morphTargetsPosition[ i ] = [];
  59. }
  60. this.morphTargets.position = morphTargetsPosition;
  61. }
  62. var morphNormals = geometry.morphNormals;
  63. var morphNormalsLength = morphNormals.length;
  64. if ( morphNormalsLength > 0 ) {
  65. var morphTargetsNormal = [];
  66. for ( var i = 0; i < morphNormalsLength; i ++ ) {
  67. morphTargetsNormal[ i ] = [];
  68. }
  69. this.morphTargets.normal = morphTargetsNormal;
  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. 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.verticesNeedUpdate = geometry.verticesNeedUpdate;
  139. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  140. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  141. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  142. this.tangentsNeedUpdate = geometry.tangentsNeedUpdate;
  143. return this;
  144. },
  145. dispose: function () {
  146. this.dispatchEvent( { type: 'dispose' } );
  147. }
  148. };
  149. THREE.EventDispatcher.prototype.apply( THREE.DirectGeometry.prototype );
粤ICP备19079148号