Geometry.js 16 KB

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