Geometry.js 27 KB

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