Geometry.js 28 KB

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