Geometry.js 14 KB

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