Geometry.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. * @author zz85 / http://www.lab4games.net/zz85/blog
  7. */
  8. THREE.Geometry = function () {
  9. this.id = THREE.GeometryCount ++;
  10. this.vertices = [];
  11. this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
  12. this.materials = [];
  13. this.faces = [];
  14. this.faceUvs = [[]];
  15. this.faceVertexUvs = [[]];
  16. this.morphTargets = [];
  17. this.morphColors = [];
  18. this.morphNormals = [];
  19. this.skinWeights = [];
  20. this.skinIndices = [];
  21. this.boundingBox = null;
  22. this.boundingSphere = null;
  23. this.hasTangents = false;
  24. this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
  25. };
  26. THREE.Geometry.prototype = {
  27. constructor : THREE.Geometry,
  28. applyMatrix: function ( matrix ) {
  29. var matrixRotation = new THREE.Matrix4();
  30. matrixRotation.extractRotation( matrix );
  31. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  32. var vertex = this.vertices[ i ];
  33. matrix.multiplyVector3( vertex );
  34. }
  35. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  36. var face = this.faces[ i ];
  37. matrixRotation.multiplyVector3( face.normal );
  38. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  39. matrixRotation.multiplyVector3( face.vertexNormals[ j ] );
  40. }
  41. matrix.multiplyVector3( face.centroid );
  42. }
  43. },
  44. computeCentroids: function () {
  45. var f, fl, face;
  46. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  47. face = this.faces[ f ];
  48. face.centroid.set( 0, 0, 0 );
  49. if ( face instanceof THREE.Face3 ) {
  50. face.centroid.addSelf( this.vertices[ face.a ] );
  51. face.centroid.addSelf( this.vertices[ face.b ] );
  52. face.centroid.addSelf( this.vertices[ face.c ] );
  53. face.centroid.divideScalar( 3 );
  54. } else if ( face instanceof THREE.Face4 ) {
  55. face.centroid.addSelf( this.vertices[ face.a ] );
  56. face.centroid.addSelf( this.vertices[ face.b ] );
  57. face.centroid.addSelf( this.vertices[ face.c ] );
  58. face.centroid.addSelf( this.vertices[ face.d ] );
  59. face.centroid.divideScalar( 4 );
  60. }
  61. }
  62. },
  63. computeFaceNormals: function () {
  64. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  65. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  66. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  67. face = this.faces[ f ];
  68. vA = this.vertices[ face.a ];
  69. vB = this.vertices[ face.b ];
  70. vC = this.vertices[ face.c ];
  71. cb.sub( vC, vB );
  72. ab.sub( vA, vB );
  73. cb.crossSelf( ab );
  74. if ( !cb.isZero() ) {
  75. cb.normalize();
  76. }
  77. face.normal.copy( cb );
  78. }
  79. },
  80. computeVertexNormals: function () {
  81. var v, vl, f, fl, face, vertices;
  82. // create internal buffers for reuse when calling this method repeatedly
  83. // (otherwise memory allocation / deallocation every frame is big resource hog)
  84. if ( this.__tmpVertices === undefined ) {
  85. this.__tmpVertices = new Array( this.vertices.length );
  86. vertices = this.__tmpVertices;
  87. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  88. vertices[ v ] = new THREE.Vector3();
  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 = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  94. } else if ( face instanceof THREE.Face4 ) {
  95. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  96. }
  97. }
  98. } else {
  99. vertices = this.__tmpVertices;
  100. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  101. vertices[ v ].set( 0, 0, 0 );
  102. }
  103. }
  104. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  105. face = this.faces[ f ];
  106. if ( face instanceof THREE.Face3 ) {
  107. vertices[ face.a ].addSelf( face.normal );
  108. vertices[ face.b ].addSelf( face.normal );
  109. vertices[ face.c ].addSelf( face.normal );
  110. } else if ( face instanceof THREE.Face4 ) {
  111. vertices[ face.a ].addSelf( face.normal );
  112. vertices[ face.b ].addSelf( face.normal );
  113. vertices[ face.c ].addSelf( face.normal );
  114. vertices[ face.d ].addSelf( face.normal );
  115. }
  116. }
  117. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  118. vertices[ v ].normalize();
  119. }
  120. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  121. face = this.faces[ f ];
  122. if ( face instanceof THREE.Face3 ) {
  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. } else if ( face instanceof THREE.Face4 ) {
  127. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  128. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  129. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  130. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  131. }
  132. }
  133. },
  134. computeMorphNormals: function () {
  135. var i, il, f, fl, face;
  136. // save original normals
  137. // - create temp variables on first access
  138. // otherwise just copy (for faster repeated calls)
  139. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  140. face = this.faces[ f ];
  141. if ( ! face.__originalFaceNormal ) {
  142. face.__originalFaceNormal = face.normal.clone();
  143. } else {
  144. face.__originalFaceNormal.copy( face.normal );
  145. }
  146. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  147. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  148. if ( ! face.__originalVertexNormals[ i ] ) {
  149. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  150. } else {
  151. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  152. }
  153. }
  154. }
  155. // use temp geometry to compute face and vertex normals for each morph
  156. var tmpGeo = new THREE.Geometry();
  157. tmpGeo.faces = this.faces;
  158. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  159. // create on first access
  160. if ( ! this.morphNormals[ i ] ) {
  161. this.morphNormals[ i ] = {};
  162. this.morphNormals[ i ].faceNormals = [];
  163. this.morphNormals[ i ].vertexNormals = [];
  164. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  165. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  166. var faceNormal, vertexNormals;
  167. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  168. face = this.faces[ f ];
  169. faceNormal = new THREE.Vector3();
  170. if ( face instanceof THREE.Face3 ) {
  171. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  172. } else {
  173. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3(), d: new THREE.Vector3() };
  174. }
  175. dstNormalsFace.push( faceNormal );
  176. dstNormalsVertex.push( vertexNormals );
  177. }
  178. }
  179. var morphNormals = this.morphNormals[ i ];
  180. // set vertices to morph target
  181. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  182. // compute morph normals
  183. tmpGeo.computeFaceNormals();
  184. tmpGeo.computeVertexNormals();
  185. // store morph normals
  186. var faceNormal, vertexNormals;
  187. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  188. face = this.faces[ f ];
  189. faceNormal = morphNormals.faceNormals[ f ];
  190. vertexNormals = morphNormals.vertexNormals[ f ];
  191. faceNormal.copy( face.normal );
  192. if ( face instanceof THREE.Face3 ) {
  193. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  194. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  195. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  196. } else {
  197. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  198. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  199. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  200. vertexNormals.d.copy( face.vertexNormals[ 3 ] );
  201. }
  202. }
  203. }
  204. // restore original normals
  205. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  206. face = this.faces[ f ];
  207. face.normal = face.__originalFaceNormal;
  208. face.vertexNormals = face.__originalVertexNormals;
  209. }
  210. },
  211. computeTangents: function () {
  212. // based on http://www.terathon.com/code/tangent.html
  213. // tangents go to vertices
  214. var f, fl, v, vl, i, il, vertexIndex,
  215. face, uv, vA, vB, vC, uvA, uvB, uvC,
  216. x1, x2, y1, y2, z1, z2,
  217. s1, s2, t1, t2, r, t, test,
  218. tan1 = [], tan2 = [],
  219. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  220. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  221. n = new THREE.Vector3(), w;
  222. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  223. tan1[ v ] = new THREE.Vector3();
  224. tan2[ v ] = new THREE.Vector3();
  225. }
  226. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  227. vA = context.vertices[ a ];
  228. vB = context.vertices[ b ];
  229. vC = context.vertices[ c ];
  230. uvA = uv[ ua ];
  231. uvB = uv[ ub ];
  232. uvC = uv[ uc ];
  233. x1 = vB.x - vA.x;
  234. x2 = vC.x - vA.x;
  235. y1 = vB.y - vA.y;
  236. y2 = vC.y - vA.y;
  237. z1 = vB.z - vA.z;
  238. z2 = vC.z - vA.z;
  239. s1 = uvB.u - uvA.u;
  240. s2 = uvC.u - uvA.u;
  241. t1 = uvB.v - uvA.v;
  242. t2 = uvC.v - uvA.v;
  243. r = 1.0 / ( s1 * t2 - s2 * t1 );
  244. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  245. ( t2 * y1 - t1 * y2 ) * r,
  246. ( t2 * z1 - t1 * z2 ) * r );
  247. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  248. ( s1 * y2 - s2 * y1 ) * r,
  249. ( s1 * z2 - s2 * z1 ) * r );
  250. tan1[ a ].addSelf( sdir );
  251. tan1[ b ].addSelf( sdir );
  252. tan1[ c ].addSelf( sdir );
  253. tan2[ a ].addSelf( tdir );
  254. tan2[ b ].addSelf( tdir );
  255. tan2[ c ].addSelf( tdir );
  256. }
  257. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  258. face = this.faces[ f ];
  259. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  260. if ( face instanceof THREE.Face3 ) {
  261. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  262. } else if ( face instanceof THREE.Face4 ) {
  263. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  264. handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
  265. }
  266. }
  267. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  268. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  269. face = this.faces[ f ];
  270. for ( i = 0; i < face.vertexNormals.length; i++ ) {
  271. n.copy( face.vertexNormals[ i ] );
  272. vertexIndex = face[ faceIndex[ i ] ];
  273. t = tan1[ vertexIndex ];
  274. // Gram-Schmidt orthogonalize
  275. tmp.copy( t );
  276. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  277. // Calculate handedness
  278. tmp2.cross( face.vertexNormals[ i ], t );
  279. test = tmp2.dot( tan2[ vertexIndex ] );
  280. w = (test < 0.0) ? -1.0 : 1.0;
  281. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  282. }
  283. }
  284. this.hasTangents = true;
  285. },
  286. computeBoundingBox: function () {
  287. if ( ! this.boundingBox ) {
  288. this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
  289. }
  290. if ( this.vertices.length > 0 ) {
  291. var position, firstPosition = this.vertices[ 0 ];
  292. this.boundingBox.min.copy( firstPosition );
  293. this.boundingBox.max.copy( firstPosition );
  294. var min = this.boundingBox.min,
  295. max = this.boundingBox.max;
  296. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  297. position = this.vertices[ v ];
  298. if ( position.x < min.x ) {
  299. min.x = position.x;
  300. } else if ( position.x > max.x ) {
  301. max.x = position.x;
  302. }
  303. if ( position.y < min.y ) {
  304. min.y = position.y;
  305. } else if ( position.y > max.y ) {
  306. max.y = position.y;
  307. }
  308. if ( position.z < min.z ) {
  309. min.z = position.z;
  310. } else if ( position.z > max.z ) {
  311. max.z = position.z;
  312. }
  313. }
  314. } else {
  315. this.boundingBox.min.set( 0, 0, 0 );
  316. this.boundingBox.max.set( 0, 0, 0 );
  317. }
  318. },
  319. computeBoundingSphere: function () {
  320. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  321. var radius, maxRadius = 0;
  322. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  323. radius = this.vertices[ v ].length();
  324. if ( radius > maxRadius ) maxRadius = radius;
  325. }
  326. this.boundingSphere.radius = maxRadius;
  327. },
  328. /*
  329. * Checks for duplicate vertices with hashmap.
  330. * Duplicated vertices are removed
  331. * and faces' vertices are updated.
  332. */
  333. mergeVertices: function() {
  334. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  335. var unique = [], changes = [];
  336. var v, key;
  337. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  338. var precision = Math.pow( 10, precisionPoints );
  339. var i,il, face;
  340. var abcd = 'abcd', o, k, j, jl, u;
  341. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  342. v = this.vertices[ i ];
  343. key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );
  344. if ( verticesMap[ key ] === undefined ) {
  345. verticesMap[ key ] = i;
  346. unique.push( this.vertices[ i ] );
  347. changes[ i ] = unique.length - 1;
  348. } else {
  349. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  350. changes[ i ] = changes[ verticesMap[ key ] ];
  351. }
  352. };
  353. // Start to patch face indices
  354. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  355. face = this.faces[ i ];
  356. if ( face instanceof THREE.Face3 ) {
  357. face.a = changes[ face.a ];
  358. face.b = changes[ face.b ];
  359. face.c = changes[ face.c ];
  360. } else if ( face instanceof THREE.Face4 ) {
  361. face.a = changes[ face.a ];
  362. face.b = changes[ face.b ];
  363. face.c = changes[ face.c ];
  364. face.d = changes[ face.d ];
  365. // check dups in (a, b, c, d) and convert to -> face3
  366. o = [face.a, face.b, face.c, face.d];
  367. for (k=3;k>0;k--) {
  368. if ( o.indexOf(face[abcd[k]]) != k ) {
  369. // console.log('faces', face.a, face.b, face.c, face.d, 'dup at', k);
  370. o.splice(k, 1);
  371. this.faces[ i ] = new THREE.Face3(o[0], o[1], o[2]);
  372. for (j=0,jl=this.faceVertexUvs.length;j<jl;j++) {
  373. u = this.faceVertexUvs[j][i];
  374. if (u) u.splice(k, 1);
  375. }
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. // Use unique set of vertices
  382. var diff = this.vertices.length - unique.length;
  383. this.vertices = unique;
  384. return diff;
  385. }
  386. };
  387. THREE.GeometryCount = 0;