Geometry.js 23 KB

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