Geometry.js 10 KB

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