Geometry.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.Geometry = function () {
  7. this.vertices = [];
  8. this.faces = [];
  9. this.uvs = [];
  10. this.geometryChunks = {};
  11. };
  12. THREE.Geometry.prototype = {
  13. computeCentroids: function () {
  14. var f, fl, face;
  15. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  16. face = this.faces[ f ];
  17. face.centroid.set( 0, 0, 0 );
  18. if ( face instanceof THREE.Face3 ) {
  19. face.centroid.addSelf( this.vertices[ face.a ].position );
  20. face.centroid.addSelf( this.vertices[ face.b ].position );
  21. face.centroid.addSelf( this.vertices[ face.c ].position );
  22. face.centroid.divideScalar( 3 );
  23. } else if ( face instanceof THREE.Face4 ) {
  24. face.centroid.addSelf( this.vertices[ face.a ].position );
  25. face.centroid.addSelf( this.vertices[ face.b ].position );
  26. face.centroid.addSelf( this.vertices[ face.c ].position );
  27. face.centroid.addSelf( this.vertices[ face.d ].position );
  28. face.centroid.divideScalar( 4 );
  29. }
  30. }
  31. },
  32. computeNormals: function ( useVertexNormals ) {
  33. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  34. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  35. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  36. vertex = this.vertices[ v ];
  37. vertex.normal.set( 0, 0, 0 );
  38. }
  39. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  40. face = this.faces[ f ];
  41. if ( useVertexNormals && face.vertexNormals.length ) {
  42. cb.set( 0, 0, 0 );
  43. for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
  44. cb.addSelf( face.vertexNormals[n] );
  45. }
  46. cb.divideScalar( 3 );
  47. if ( ! cb.isZero() ) {
  48. cb.normalize();
  49. }
  50. face.normal.copy( cb );
  51. } else {
  52. vA = this.vertices[ face.a ];
  53. vB = this.vertices[ face.b ];
  54. vC = this.vertices[ face.c ];
  55. cb.sub( vC.position, vB.position );
  56. ab.sub( vA.position, vB.position );
  57. cb.crossSelf( ab );
  58. if ( !cb.isZero() ) {
  59. cb.normalize();
  60. }
  61. face.normal.copy( cb );
  62. }
  63. }
  64. },
  65. computeVertexNormals: function () {
  66. var v, vertices = [],
  67. f, fl, face;
  68. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  69. vertices[ v ] = new THREE.Vector3();
  70. }
  71. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  72. face = this.faces[ f ];
  73. if ( face instanceof THREE.Face3 ) {
  74. vertices[ face.a ].addSelf( face.normal );
  75. vertices[ face.b ].addSelf( face.normal );
  76. vertices[ face.c ].addSelf( face.normal );
  77. } else if ( face instanceof THREE.Face4 ) {
  78. vertices[ face.a ].addSelf( face.normal );
  79. vertices[ face.b ].addSelf( face.normal );
  80. vertices[ face.c ].addSelf( face.normal );
  81. vertices[ face.d ].addSelf( face.normal );
  82. }
  83. }
  84. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  85. vertices[ v ].normalize();
  86. }
  87. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  88. face = this.faces[ f ];
  89. if ( face instanceof THREE.Face3 ) {
  90. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  91. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  92. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  93. } else if ( face instanceof THREE.Face4 ) {
  94. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  95. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  96. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  97. face.vertexNormals[ 3 ] = vertices[ face.d ].clone();
  98. }
  99. }
  100. },
  101. computeBoundingBox: function () {
  102. if ( this.vertices.length > 0 ) {
  103. this.bbox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
  104. 'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
  105. 'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };
  106. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  107. vertex = this.vertices[ v ];
  108. if ( vertex.position.x < this.bbox.x[ 0 ] ) {
  109. this.bbox.x[ 0 ] = vertex.position.x;
  110. } else if ( vertex.position.x > this.bbox.x[ 1 ] ) {
  111. this.bbox.x[ 1 ] = vertex.position.x;
  112. }
  113. if ( vertex.position.y < this.bbox.y[ 0 ] ) {
  114. this.bbox.y[ 0 ] = vertex.position.y;
  115. } else if ( vertex.position.y > this.bbox.y[ 1 ] ) {
  116. this.bbox.y[ 1 ] = vertex.position.y;
  117. }
  118. if ( vertex.position.z < this.bbox.z[ 0 ] ) {
  119. this.bbox.z[ 0 ] = vertex.position.z;
  120. } else if ( vertex.position.z > this.bbox.z[ 1 ] ) {
  121. this.bbox.z[ 1 ] = vertex.position.z;
  122. }
  123. }
  124. }
  125. },
  126. sortFacesByMaterial: function () {
  127. // TODO
  128. // Should optimize by grouping faces with ColorFill / ColorStroke materials
  129. // which could then use vertex color attributes instead of each being
  130. // in its separate VBO
  131. var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
  132. function materialHash( material ) {
  133. var hash_array = [];
  134. for ( i = 0, l = material.length; i < l; i++ ) {
  135. if ( material[ i ] == undefined ) {
  136. hash_array.push( "undefined" );
  137. } else {
  138. hash_array.push( material[ i ].toString() );
  139. }
  140. }
  141. return hash_array.join( '_' );
  142. }
  143. for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
  144. face = this.faces[ f ];
  145. material = face.material;
  146. mhash = materialHash( material );
  147. if ( hash_map[ mhash ] == undefined ) {
  148. hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
  149. }
  150. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  151. if ( this.geometryChunks[ ghash ] == undefined ) {
  152. this.geometryChunks[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
  153. }
  154. vertices = face instanceof THREE.Face3 ? 3 : 4;
  155. if ( this.geometryChunks[ ghash ].vertices + vertices > 65535 ) {
  156. hash_map[ mhash ].counter += 1;
  157. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  158. if ( this.geometryChunks[ ghash ] == undefined ) {
  159. this.geometryChunks[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
  160. }
  161. }
  162. this.geometryChunks[ ghash ].faces.push( f );
  163. this.geometryChunks[ ghash ].vertices += vertices;
  164. }
  165. },
  166. toString: function () {
  167. return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ', uvs: ' + this.uvs + ' )';
  168. }
  169. };