DirectGeometry.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.vertices = [];
  10. this.colors = [];
  11. this.normals = [];
  12. this.colors = [];
  13. this.uvs = [];
  14. this.uvs2 = [];
  15. this.morphTargets = [];
  16. this.morphColors = [];
  17. this.morphNormals = [];
  18. this.skinWeights = [];
  19. this.skinIndices = [];
  20. // this.lineDistances = [];
  21. this.boundingBox = null;
  22. this.boundingSphere = null;
  23. // update flags
  24. this.verticesNeedUpdate = false;
  25. this.normalsNeedUpdate = false;
  26. this.colorsNeedUpdate = false;
  27. this.uvsNeedUpdate = 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. fromGeometry: function ( geometry, material ) {
  42. material = material || { 'vertexColors': THREE.NoColors };
  43. var faces = geometry.faces;
  44. var vertices = geometry.vertices;
  45. var faceVertexUvs = geometry.faceVertexUvs;
  46. var materialVertexColors = material.vertexColors;
  47. var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  48. var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
  49. // morphs
  50. var morphTargets = geometry.morphTargets;
  51. var morphTargetsLength = morphTargets.length;
  52. for ( var i = 0; i < morphTargetsLength; i ++ ) {
  53. this.morphTargets[ i ] = [];
  54. }
  55. var morphNormals = geometry.morphNormals;
  56. var morphNormalsLength = morphNormals.length;
  57. for ( var i = 0; i < morphNormalsLength; i ++ ) {
  58. this.morphNormals[ i ] = [];
  59. }
  60. var morphColors = geometry.morphColors;
  61. var morphColorsLength = morphColors.length;
  62. for ( var i = 0; i < morphColorsLength; i ++ ) {
  63. this.morphColors[ i ] = [];
  64. }
  65. // skins
  66. var skinIndices = geometry.skinIndices;
  67. var skinWeights = geometry.skinWeights;
  68. var hasSkinIndices = skinIndices.length === vertices.length;
  69. var hasSkinWeights = skinWeights.length === vertices.length;
  70. //
  71. for ( var i = 0; i < faces.length; i ++ ) {
  72. var face = faces[ i ];
  73. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  74. var vertexNormals = face.vertexNormals;
  75. if ( vertexNormals.length === 3 ) {
  76. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  77. } else {
  78. var normal = face.normal;
  79. this.normals.push( normal, normal, normal );
  80. }
  81. var vertexColors = face.vertexColors;
  82. if ( materialVertexColors === THREE.VertexColors ) {
  83. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  84. } else if ( materialVertexColors === THREE.FaceColors ) {
  85. var color = face.color;
  86. this.colors.push( color, color, color );
  87. }
  88. if ( hasFaceVertexUv === true ) {
  89. var vertexUvs = faceVertexUvs[ 0 ][ i ];
  90. if ( vertexUvs !== undefined ) {
  91. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  92. } else {
  93. console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv', i );
  94. this.uvs.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  95. }
  96. }
  97. if ( hasFaceVertexUv2 === true ) {
  98. var vertexUvs = faceVertexUvs[ 1 ][ i ];
  99. if ( vertexUvs !== undefined ) {
  100. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  101. } else {
  102. console.warn( 'THREE.BufferGeometry.fromGeometry(): Undefined vertexUv2', i );
  103. this.uvs2.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  104. }
  105. }
  106. // morphs
  107. for ( var j = 0; j < morphTargetsLength; j ++ ) {
  108. var morphTarget = morphTargets[ j ].vertices;
  109. this.morphTargets[ j ].push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  110. }
  111. for ( var j = 0; j < morphNormalsLength; j ++ ) {
  112. var morphNormal = morphNormals[ j ].normals;
  113. this.morphNormals[ j ].push( morphNormal[ face.a ], morphNormal[ face.b ], morphNormal[ face.c ] );
  114. }
  115. for ( var j = 0; j < morphColorsLength; j ++ ) {
  116. var morphColor = morphColors[ j ].colors;
  117. this.morphColors[ j ].push( morphColor[ face.a ], morphColor[ face.b ], morphColor[ face.c ] );
  118. }
  119. // skins
  120. if ( hasSkinIndices ) {
  121. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  122. }
  123. if ( hasSkinWeights ) {
  124. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  125. }
  126. }
  127. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  128. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  129. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  130. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  131. return this;
  132. },
  133. dispose: function () {
  134. this.dispatchEvent( { type: 'dispose' } );
  135. }
  136. };
  137. THREE.EventDispatcher.prototype.apply( THREE.DirectGeometry.prototype );