Geometry.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. * @author bhouston / http://exocortex.com
  8. */
  9. THREE.Geometry = function () {
  10. Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );
  11. this.uuid = THREE.Math.generateUUID();
  12. this.name = '';
  13. this.type = 'Geometry';
  14. // FlattenedGeometry
  15. this.isFlattened = false;
  16. this.vertices = [];
  17. this.colors = [];
  18. this.normals = [];
  19. this.colors = [];
  20. this.uvs = [];
  21. this.faces = [];
  22. //
  23. this.faceVertexUvs = [ [] ];
  24. this.morphTargets = [];
  25. this.morphColors = [];
  26. this.morphNormals = [];
  27. this.skinWeights = [];
  28. this.skinIndices = [];
  29. this.lineDistances = [];
  30. this.boundingBox = null;
  31. this.boundingSphere = null;
  32. this.hasTangents = false;
  33. this.dynamic = true;
  34. // update flags
  35. this.verticesNeedUpdate = false;
  36. this.elementsNeedUpdate = false;
  37. this.uvsNeedUpdate = false;
  38. this.normalsNeedUpdate = false;
  39. this.tangentsNeedUpdate = false;
  40. this.colorsNeedUpdate = false;
  41. this.lineDistancesNeedUpdate = false;
  42. this.groupsNeedUpdate = false;
  43. };
  44. THREE.Geometry.prototype = {
  45. constructor: THREE.Geometry,
  46. applyMatrix: function ( matrix ) {
  47. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  48. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  49. var vertex = this.vertices[ i ];
  50. vertex.applyMatrix4( matrix );
  51. }
  52. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  53. var face = this.faces[ i ];
  54. face.normal.applyMatrix3( normalMatrix ).normalize();
  55. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  56. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  57. }
  58. }
  59. if ( this.boundingBox !== null ) {
  60. this.computeBoundingBox();
  61. }
  62. if ( this.boundingSphere !== null ) {
  63. this.computeBoundingSphere();
  64. }
  65. this.verticesNeedUpdate = true;
  66. this.normalsNeedUpdate = true;
  67. },
  68. flatten: function () {
  69. var faces = this.faces;
  70. var faceVertexUvs = this.faceVertexUvs[ 0 ];
  71. for ( var i = 0, il = faces.length; i < il; i ++ ) {
  72. var face = faces[ i ];
  73. var indices = [ face.a, face.b, face.c ];
  74. var vertexNormals = face.vertexNormals;
  75. var vertexColors = face.vertexColors;
  76. var vertexUvs = faceVertexUvs[ i ];
  77. for ( var j = 0, jl = vertexNormals.length; j < jl; j ++ ) {
  78. this.normals[ indices[ j ] ] = vertexNormals[ j ];
  79. }
  80. for ( var j = 0, jl = vertexColors.length; j < jl; j ++ ) {
  81. this.colors[ indices[ j ] ] = vertexColors[ j ];
  82. }
  83. for ( var j = 0, jl = vertexUvs.length; j < jl; j ++ ) {
  84. this.uvs[ indices[ j ] ] = vertexUvs[ j ];
  85. }
  86. }
  87. this.isFlattened = true;
  88. },
  89. fromBufferGeometry: function ( geometry ) {
  90. var scope = this;
  91. var attributes = geometry.attributes;
  92. var vertices = attributes.position.array;
  93. var indices = attributes.index !== undefined ? attributes.index.array : undefined;
  94. var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
  95. var colors = attributes.color !== undefined ? attributes.color.array : undefined;
  96. var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
  97. var tempNormals = [];
  98. var tempUVs = [];
  99. for ( var i = 0, j = 0; i < vertices.length; i += 3, j += 2 ) {
  100. scope.vertices.push( new THREE.Vector3( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ) );
  101. if ( normals !== undefined ) {
  102. tempNormals.push( new THREE.Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );
  103. }
  104. if ( colors !== undefined ) {
  105. scope.colors.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  106. }
  107. if ( uvs !== undefined ) {
  108. tempUVs.push( new THREE.Vector2( uvs[ j ], uvs[ j + 1 ] ) );
  109. }
  110. }
  111. var addFace = function ( a, b, c ) {
  112. var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
  113. var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
  114. scope.faces.push( new THREE.Face3( a, b, c, vertexNormals, vertexColors ) );
  115. if ( uvs !== undefined ) {
  116. scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
  117. }
  118. };
  119. if ( indices !== undefined ) {
  120. var drawcalls = geometry.drawcalls;
  121. if ( drawcalls.length > 0 ) {
  122. for ( var i = 0; i < drawcalls.length; i ++ ) {
  123. var drawcall = drawcalls[ i ];
  124. var start = drawcall.start;
  125. var count = drawcall.count;
  126. var index = drawcall.index;
  127. for ( var j = start, jl = start + count; j < jl; j += 3 ) {
  128. addFace( index + indices[ j ], index + indices[ j + 1 ], index + indices[ j + 2 ] );
  129. }
  130. }
  131. } else {
  132. for ( var i = 0; i < indices.length; i += 3 ) {
  133. addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
  134. }
  135. }
  136. } else {
  137. for ( var i = 0; i < vertices.length / 3; i += 3 ) {
  138. addFace( i, i + 1, i + 2 );
  139. }
  140. }
  141. this.computeFaceNormals();
  142. if ( geometry.boundingBox !== null ) {
  143. this.boundingBox = geometry.boundingBox.clone();
  144. }
  145. if ( geometry.boundingSphere !== null ) {
  146. this.boundingSphere = geometry.boundingSphere.clone();
  147. }
  148. return this;
  149. },
  150. center: function () {
  151. this.computeBoundingBox();
  152. var offset = this.boundingBox.center().negate();
  153. this.applyMatrix( new THREE.Matrix4().setPosition( offset ) );
  154. return offset;
  155. },
  156. computeFaceNormals: function () {
  157. var cb = new THREE.Vector3(), ab = new THREE.Vector3();
  158. for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
  159. var face = this.faces[ f ];
  160. var vA = this.vertices[ face.a ];
  161. var vB = this.vertices[ face.b ];
  162. var vC = this.vertices[ face.c ];
  163. cb.subVectors( vC, vB );
  164. ab.subVectors( vA, vB );
  165. cb.cross( ab );
  166. cb.normalize();
  167. face.normal.copy( cb );
  168. }
  169. },
  170. computeVertexNormals: function ( areaWeighted ) {
  171. var v, vl, f, fl, face, vertices;
  172. vertices = new Array( this.vertices.length );
  173. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  174. vertices[ v ] = new THREE.Vector3();
  175. }
  176. if ( areaWeighted ) {
  177. // vertex normals weighted by triangle areas
  178. // http://www.iquilezles.org/www/articles/normals/normals.htm
  179. var vA, vB, vC;
  180. var cb = new THREE.Vector3(), ab = new THREE.Vector3();
  181. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  182. face = this.faces[ f ];
  183. vA = this.vertices[ face.a ];
  184. vB = this.vertices[ face.b ];
  185. vC = this.vertices[ face.c ];
  186. cb.subVectors( vC, vB );
  187. ab.subVectors( vA, vB );
  188. cb.cross( ab );
  189. vertices[ face.a ].add( cb );
  190. vertices[ face.b ].add( cb );
  191. vertices[ face.c ].add( cb );
  192. }
  193. } else {
  194. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  195. face = this.faces[ f ];
  196. vertices[ face.a ].add( face.normal );
  197. vertices[ face.b ].add( face.normal );
  198. vertices[ face.c ].add( face.normal );
  199. }
  200. }
  201. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  202. vertices[ v ].normalize();
  203. }
  204. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  205. face = this.faces[ f ];
  206. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  207. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  208. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  209. }
  210. },
  211. computeMorphNormals: function () {
  212. var i, il, f, fl, face;
  213. // save original normals
  214. // - create temp variables on first access
  215. // otherwise just copy (for faster repeated calls)
  216. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  217. face = this.faces[ f ];
  218. if ( ! face.__originalFaceNormal ) {
  219. face.__originalFaceNormal = face.normal.clone();
  220. } else {
  221. face.__originalFaceNormal.copy( face.normal );
  222. }
  223. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  224. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  225. if ( ! face.__originalVertexNormals[ i ] ) {
  226. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  227. } else {
  228. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  229. }
  230. }
  231. }
  232. // use temp geometry to compute face and vertex normals for each morph
  233. var tmpGeo = new THREE.Geometry();
  234. tmpGeo.faces = this.faces;
  235. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  236. // create on first access
  237. if ( ! this.morphNormals[ i ] ) {
  238. this.morphNormals[ i ] = {};
  239. this.morphNormals[ i ].faceNormals = [];
  240. this.morphNormals[ i ].vertexNormals = [];
  241. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  242. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  243. var faceNormal, vertexNormals;
  244. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  245. faceNormal = new THREE.Vector3();
  246. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  247. dstNormalsFace.push( faceNormal );
  248. dstNormalsVertex.push( vertexNormals );
  249. }
  250. }
  251. var morphNormals = this.morphNormals[ i ];
  252. // set vertices to morph target
  253. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  254. // compute morph normals
  255. tmpGeo.computeFaceNormals();
  256. tmpGeo.computeVertexNormals();
  257. // store morph normals
  258. var faceNormal, vertexNormals;
  259. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  260. face = this.faces[ f ];
  261. faceNormal = morphNormals.faceNormals[ f ];
  262. vertexNormals = morphNormals.vertexNormals[ f ];
  263. faceNormal.copy( face.normal );
  264. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  265. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  266. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  267. }
  268. }
  269. // restore original normals
  270. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  271. face = this.faces[ f ];
  272. face.normal = face.__originalFaceNormal;
  273. face.vertexNormals = face.__originalVertexNormals;
  274. }
  275. },
  276. computeTangents: function () {
  277. // based on http://www.terathon.com/code/tangent.html
  278. // tangents go to vertices
  279. var f, fl, v, vl, i, vertexIndex,
  280. face, uv, vA, vB, vC, uvA, uvB, uvC,
  281. x1, x2, y1, y2, z1, z2,
  282. s1, s2, t1, t2, r, t, test,
  283. tan1 = [], tan2 = [],
  284. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  285. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  286. n = new THREE.Vector3(), w;
  287. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  288. tan1[ v ] = new THREE.Vector3();
  289. tan2[ v ] = new THREE.Vector3();
  290. }
  291. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  292. vA = context.vertices[ a ];
  293. vB = context.vertices[ b ];
  294. vC = context.vertices[ c ];
  295. uvA = uv[ ua ];
  296. uvB = uv[ ub ];
  297. uvC = uv[ uc ];
  298. x1 = vB.x - vA.x;
  299. x2 = vC.x - vA.x;
  300. y1 = vB.y - vA.y;
  301. y2 = vC.y - vA.y;
  302. z1 = vB.z - vA.z;
  303. z2 = vC.z - vA.z;
  304. s1 = uvB.x - uvA.x;
  305. s2 = uvC.x - uvA.x;
  306. t1 = uvB.y - uvA.y;
  307. t2 = uvC.y - uvA.y;
  308. r = 1.0 / ( s1 * t2 - s2 * t1 );
  309. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  310. ( t2 * y1 - t1 * y2 ) * r,
  311. ( t2 * z1 - t1 * z2 ) * r );
  312. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  313. ( s1 * y2 - s2 * y1 ) * r,
  314. ( s1 * z2 - s2 * z1 ) * r );
  315. tan1[ a ].add( sdir );
  316. tan1[ b ].add( sdir );
  317. tan1[ c ].add( sdir );
  318. tan2[ a ].add( tdir );
  319. tan2[ b ].add( tdir );
  320. tan2[ c ].add( tdir );
  321. }
  322. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  323. face = this.faces[ f ];
  324. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  325. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  326. }
  327. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  328. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  329. face = this.faces[ f ];
  330. for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i ++ ) {
  331. n.copy( face.vertexNormals[ i ] );
  332. vertexIndex = face[ faceIndex[ i ] ];
  333. t = tan1[ vertexIndex ];
  334. // Gram-Schmidt orthogonalize
  335. tmp.copy( t );
  336. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  337. // Calculate handedness
  338. tmp2.crossVectors( face.vertexNormals[ i ], t );
  339. test = tmp2.dot( tan2[ vertexIndex ] );
  340. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  341. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  342. }
  343. }
  344. this.hasTangents = true;
  345. },
  346. computeLineDistances: function () {
  347. var d = 0;
  348. var vertices = this.vertices;
  349. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  350. if ( i > 0 ) {
  351. d += vertices[ i ].distanceTo( vertices[ i - 1 ] );
  352. }
  353. this.lineDistances[ i ] = d;
  354. }
  355. },
  356. computeBoundingBox: function () {
  357. if ( this.boundingBox === null ) {
  358. this.boundingBox = new THREE.Box3();
  359. }
  360. this.boundingBox.setFromPoints( this.vertices );
  361. },
  362. computeBoundingSphere: function () {
  363. if ( this.boundingSphere === null ) {
  364. this.boundingSphere = new THREE.Sphere();
  365. }
  366. this.boundingSphere.setFromPoints( this.vertices );
  367. },
  368. merge: function ( geometry, matrix ) {
  369. if ( geometry instanceof THREE.Geometry === false ) {
  370. THREE.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  371. return;
  372. }
  373. var normalMatrix,
  374. vertexOffset = this.vertices.length,
  375. vertices1 = this.vertices,
  376. vertices2 = geometry.vertices,
  377. faces1 = this.faces,
  378. faces2 = geometry.faces,
  379. uvs1 = this.faceVertexUvs[ 0 ],
  380. uvs2 = geometry.faceVertexUvs[ 0 ];
  381. if ( matrix !== undefined ) {
  382. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  383. }
  384. // vertices
  385. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  386. var vertex = vertices2[ i ];
  387. var vertexCopy = vertex.clone();
  388. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  389. vertices1.push( vertexCopy );
  390. }
  391. // faces
  392. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  393. var face = faces2[ i ], faceCopy, normal, color,
  394. faceVertexNormals = face.vertexNormals,
  395. faceVertexColors = face.vertexColors;
  396. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  397. faceCopy.normal.copy( face.normal );
  398. if ( normalMatrix !== undefined ) {
  399. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  400. }
  401. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  402. normal = faceVertexNormals[ j ].clone();
  403. if ( normalMatrix !== undefined ) {
  404. normal.applyMatrix3( normalMatrix ).normalize();
  405. }
  406. faceCopy.vertexNormals.push( normal );
  407. }
  408. faceCopy.color.copy( face.color );
  409. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  410. color = faceVertexColors[ j ];
  411. faceCopy.vertexColors.push( color.clone() );
  412. }
  413. faces1.push( faceCopy );
  414. }
  415. // uvs
  416. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  417. var uv = uvs2[ i ], uvCopy = [];
  418. if ( uv === undefined ) {
  419. continue;
  420. }
  421. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  422. uvCopy.push( uv[ j ].clone() );
  423. }
  424. uvs1.push( uvCopy );
  425. }
  426. },
  427. mergeMesh: function ( mesh ) {
  428. if ( mesh instanceof THREE.Mesh === false ) {
  429. THREE.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  430. return;
  431. }
  432. mesh.matrixAutoUpdate && mesh.updateMatrix();
  433. this.merge( mesh.geometry, mesh.matrix );
  434. },
  435. /*
  436. * Checks for duplicate vertices with hashmap.
  437. * Duplicated vertices are removed
  438. * and faces' vertices are updated.
  439. */
  440. mergeVertices: function () {
  441. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  442. var unique = [], changes = [];
  443. var v, key;
  444. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  445. var precision = Math.pow( 10, precisionPoints );
  446. var i, il, face;
  447. var indices, j, jl;
  448. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  449. v = this.vertices[ i ];
  450. key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  451. if ( verticesMap[ key ] === undefined ) {
  452. verticesMap[ key ] = i;
  453. unique.push( this.vertices[ i ] );
  454. changes[ i ] = unique.length - 1;
  455. } else {
  456. //THREE.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  457. changes[ i ] = changes[ verticesMap[ key ] ];
  458. }
  459. }
  460. // if faces are completely degenerate after merging vertices, we
  461. // have to remove them from the geometry.
  462. var faceIndicesToRemove = [];
  463. for ( i = 0, il = this.faces.length; i < il; i ++ ) {
  464. face = this.faces[ i ];
  465. face.a = changes[ face.a ];
  466. face.b = changes[ face.b ];
  467. face.c = changes[ face.c ];
  468. indices = [ face.a, face.b, face.c ];
  469. var dupIndex = - 1;
  470. // if any duplicate vertices are found in a Face3
  471. // we have to remove the face as nothing can be saved
  472. for ( var n = 0; n < 3; n ++ ) {
  473. if ( indices[ n ] == indices[ ( n + 1 ) % 3 ] ) {
  474. dupIndex = n;
  475. faceIndicesToRemove.push( i );
  476. break;
  477. }
  478. }
  479. }
  480. for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  481. var idx = faceIndicesToRemove[ i ];
  482. this.faces.splice( idx, 1 );
  483. for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  484. this.faceVertexUvs[ j ].splice( idx, 1 );
  485. }
  486. }
  487. // Use unique set of vertices
  488. var diff = this.vertices.length - unique.length;
  489. this.vertices = unique;
  490. return diff;
  491. },
  492. toJSON: function() {
  493. // we will store all serialization data on 'data'
  494. var data = {};
  495. // add metadata
  496. data.metadata = {
  497. version: 4.4,
  498. type: 'Geometry',
  499. generator: 'Geometry.toJSON'
  500. };
  501. // standard Geometry serialization
  502. data.type = this.type;
  503. data.uuid = this.uuid;
  504. if ( this.name !== '' ) data.name = this.name;
  505. if ( this.parameters !== undefined ) {
  506. var parameters = this.parameters;
  507. for ( var key in parameters ) {
  508. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  509. }
  510. return data;
  511. }
  512. var vertices = [];
  513. for ( var i = 0; i < this.vertices.length; i ++ ) {
  514. var vertex = this.vertices[ i ];
  515. vertices.push( vertex.x, vertex.y, vertex.z );
  516. }
  517. var faces = [];
  518. var normals = [];
  519. var normalsHash = {};
  520. var colors = [];
  521. var colorsHash = {};
  522. var uvs = [];
  523. var uvsHash = {};
  524. for ( var i = 0; i < this.faces.length; i ++ ) {
  525. var face = this.faces[ i ];
  526. var hasMaterial = false; // face.materialIndex !== undefined;
  527. var hasFaceUv = false; // deprecated
  528. var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  529. var hasFaceNormal = face.normal.length() > 0;
  530. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  531. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  532. var hasFaceVertexColor = face.vertexColors.length > 0;
  533. var faceType = 0;
  534. faceType = setBit( faceType, 0, 0 );
  535. faceType = setBit( faceType, 1, hasMaterial );
  536. faceType = setBit( faceType, 2, hasFaceUv );
  537. faceType = setBit( faceType, 3, hasFaceVertexUv );
  538. faceType = setBit( faceType, 4, hasFaceNormal );
  539. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  540. faceType = setBit( faceType, 6, hasFaceColor );
  541. faceType = setBit( faceType, 7, hasFaceVertexColor );
  542. faces.push( faceType );
  543. faces.push( face.a, face.b, face.c );
  544. if ( hasFaceVertexUv ) {
  545. var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  546. faces.push(
  547. getUvIndex( faceVertexUvs[ 0 ] ),
  548. getUvIndex( faceVertexUvs[ 1 ] ),
  549. getUvIndex( faceVertexUvs[ 2 ] )
  550. );
  551. }
  552. if ( hasFaceNormal ) {
  553. faces.push( getNormalIndex( face.normal ) );
  554. }
  555. if ( hasFaceVertexNormal ) {
  556. var vertexNormals = face.vertexNormals;
  557. faces.push(
  558. getNormalIndex( vertexNormals[ 0 ] ),
  559. getNormalIndex( vertexNormals[ 1 ] ),
  560. getNormalIndex( vertexNormals[ 2 ] )
  561. );
  562. }
  563. if ( hasFaceColor ) {
  564. faces.push( getColorIndex( face.color ) );
  565. }
  566. if ( hasFaceVertexColor ) {
  567. var vertexColors = face.vertexColors;
  568. faces.push(
  569. getColorIndex( vertexColors[ 0 ] ),
  570. getColorIndex( vertexColors[ 1 ] ),
  571. getColorIndex( vertexColors[ 2 ] )
  572. );
  573. }
  574. }
  575. function setBit( value, position, enabled ) {
  576. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  577. }
  578. function getNormalIndex( normal ) {
  579. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  580. if ( normalsHash[ hash ] !== undefined ) {
  581. return normalsHash[ hash ];
  582. }
  583. normalsHash[ hash ] = normals.length / 3;
  584. normals.push( normal.x, normal.y, normal.z );
  585. return normalsHash[ hash ];
  586. }
  587. function getColorIndex( color ) {
  588. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  589. if ( colorsHash[ hash ] !== undefined ) {
  590. return colorsHash[ hash ];
  591. }
  592. colorsHash[ hash ] = colors.length;
  593. colors.push( color.getHex() );
  594. return colorsHash[ hash ];
  595. }
  596. function getUvIndex( uv ) {
  597. var hash = uv.x.toString() + uv.y.toString();
  598. if ( uvsHash[ hash ] !== undefined ) {
  599. return uvsHash[ hash ];
  600. }
  601. uvsHash[ hash ] = uvs.length / 2;
  602. uvs.push( uv.x, uv.y );
  603. return uvsHash[ hash ];
  604. }
  605. data.data = {};
  606. data.data.vertices = vertices;
  607. data.data.normals = normals;
  608. if ( colors.length > 0 ) data.data.colors = colors;
  609. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  610. data.data.faces = faces;
  611. return data;
  612. },
  613. clone: function () {
  614. var geometry = new THREE.Geometry();
  615. var vertices = this.vertices;
  616. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  617. geometry.vertices.push( vertices[ i ].clone() );
  618. }
  619. var faces = this.faces;
  620. for ( var i = 0, il = faces.length; i < il; i ++ ) {
  621. geometry.faces.push( faces[ i ].clone() );
  622. }
  623. for ( var i = 0, il = this.faceVertexUvs.length; i < il; i ++ ) {
  624. var faceVertexUvs = this.faceVertexUvs[ i ];
  625. if ( geometry.faceVertexUvs[ i ] === undefined ) {
  626. geometry.faceVertexUvs[ i ] = [];
  627. }
  628. for ( var j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  629. var uvs = faceVertexUvs[ j ], uvsCopy = [];
  630. for ( var k = 0, kl = uvs.length; k < kl; k ++ ) {
  631. var uv = uvs[ k ];
  632. uvsCopy.push( uv.clone() );
  633. }
  634. geometry.faceVertexUvs[ i ].push( uvsCopy );
  635. }
  636. }
  637. return geometry;
  638. },
  639. dispose: function () {
  640. this.dispatchEvent( { type: 'dispose' } );
  641. }
  642. };
  643. THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );
  644. THREE.GeometryIdCount = 0;