2
0

Geometry.js 27 KB

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