Geometry.js 27 KB

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