Geometry.js 27 KB

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