Geometry.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author mikael emtinger / http://gomo.se/
  6. */
  7. THREE.Geometry = function () {
  8. this.id = "Geometry" + THREE.GeometryIdCounter ++;
  9. this.vertices = [];
  10. this.faces = [];
  11. this.uvs = [];
  12. this.uvs2 = [];
  13. this.colors = [];
  14. this.skinWeights = [];
  15. this.skinIndices = [];
  16. this.boundingBox = null;
  17. this.boundingSphere = null;
  18. this.hasTangents = false;
  19. };
  20. THREE.Geometry.prototype = {
  21. computeCentroids: function () {
  22. var f, fl, face;
  23. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  24. face = this.faces[ f ];
  25. face.centroid.set( 0, 0, 0 );
  26. if ( face instanceof THREE.Face3 ) {
  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.divideScalar( 3 );
  31. } else if ( face instanceof THREE.Face4 ) {
  32. face.centroid.addSelf( this.vertices[ face.a ].position );
  33. face.centroid.addSelf( this.vertices[ face.b ].position );
  34. face.centroid.addSelf( this.vertices[ face.c ].position );
  35. face.centroid.addSelf( this.vertices[ face.d ].position );
  36. face.centroid.divideScalar( 4 );
  37. }
  38. }
  39. },
  40. computeFaceNormals: function ( useVertexNormals ) {
  41. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  42. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  43. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  44. vertex = this.vertices[ v ];
  45. vertex.normal.set( 0, 0, 0 );
  46. }
  47. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  48. face = this.faces[ f ];
  49. if ( useVertexNormals && face.vertexNormals.length ) {
  50. cb.set( 0, 0, 0 );
  51. for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
  52. cb.addSelf( face.vertexNormals[n] );
  53. }
  54. cb.divideScalar( 3 );
  55. if ( ! cb.isZero() ) {
  56. cb.normalize();
  57. }
  58. face.normal.copy( cb );
  59. } else {
  60. vA = this.vertices[ face.a ];
  61. vB = this.vertices[ face.b ];
  62. vC = this.vertices[ face.c ];
  63. cb.sub( vC.position, vB.position );
  64. ab.sub( vA.position, vB.position );
  65. cb.crossSelf( ab );
  66. if ( !cb.isZero() ) {
  67. cb.normalize();
  68. }
  69. face.normal.copy( cb );
  70. }
  71. }
  72. },
  73. computeVertexNormals: function () {
  74. var v, vl, f, fl, face, vertices;
  75. // create internal buffers for reuse when calling this method repeatedly
  76. // (otherwise memory allocation / deallocation every frame is big resource hog)
  77. if ( this.__tmpVertices == undefined ) {
  78. this.__tmpVertices = new Array( this.vertices.length );
  79. vertices = this.__tmpVertices;
  80. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  81. vertices[ v ] = new THREE.Vector3();
  82. }
  83. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  84. face = this.faces[ f ];
  85. if ( face instanceof THREE.Face3 ) {
  86. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  87. } else if ( face instanceof THREE.Face4 ) {
  88. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  89. }
  90. }
  91. } else {
  92. vertices = this.__tmpVertices;
  93. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  94. vertices[ v ].set( 0, 0, 0 );
  95. }
  96. }
  97. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  98. face = this.faces[ f ];
  99. if ( face instanceof THREE.Face3 ) {
  100. vertices[ face.a ].addSelf( face.normal );
  101. vertices[ face.b ].addSelf( face.normal );
  102. vertices[ face.c ].addSelf( face.normal );
  103. } else if ( face instanceof THREE.Face4 ) {
  104. vertices[ face.a ].addSelf( face.normal );
  105. vertices[ face.b ].addSelf( face.normal );
  106. vertices[ face.c ].addSelf( face.normal );
  107. vertices[ face.d ].addSelf( face.normal );
  108. }
  109. }
  110. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  111. vertices[ v ].normalize();
  112. }
  113. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  114. face = this.faces[ f ];
  115. if ( face instanceof THREE.Face3 ) {
  116. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  117. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  118. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  119. } else if ( face instanceof THREE.Face4 ) {
  120. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  121. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  122. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  123. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  124. }
  125. }
  126. },
  127. computeTangents: function () {
  128. // based on http://www.terathon.com/code/tangent.html
  129. // tangents go to vertices
  130. var f, fl, v, vl, face, uv, vA, vB, vC, uvA, uvB, uvC,
  131. x1, x2, y1, y2, z1, z2,
  132. s1, s2, t1, t2, r, t, test,
  133. tan1 = [], tan2 = [],
  134. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  135. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  136. n = new THREE.Vector3(), w;
  137. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  138. tan1[ v ] = new THREE.Vector3();
  139. tan2[ v ] = new THREE.Vector3();
  140. }
  141. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  142. vA = context.vertices[ a ].position;
  143. vB = context.vertices[ b ].position;
  144. vC = context.vertices[ c ].position;
  145. uvA = uv[ ua ];
  146. uvB = uv[ ub ];
  147. uvC = uv[ uc ];
  148. x1 = vB.x - vA.x;
  149. x2 = vC.x - vA.x;
  150. y1 = vB.y - vA.y;
  151. y2 = vC.y - vA.y;
  152. z1 = vB.z - vA.z;
  153. z2 = vC.z - vA.z;
  154. s1 = uvB.u - uvA.u;
  155. s2 = uvC.u - uvA.u;
  156. t1 = uvB.v - uvA.v;
  157. t2 = uvC.v - uvA.v;
  158. r = 1.0 / ( s1 * t2 - s2 * t1 );
  159. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  160. ( t2 * y1 - t1 * y2 ) * r,
  161. ( t2 * z1 - t1 * z2 ) * r );
  162. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  163. ( s1 * y2 - s2 * y1 ) * r,
  164. ( s1 * z2 - s2 * z1 ) * r );
  165. tan1[ a ].addSelf( sdir );
  166. tan1[ b ].addSelf( sdir );
  167. tan1[ c ].addSelf( sdir );
  168. tan2[ a ].addSelf( tdir );
  169. tan2[ b ].addSelf( tdir );
  170. tan2[ c ].addSelf( tdir );
  171. }
  172. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  173. face = this.faces[ f ];
  174. uv = this.uvs[ f ];
  175. if ( face instanceof THREE.Face3 ) {
  176. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  177. this.vertices[ face.a ].normal.copy( face.vertexNormals[ 0 ] );
  178. this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
  179. this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
  180. } else if ( face instanceof THREE.Face4 ) {
  181. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  182. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  183. this.vertices[ face.a ].normal.copy( face.vertexNormals[ 0 ] );
  184. this.vertices[ face.b ].normal.copy( face.vertexNormals[ 1 ] );
  185. this.vertices[ face.c ].normal.copy( face.vertexNormals[ 2 ] );
  186. this.vertices[ face.d ].normal.copy( face.vertexNormals[ 3 ] );
  187. }
  188. }
  189. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  190. n.copy( this.vertices[ v ].normal );
  191. t = tan1[ v ];
  192. // Gram-Schmidt orthogonalize
  193. tmp.copy( t );
  194. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  195. // Calculate handedness
  196. tmp2.cross( this.vertices[ v ].normal, t );
  197. test = tmp2.dot( tan2[ v ] );
  198. w = (test < 0.0) ? -1.0 : 1.0;
  199. this.vertices[ v ].tangent.set( tmp.x, tmp.y, tmp.z, w );
  200. }
  201. this.hasTangents = true;
  202. },
  203. computeBoundingBox: function () {
  204. var vertex;
  205. if ( this.vertices.length > 0 ) {
  206. this.boundingBox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
  207. 'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
  208. 'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };
  209. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  210. vertex = this.vertices[ v ];
  211. if ( vertex.position.x < this.boundingBox.x[ 0 ] ) {
  212. this.boundingBox.x[ 0 ] = vertex.position.x;
  213. } else if ( vertex.position.x > this.boundingBox.x[ 1 ] ) {
  214. this.boundingBox.x[ 1 ] = vertex.position.x;
  215. }
  216. if ( vertex.position.y < this.boundingBox.y[ 0 ] ) {
  217. this.boundingBox.y[ 0 ] = vertex.position.y;
  218. } else if ( vertex.position.y > this.boundingBox.y[ 1 ] ) {
  219. this.boundingBox.y[ 1 ] = vertex.position.y;
  220. }
  221. if ( vertex.position.z < this.boundingBox.z[ 0 ] ) {
  222. this.boundingBox.z[ 0 ] = vertex.position.z;
  223. } else if ( vertex.position.z > this.boundingBox.z[ 1 ] ) {
  224. this.boundingBox.z[ 1 ] = vertex.position.z;
  225. }
  226. }
  227. }
  228. },
  229. computeBoundingSphere: function () {
  230. var radius = this.boundingSphere === null ? 0 : this.boundingSphere.radius;
  231. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  232. radius = Math.max( radius, this.vertices[ v ].position.length() );
  233. }
  234. this.boundingSphere = { radius: radius };
  235. }
  236. };
  237. THREE.GeometryIdCounter = 0;