Geometry.js 23 KB

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