Geometry.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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.boundingBox = null;
  11. this.boundingSphere = null;
  12. this.geometryChunks = {};
  13. this.hasTangents = false;
  14. };
  15. THREE.Geometry.prototype = {
  16. computeCentroids: function () {
  17. var f, fl, face;
  18. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  19. face = this.faces[ f ];
  20. face.centroid.set( 0, 0, 0 );
  21. if ( face instanceof THREE.Face3 ) {
  22. face.centroid.addSelf( this.vertices[ face.a ].position );
  23. face.centroid.addSelf( this.vertices[ face.b ].position );
  24. face.centroid.addSelf( this.vertices[ face.c ].position );
  25. face.centroid.divideScalar( 3 );
  26. } else if ( face instanceof THREE.Face4 ) {
  27. face.centroid.addSelf( this.vertices[ face.a ].position );
  28. face.centroid.addSelf( this.vertices[ face.b ].position );
  29. face.centroid.addSelf( this.vertices[ face.c ].position );
  30. face.centroid.addSelf( this.vertices[ face.d ].position );
  31. face.centroid.divideScalar( 4 );
  32. }
  33. }
  34. },
  35. computeFaceNormals: function ( useVertexNormals ) {
  36. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  37. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  38. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  39. vertex = this.vertices[ v ];
  40. vertex.normal.set( 0, 0, 0 );
  41. }
  42. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  43. face = this.faces[ f ];
  44. if ( useVertexNormals && face.vertexNormals.length ) {
  45. cb.set( 0, 0, 0 );
  46. for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
  47. cb.addSelf( face.vertexNormals[n] );
  48. }
  49. cb.divideScalar( 3 );
  50. if ( ! cb.isZero() ) {
  51. cb.normalize();
  52. }
  53. face.normal.copy( cb );
  54. } else {
  55. vA = this.vertices[ face.a ];
  56. vB = this.vertices[ face.b ];
  57. vC = this.vertices[ face.c ];
  58. cb.sub( vC.position, vB.position );
  59. ab.sub( vA.position, vB.position );
  60. cb.crossSelf( ab );
  61. if ( !cb.isZero() ) {
  62. cb.normalize();
  63. }
  64. face.normal.copy( cb );
  65. }
  66. }
  67. },
  68. computeVertexNormals: function () {
  69. var v, vertices = [],
  70. f, fl, face;
  71. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  72. vertices[ v ] = new THREE.Vector3();
  73. }
  74. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  75. face = this.faces[ f ];
  76. if ( face instanceof THREE.Face3 ) {
  77. vertices[ face.a ].addSelf( face.normal );
  78. vertices[ face.b ].addSelf( face.normal );
  79. vertices[ face.c ].addSelf( face.normal );
  80. } else if ( face instanceof THREE.Face4 ) {
  81. vertices[ face.a ].addSelf( face.normal );
  82. vertices[ face.b ].addSelf( face.normal );
  83. vertices[ face.c ].addSelf( face.normal );
  84. vertices[ face.d ].addSelf( face.normal );
  85. }
  86. }
  87. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  88. vertices[ v ].normalize();
  89. }
  90. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  91. face = this.faces[ f ];
  92. if ( face instanceof THREE.Face3 ) {
  93. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  94. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  95. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  96. } else if ( face instanceof THREE.Face4 ) {
  97. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  98. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  99. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  100. face.vertexNormals[ 3 ] = vertices[ face.d ].clone();
  101. }
  102. }
  103. },
  104. computeTangents: function() {
  105. // based on http://www.terathon.com/code/tangent.html
  106. // tangents go to vertices
  107. var f, fl, v, vl, face, uv, vA, vB, vC, uvA, uvB, uvC,
  108. x1, x2, y1, y2, z1, z2,
  109. s1, s2, t1, t2, r, t, n,
  110. tan1 = [], tan2 = [],
  111. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  112. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  113. n = new THREE.Vector3(), w;
  114. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  115. tan1[ v ] = new THREE.Vector3();
  116. tan2[ v ] = new THREE.Vector3();
  117. }
  118. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  119. vA = context.vertices[ a ].position;
  120. vB = context.vertices[ b ].position;
  121. vC = context.vertices[ c ].position;
  122. uvA = uv[ ua ];
  123. uvB = uv[ ub ];
  124. uvC = uv[ uc ];
  125. x1 = vB.x - vA.x;
  126. x2 = vC.x - vA.x;
  127. y1 = vB.y - vA.y;
  128. y2 = vC.y - vA.y;
  129. z1 = vB.z - vA.z;
  130. z2 = vC.z - vA.z;
  131. s1 = uvB.u - uvA.u;
  132. s2 = uvC.u - uvA.u;
  133. t1 = uvB.v - uvA.v;
  134. t2 = uvC.v - uvA.v;
  135. r = 1.0 / ( s1 * t2 - s2 * t1 );
  136. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  137. ( t2 * y1 - t1 * y2 ) * r,
  138. ( t2 * z1 - t1 * z2 ) * r );
  139. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  140. ( s1 * y2 - s2 * y1 ) * r,
  141. ( s1 * z2 - s2 * z1 ) * r );
  142. tan1[ a ].addSelf( sdir );
  143. tan1[ b ].addSelf( sdir );
  144. tan1[ c ].addSelf( sdir );
  145. tan2[ a ].addSelf( tdir );
  146. tan2[ b ].addSelf( tdir );
  147. tan2[ c ].addSelf( tdir );
  148. }
  149. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  150. face = this.faces[ f ];
  151. uv = this.uvs[ f ];
  152. if ( face instanceof THREE.Face3 ) {
  153. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  154. this.vertices[ face.a ].normal.copy( face.vertexNormals[ 0 ] );
  155. this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
  156. this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
  157. } else if ( face instanceof THREE.Face4 ) {
  158. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  159. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  160. this.vertices[ face.a ].normal.copy( face.vertexNormals[ 0 ] );
  161. this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
  162. this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
  163. this.vertices[ face.d ].normal.copy( face.vertexNormals[ 3 ] );
  164. }
  165. }
  166. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  167. n.copy( this.vertices[ v ].normal );
  168. t = tan1[ v ];
  169. // Gram-Schmidt orthogonalize
  170. tmp.copy( t );
  171. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  172. // Calculate handedness
  173. tmp2.cross( this.vertices[ v ].normal, t );
  174. test = tmp2.dot( tan2[ v ] );
  175. w = (test < 0.0) ? -1.0 : 1.0;
  176. this.vertices[ v ].tangent.set( tmp.x, tmp.y, tmp.z, w );
  177. }
  178. this.hasTangents = true;
  179. },
  180. computeBoundingBox: function () {
  181. var vertex;
  182. if ( this.vertices.length > 0 ) {
  183. this.boundingBox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
  184. 'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
  185. 'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };
  186. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  187. vertex = this.vertices[ v ];
  188. if ( vertex.position.x < this.boundingBox.x[ 0 ] ) {
  189. this.boundingBox.x[ 0 ] = vertex.position.x;
  190. } else if ( vertex.position.x > this.boundingBox.x[ 1 ] ) {
  191. this.boundingBox.x[ 1 ] = vertex.position.x;
  192. }
  193. if ( vertex.position.y < this.boundingBox.y[ 0 ] ) {
  194. this.boundingBox.y[ 0 ] = vertex.position.y;
  195. } else if ( vertex.position.y > this.boundingBox.y[ 1 ] ) {
  196. this.boundingBox.y[ 1 ] = vertex.position.y;
  197. }
  198. if ( vertex.position.z < this.boundingBox.z[ 0 ] ) {
  199. this.boundingBox.z[ 0 ] = vertex.position.z;
  200. } else if ( vertex.position.z > this.boundingBox.z[ 1 ] ) {
  201. this.boundingBox.z[ 1 ] = vertex.position.z;
  202. }
  203. }
  204. }
  205. },
  206. computeBoundingSphere: function () {
  207. var radius = this.boundingSphere === null ? 0 : this.boundingSphere.radius;
  208. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  209. radius = Math.max( radius, this.vertices[ v ].position.length() );
  210. }
  211. this.boundingSphere = { radius: radius };
  212. },
  213. sortFacesByMaterial: function () {
  214. // TODO
  215. // Should optimize by grouping faces with ColorFill / ColorStroke materials
  216. // which could then use vertex color attributes instead of each being
  217. // in its separate VBO
  218. var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
  219. function materialHash( material ) {
  220. var hash_array = [];
  221. for ( i = 0, l = material.length; i < l; i++ ) {
  222. if ( material[ i ] == undefined ) {
  223. hash_array.push( "undefined" );
  224. } else {
  225. hash_array.push( material[ i ].toString() );
  226. }
  227. }
  228. return hash_array.join( '_' );
  229. }
  230. for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
  231. face = this.faces[ f ];
  232. materials = face.materials;
  233. mhash = materialHash( materials );
  234. if ( hash_map[ mhash ] == undefined ) {
  235. hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
  236. }
  237. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  238. if ( this.geometryChunks[ ghash ] == undefined ) {
  239. this.geometryChunks[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0 };
  240. }
  241. vertices = face instanceof THREE.Face3 ? 3 : 4;
  242. if ( this.geometryChunks[ ghash ].vertices + vertices > 65535 ) {
  243. hash_map[ mhash ].counter += 1;
  244. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  245. if ( this.geometryChunks[ ghash ] == undefined ) {
  246. this.geometryChunks[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0 };
  247. }
  248. }
  249. this.geometryChunks[ ghash ].faces.push( f );
  250. this.geometryChunks[ ghash ].vertices += vertices;
  251. }
  252. },
  253. toString: function () {
  254. return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ', uvs: ' + this.uvs + ' )';
  255. }
  256. };
粤ICP备19079148号