2
0

Geometry.js 23 KB

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