Geometry.js 10 KB

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