Geometry.js 27 KB

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