Geometry.js 27 KB

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