Geometry.js 14 KB

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