Geometry.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. import {
  2. Box3,
  3. BufferAttribute,
  4. BufferGeometry,
  5. Color,
  6. EventDispatcher,
  7. Face3,
  8. Float32BufferAttribute,
  9. Matrix3,
  10. Matrix4,
  11. MathUtils,
  12. Object3D,
  13. Sphere,
  14. Vector2,
  15. Vector3
  16. } from '../../../build/three.module.js';
  17. const _m1 = new Matrix4();
  18. const _obj = new Object3D();
  19. const _offset = new Vector3();
  20. function Geometry() {
  21. this.uuid = MathUtils.generateUUID();
  22. this.name = '';
  23. this.type = 'Geometry';
  24. this.vertices = [];
  25. this.colors = [];
  26. this.faces = [];
  27. this.faceVertexUvs = [[]];
  28. this.morphTargets = [];
  29. this.morphNormals = [];
  30. this.skinWeights = [];
  31. this.skinIndices = [];
  32. this.lineDistances = [];
  33. this.boundingBox = null;
  34. this.boundingSphere = null;
  35. // update flags
  36. this.elementsNeedUpdate = false;
  37. this.verticesNeedUpdate = false;
  38. this.uvsNeedUpdate = false;
  39. this.normalsNeedUpdate = false;
  40. this.colorsNeedUpdate = false;
  41. this.lineDistancesNeedUpdate = false;
  42. this.groupsNeedUpdate = false;
  43. }
  44. Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  45. constructor: Geometry,
  46. isGeometry: true,
  47. applyMatrix4: function ( matrix ) {
  48. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  49. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  50. const vertex = this.vertices[ i ];
  51. vertex.applyMatrix4( matrix );
  52. }
  53. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  54. const face = this.faces[ i ];
  55. face.normal.applyMatrix3( normalMatrix ).normalize();
  56. for ( let j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  57. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  58. }
  59. }
  60. if ( this.boundingBox !== null ) {
  61. this.computeBoundingBox();
  62. }
  63. if ( this.boundingSphere !== null ) {
  64. this.computeBoundingSphere();
  65. }
  66. this.verticesNeedUpdate = true;
  67. this.normalsNeedUpdate = true;
  68. return this;
  69. },
  70. rotateX: function ( angle ) {
  71. // rotate geometry around world x-axis
  72. _m1.makeRotationX( angle );
  73. this.applyMatrix4( _m1 );
  74. return this;
  75. },
  76. rotateY: function ( angle ) {
  77. // rotate geometry around world y-axis
  78. _m1.makeRotationY( angle );
  79. this.applyMatrix4( _m1 );
  80. return this;
  81. },
  82. rotateZ: function ( angle ) {
  83. // rotate geometry around world z-axis
  84. _m1.makeRotationZ( angle );
  85. this.applyMatrix4( _m1 );
  86. return this;
  87. },
  88. translate: function ( x, y, z ) {
  89. // translate geometry
  90. _m1.makeTranslation( x, y, z );
  91. this.applyMatrix4( _m1 );
  92. return this;
  93. },
  94. scale: function ( x, y, z ) {
  95. // scale geometry
  96. _m1.makeScale( x, y, z );
  97. this.applyMatrix4( _m1 );
  98. return this;
  99. },
  100. lookAt: function ( vector ) {
  101. _obj.lookAt( vector );
  102. _obj.updateMatrix();
  103. this.applyMatrix4( _obj.matrix );
  104. return this;
  105. },
  106. fromBufferGeometry: function ( geometry ) {
  107. const scope = this;
  108. const index = geometry.index !== null ? geometry.index : undefined;
  109. const attributes = geometry.attributes;
  110. if ( attributes.position === undefined ) {
  111. console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' );
  112. return this;
  113. }
  114. const position = attributes.position;
  115. const normal = attributes.normal;
  116. const color = attributes.color;
  117. const uv = attributes.uv;
  118. const uv2 = attributes.uv2;
  119. if ( uv2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
  120. for ( let i = 0; i < position.count; i ++ ) {
  121. scope.vertices.push( new Vector3().fromBufferAttribute( position, i ) );
  122. if ( color !== undefined ) {
  123. scope.colors.push( new Color().fromBufferAttribute( color, i ) );
  124. }
  125. }
  126. function addFace( a, b, c, materialIndex ) {
  127. const vertexColors = ( color === undefined ) ? [] : [
  128. scope.colors[ a ].clone(),
  129. scope.colors[ b ].clone(),
  130. scope.colors[ c ].clone()
  131. ];
  132. const vertexNormals = ( normal === undefined ) ? [] : [
  133. new Vector3().fromBufferAttribute( normal, a ),
  134. new Vector3().fromBufferAttribute( normal, b ),
  135. new Vector3().fromBufferAttribute( normal, c )
  136. ];
  137. const face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
  138. scope.faces.push( face );
  139. if ( uv !== undefined ) {
  140. scope.faceVertexUvs[ 0 ].push( [
  141. new Vector2().fromBufferAttribute( uv, a ),
  142. new Vector2().fromBufferAttribute( uv, b ),
  143. new Vector2().fromBufferAttribute( uv, c )
  144. ] );
  145. }
  146. if ( uv2 !== undefined ) {
  147. scope.faceVertexUvs[ 1 ].push( [
  148. new Vector2().fromBufferAttribute( uv2, a ),
  149. new Vector2().fromBufferAttribute( uv2, b ),
  150. new Vector2().fromBufferAttribute( uv2, c )
  151. ] );
  152. }
  153. }
  154. const groups = geometry.groups;
  155. if ( groups.length > 0 ) {
  156. for ( let i = 0; i < groups.length; i ++ ) {
  157. const group = groups[ i ];
  158. const start = group.start;
  159. const count = group.count;
  160. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  161. if ( index !== undefined ) {
  162. addFace( index.getX( j ), index.getX( j + 1 ), index.getX( j + 2 ), group.materialIndex );
  163. } else {
  164. addFace( j, j + 1, j + 2, group.materialIndex );
  165. }
  166. }
  167. }
  168. } else {
  169. if ( index !== undefined ) {
  170. for ( let i = 0; i < index.count; i += 3 ) {
  171. addFace( index.getX( i ), index.getX( i + 1 ), index.getX( i + 2 ) );
  172. }
  173. } else {
  174. for ( let i = 0; i < position.count; i += 3 ) {
  175. addFace( i, i + 1, i + 2 );
  176. }
  177. }
  178. }
  179. this.computeFaceNormals();
  180. if ( geometry.boundingBox !== null ) {
  181. this.boundingBox = geometry.boundingBox.clone();
  182. }
  183. if ( geometry.boundingSphere !== null ) {
  184. this.boundingSphere = geometry.boundingSphere.clone();
  185. }
  186. return this;
  187. },
  188. center: function () {
  189. this.computeBoundingBox();
  190. this.boundingBox.getCenter( _offset ).negate();
  191. this.translate( _offset.x, _offset.y, _offset.z );
  192. return this;
  193. },
  194. normalize: function () {
  195. this.computeBoundingSphere();
  196. const center = this.boundingSphere.center;
  197. const radius = this.boundingSphere.radius;
  198. const s = radius === 0 ? 1 : 1.0 / radius;
  199. const matrix = new Matrix4();
  200. matrix.set(
  201. s, 0, 0, - s * center.x,
  202. 0, s, 0, - s * center.y,
  203. 0, 0, s, - s * center.z,
  204. 0, 0, 0, 1
  205. );
  206. this.applyMatrix4( matrix );
  207. return this;
  208. },
  209. computeFaceNormals: function () {
  210. const cb = new Vector3(), ab = new Vector3();
  211. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  212. const face = this.faces[ f ];
  213. const vA = this.vertices[ face.a ];
  214. const vB = this.vertices[ face.b ];
  215. const vC = this.vertices[ face.c ];
  216. cb.subVectors( vC, vB );
  217. ab.subVectors( vA, vB );
  218. cb.cross( ab );
  219. cb.normalize();
  220. face.normal.copy( cb );
  221. }
  222. },
  223. computeVertexNormals: function ( areaWeighted = true ) {
  224. const vertices = new Array( this.vertices.length );
  225. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  226. vertices[ v ] = new Vector3();
  227. }
  228. if ( areaWeighted ) {
  229. // vertex normals weighted by triangle areas
  230. // http://www.iquilezles.org/www/articles/normals/normals.htm
  231. const cb = new Vector3(), ab = new Vector3();
  232. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  233. const face = this.faces[ f ];
  234. const vA = this.vertices[ face.a ];
  235. const vB = this.vertices[ face.b ];
  236. const vC = this.vertices[ face.c ];
  237. cb.subVectors( vC, vB );
  238. ab.subVectors( vA, vB );
  239. cb.cross( ab );
  240. vertices[ face.a ].add( cb );
  241. vertices[ face.b ].add( cb );
  242. vertices[ face.c ].add( cb );
  243. }
  244. } else {
  245. this.computeFaceNormals();
  246. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  247. const face = this.faces[ f ];
  248. vertices[ face.a ].add( face.normal );
  249. vertices[ face.b ].add( face.normal );
  250. vertices[ face.c ].add( face.normal );
  251. }
  252. }
  253. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  254. vertices[ v ].normalize();
  255. }
  256. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  257. const face = this.faces[ f ];
  258. const vertexNormals = face.vertexNormals;
  259. if ( vertexNormals.length === 3 ) {
  260. vertexNormals[ 0 ].copy( vertices[ face.a ] );
  261. vertexNormals[ 1 ].copy( vertices[ face.b ] );
  262. vertexNormals[ 2 ].copy( vertices[ face.c ] );
  263. } else {
  264. vertexNormals[ 0 ] = vertices[ face.a ].clone();
  265. vertexNormals[ 1 ] = vertices[ face.b ].clone();
  266. vertexNormals[ 2 ] = vertices[ face.c ].clone();
  267. }
  268. }
  269. if ( this.faces.length > 0 ) {
  270. this.normalsNeedUpdate = true;
  271. }
  272. },
  273. computeFlatVertexNormals: function () {
  274. this.computeFaceNormals();
  275. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  276. const face = this.faces[ f ];
  277. const vertexNormals = face.vertexNormals;
  278. if ( vertexNormals.length === 3 ) {
  279. vertexNormals[ 0 ].copy( face.normal );
  280. vertexNormals[ 1 ].copy( face.normal );
  281. vertexNormals[ 2 ].copy( face.normal );
  282. } else {
  283. vertexNormals[ 0 ] = face.normal.clone();
  284. vertexNormals[ 1 ] = face.normal.clone();
  285. vertexNormals[ 2 ] = face.normal.clone();
  286. }
  287. }
  288. if ( this.faces.length > 0 ) {
  289. this.normalsNeedUpdate = true;
  290. }
  291. },
  292. computeMorphNormals: function () {
  293. // save original normals
  294. // - create temp variables on first access
  295. // otherwise just copy (for faster repeated calls)
  296. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  297. const face = this.faces[ f ];
  298. if ( ! face.__originalFaceNormal ) {
  299. face.__originalFaceNormal = face.normal.clone();
  300. } else {
  301. face.__originalFaceNormal.copy( face.normal );
  302. }
  303. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  304. for ( let i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  305. if ( ! face.__originalVertexNormals[ i ] ) {
  306. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  307. } else {
  308. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  309. }
  310. }
  311. }
  312. // use temp geometry to compute face and vertex normals for each morph
  313. const tmpGeo = new Geometry();
  314. tmpGeo.faces = this.faces;
  315. for ( let i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  316. // create on first access
  317. if ( ! this.morphNormals[ i ] ) {
  318. this.morphNormals[ i ] = {};
  319. this.morphNormals[ i ].faceNormals = [];
  320. this.morphNormals[ i ].vertexNormals = [];
  321. const dstNormalsFace = this.morphNormals[ i ].faceNormals;
  322. const dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  323. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  324. const faceNormal = new Vector3();
  325. const vertexNormals = { a: new Vector3(), b: new Vector3(), c: new Vector3() };
  326. dstNormalsFace.push( faceNormal );
  327. dstNormalsVertex.push( vertexNormals );
  328. }
  329. }
  330. const morphNormals = this.morphNormals[ i ];
  331. // set vertices to morph target
  332. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  333. // compute morph normals
  334. tmpGeo.computeFaceNormals();
  335. tmpGeo.computeVertexNormals();
  336. // store morph normals
  337. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  338. const face = this.faces[ f ];
  339. const faceNormal = morphNormals.faceNormals[ f ];
  340. const vertexNormals = morphNormals.vertexNormals[ f ];
  341. faceNormal.copy( face.normal );
  342. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  343. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  344. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  345. }
  346. }
  347. // restore original normals
  348. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  349. const face = this.faces[ f ];
  350. face.normal = face.__originalFaceNormal;
  351. face.vertexNormals = face.__originalVertexNormals;
  352. }
  353. },
  354. computeBoundingBox: function () {
  355. if ( this.boundingBox === null ) {
  356. this.boundingBox = new Box3();
  357. }
  358. this.boundingBox.setFromPoints( this.vertices );
  359. },
  360. computeBoundingSphere: function () {
  361. if ( this.boundingSphere === null ) {
  362. this.boundingSphere = new Sphere();
  363. }
  364. this.boundingSphere.setFromPoints( this.vertices );
  365. },
  366. merge: function ( geometry, matrix, materialIndexOffset = 0 ) {
  367. if ( ! ( geometry && geometry.isGeometry ) ) {
  368. console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  369. return;
  370. }
  371. let normalMatrix;
  372. const vertexOffset = this.vertices.length,
  373. vertices1 = this.vertices,
  374. vertices2 = geometry.vertices,
  375. faces1 = this.faces,
  376. faces2 = geometry.faces,
  377. colors1 = this.colors,
  378. colors2 = geometry.colors;
  379. if ( matrix !== undefined ) {
  380. normalMatrix = new Matrix3().getNormalMatrix( matrix );
  381. }
  382. // vertices
  383. for ( let i = 0, il = vertices2.length; i < il; i ++ ) {
  384. const vertex = vertices2[ i ];
  385. const vertexCopy = vertex.clone();
  386. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  387. vertices1.push( vertexCopy );
  388. }
  389. // colors
  390. for ( let i = 0, il = colors2.length; i < il; i ++ ) {
  391. colors1.push( colors2[ i ].clone() );
  392. }
  393. // faces
  394. for ( let i = 0, il = faces2.length; i < il; i ++ ) {
  395. const face = faces2[ i ];
  396. let normal, color;
  397. const faceVertexNormals = face.vertexNormals,
  398. faceVertexColors = face.vertexColors;
  399. const faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  400. faceCopy.normal.copy( face.normal );
  401. if ( normalMatrix !== undefined ) {
  402. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  403. }
  404. for ( let j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  405. normal = faceVertexNormals[ j ].clone();
  406. if ( normalMatrix !== undefined ) {
  407. normal.applyMatrix3( normalMatrix ).normalize();
  408. }
  409. faceCopy.vertexNormals.push( normal );
  410. }
  411. faceCopy.color.copy( face.color );
  412. for ( let j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  413. color = faceVertexColors[ j ];
  414. faceCopy.vertexColors.push( color.clone() );
  415. }
  416. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  417. faces1.push( faceCopy );
  418. }
  419. // uvs
  420. for ( let i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
  421. const faceVertexUvs2 = geometry.faceVertexUvs[ i ];
  422. if ( this.faceVertexUvs[ i ] === undefined ) this.faceVertexUvs[ i ] = [];
  423. for ( let j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) {
  424. const uvs2 = faceVertexUvs2[ j ], uvsCopy = [];
  425. for ( let k = 0, kl = uvs2.length; k < kl; k ++ ) {
  426. uvsCopy.push( uvs2[ k ].clone() );
  427. }
  428. this.faceVertexUvs[ i ].push( uvsCopy );
  429. }
  430. }
  431. },
  432. mergeMesh: function ( mesh ) {
  433. if ( ! ( mesh && mesh.isMesh ) ) {
  434. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  435. return;
  436. }
  437. if ( mesh.matrixAutoUpdate ) mesh.updateMatrix();
  438. this.merge( mesh.geometry, mesh.matrix );
  439. },
  440. /*
  441. * Checks for duplicate vertices with hashmap.
  442. * Duplicated vertices are removed
  443. * and faces' vertices are updated.
  444. */
  445. mergeVertices: function ( precisionPoints = 4 ) {
  446. const verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
  447. const unique = [], changes = [];
  448. const precision = Math.pow( 10, precisionPoints );
  449. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  450. const v = this.vertices[ i ];
  451. const key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  452. if ( verticesMap[ key ] === undefined ) {
  453. verticesMap[ key ] = i;
  454. unique.push( this.vertices[ i ] );
  455. changes[ i ] = unique.length - 1;
  456. } else {
  457. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  458. changes[ i ] = changes[ verticesMap[ key ] ];
  459. }
  460. }
  461. // if faces are completely degenerate after merging vertices, we
  462. // have to remove them from the geometry.
  463. const faceIndicesToRemove = [];
  464. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  465. const face = this.faces[ i ];
  466. face.a = changes[ face.a ];
  467. face.b = changes[ face.b ];
  468. face.c = changes[ face.c ];
  469. const indices = [ face.a, face.b, face.c ];
  470. // if any duplicate vertices are found in a Face3
  471. // we have to remove the face as nothing can be saved
  472. for ( let n = 0; n < 3; n ++ ) {
  473. if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
  474. faceIndicesToRemove.push( i );
  475. break;
  476. }
  477. }
  478. }
  479. for ( let i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  480. const idx = faceIndicesToRemove[ i ];
  481. this.faces.splice( idx, 1 );
  482. for ( let j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  483. this.faceVertexUvs[ j ].splice( idx, 1 );
  484. }
  485. }
  486. // Use unique set of vertices
  487. const diff = this.vertices.length - unique.length;
  488. this.vertices = unique;
  489. return diff;
  490. },
  491. setFromPoints: function ( points ) {
  492. this.vertices = [];
  493. for ( let i = 0, l = points.length; i < l; i ++ ) {
  494. const point = points[ i ];
  495. this.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
  496. }
  497. return this;
  498. },
  499. sortFacesByMaterialIndex: function () {
  500. const faces = this.faces;
  501. const length = faces.length;
  502. // tag faces
  503. for ( let i = 0; i < length; i ++ ) {
  504. faces[ i ]._id = i;
  505. }
  506. // sort faces
  507. function materialIndexSort( a, b ) {
  508. return a.materialIndex - b.materialIndex;
  509. }
  510. faces.sort( materialIndexSort );
  511. // sort uvs
  512. const uvs1 = this.faceVertexUvs[ 0 ];
  513. const uvs2 = this.faceVertexUvs[ 1 ];
  514. let newUvs1, newUvs2;
  515. if ( uvs1 && uvs1.length === length ) newUvs1 = [];
  516. if ( uvs2 && uvs2.length === length ) newUvs2 = [];
  517. for ( let i = 0; i < length; i ++ ) {
  518. const id = faces[ i ]._id;
  519. if ( newUvs1 ) newUvs1.push( uvs1[ id ] );
  520. if ( newUvs2 ) newUvs2.push( uvs2[ id ] );
  521. }
  522. if ( newUvs1 ) this.faceVertexUvs[ 0 ] = newUvs1;
  523. if ( newUvs2 ) this.faceVertexUvs[ 1 ] = newUvs2;
  524. },
  525. toJSON: function () {
  526. const data = {
  527. metadata: {
  528. version: 4.5,
  529. type: 'Geometry',
  530. generator: 'Geometry.toJSON'
  531. }
  532. };
  533. // standard Geometry serialization
  534. data.uuid = this.uuid;
  535. data.type = this.type;
  536. if ( this.name !== '' ) data.name = this.name;
  537. if ( this.parameters !== undefined ) {
  538. const parameters = this.parameters;
  539. for ( const key in parameters ) {
  540. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  541. }
  542. return data;
  543. }
  544. const vertices = [];
  545. for ( let i = 0; i < this.vertices.length; i ++ ) {
  546. const vertex = this.vertices[ i ];
  547. vertices.push( vertex.x, vertex.y, vertex.z );
  548. }
  549. const faces = [];
  550. const normals = [];
  551. const normalsHash = {};
  552. const colors = [];
  553. const colorsHash = {};
  554. const uvs = [];
  555. const uvsHash = {};
  556. for ( let i = 0; i < this.faces.length; i ++ ) {
  557. const face = this.faces[ i ];
  558. const hasMaterial = true;
  559. const hasFaceUv = false; // deprecated
  560. const hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  561. const hasFaceNormal = face.normal.length() > 0;
  562. const hasFaceVertexNormal = face.vertexNormals.length > 0;
  563. const hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  564. const hasFaceVertexColor = face.vertexColors.length > 0;
  565. let faceType = 0;
  566. faceType = setBit( faceType, 0, 0 ); // isQuad
  567. faceType = setBit( faceType, 1, hasMaterial );
  568. faceType = setBit( faceType, 2, hasFaceUv );
  569. faceType = setBit( faceType, 3, hasFaceVertexUv );
  570. faceType = setBit( faceType, 4, hasFaceNormal );
  571. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  572. faceType = setBit( faceType, 6, hasFaceColor );
  573. faceType = setBit( faceType, 7, hasFaceVertexColor );
  574. faces.push( faceType );
  575. faces.push( face.a, face.b, face.c );
  576. faces.push( face.materialIndex );
  577. if ( hasFaceVertexUv ) {
  578. const faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  579. faces.push(
  580. getUvIndex( faceVertexUvs[ 0 ] ),
  581. getUvIndex( faceVertexUvs[ 1 ] ),
  582. getUvIndex( faceVertexUvs[ 2 ] )
  583. );
  584. }
  585. if ( hasFaceNormal ) {
  586. faces.push( getNormalIndex( face.normal ) );
  587. }
  588. if ( hasFaceVertexNormal ) {
  589. const vertexNormals = face.vertexNormals;
  590. faces.push(
  591. getNormalIndex( vertexNormals[ 0 ] ),
  592. getNormalIndex( vertexNormals[ 1 ] ),
  593. getNormalIndex( vertexNormals[ 2 ] )
  594. );
  595. }
  596. if ( hasFaceColor ) {
  597. faces.push( getColorIndex( face.color ) );
  598. }
  599. if ( hasFaceVertexColor ) {
  600. const vertexColors = face.vertexColors;
  601. faces.push(
  602. getColorIndex( vertexColors[ 0 ] ),
  603. getColorIndex( vertexColors[ 1 ] ),
  604. getColorIndex( vertexColors[ 2 ] )
  605. );
  606. }
  607. }
  608. function setBit( value, position, enabled ) {
  609. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
  610. }
  611. function getNormalIndex( normal ) {
  612. const hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  613. if ( normalsHash[ hash ] !== undefined ) {
  614. return normalsHash[ hash ];
  615. }
  616. normalsHash[ hash ] = normals.length / 3;
  617. normals.push( normal.x, normal.y, normal.z );
  618. return normalsHash[ hash ];
  619. }
  620. function getColorIndex( color ) {
  621. const hash = color.r.toString() + color.g.toString() + color.b.toString();
  622. if ( colorsHash[ hash ] !== undefined ) {
  623. return colorsHash[ hash ];
  624. }
  625. colorsHash[ hash ] = colors.length;
  626. colors.push( color.getHex() );
  627. return colorsHash[ hash ];
  628. }
  629. function getUvIndex( uv ) {
  630. const hash = uv.x.toString() + uv.y.toString();
  631. if ( uvsHash[ hash ] !== undefined ) {
  632. return uvsHash[ hash ];
  633. }
  634. uvsHash[ hash ] = uvs.length / 2;
  635. uvs.push( uv.x, uv.y );
  636. return uvsHash[ hash ];
  637. }
  638. data.data = {};
  639. data.data.vertices = vertices;
  640. data.data.normals = normals;
  641. if ( colors.length > 0 ) data.data.colors = colors;
  642. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  643. data.data.faces = faces;
  644. return data;
  645. },
  646. clone: function () {
  647. /*
  648. // Handle primitives
  649. const parameters = this.parameters;
  650. if ( parameters !== undefined ) {
  651. const values = [];
  652. for ( const key in parameters ) {
  653. values.push( parameters[ key ] );
  654. }
  655. const geometry = Object.create( this.constructor.prototype );
  656. this.constructor.apply( geometry, values );
  657. return geometry;
  658. }
  659. return new this.constructor().copy( this );
  660. */
  661. return new Geometry().copy( this );
  662. },
  663. copy: function ( source ) {
  664. // reset
  665. this.vertices = [];
  666. this.colors = [];
  667. this.faces = [];
  668. this.faceVertexUvs = [[]];
  669. this.morphTargets = [];
  670. this.morphNormals = [];
  671. this.skinWeights = [];
  672. this.skinIndices = [];
  673. this.lineDistances = [];
  674. this.boundingBox = null;
  675. this.boundingSphere = null;
  676. // name
  677. this.name = source.name;
  678. // vertices
  679. const vertices = source.vertices;
  680. for ( let i = 0, il = vertices.length; i < il; i ++ ) {
  681. this.vertices.push( vertices[ i ].clone() );
  682. }
  683. // colors
  684. const colors = source.colors;
  685. for ( let i = 0, il = colors.length; i < il; i ++ ) {
  686. this.colors.push( colors[ i ].clone() );
  687. }
  688. // faces
  689. const faces = source.faces;
  690. for ( let i = 0, il = faces.length; i < il; i ++ ) {
  691. this.faces.push( faces[ i ].clone() );
  692. }
  693. // face vertex uvs
  694. for ( let i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
  695. const faceVertexUvs = source.faceVertexUvs[ i ];
  696. if ( this.faceVertexUvs[ i ] === undefined ) {
  697. this.faceVertexUvs[ i ] = [];
  698. }
  699. for ( let j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  700. const uvs = faceVertexUvs[ j ], uvsCopy = [];
  701. for ( let k = 0, kl = uvs.length; k < kl; k ++ ) {
  702. const uv = uvs[ k ];
  703. uvsCopy.push( uv.clone() );
  704. }
  705. this.faceVertexUvs[ i ].push( uvsCopy );
  706. }
  707. }
  708. // morph targets
  709. const morphTargets = source.morphTargets;
  710. for ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  711. const morphTarget = {};
  712. morphTarget.name = morphTargets[ i ].name;
  713. // vertices
  714. if ( morphTargets[ i ].vertices !== undefined ) {
  715. morphTarget.vertices = [];
  716. for ( let j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) {
  717. morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() );
  718. }
  719. }
  720. // normals
  721. if ( morphTargets[ i ].normals !== undefined ) {
  722. morphTarget.normals = [];
  723. for ( let j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) {
  724. morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() );
  725. }
  726. }
  727. this.morphTargets.push( morphTarget );
  728. }
  729. // morph normals
  730. const morphNormals = source.morphNormals;
  731. for ( let i = 0, il = morphNormals.length; i < il; i ++ ) {
  732. const morphNormal = {};
  733. // vertex normals
  734. if ( morphNormals[ i ].vertexNormals !== undefined ) {
  735. morphNormal.vertexNormals = [];
  736. for ( let j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) {
  737. const srcVertexNormal = morphNormals[ i ].vertexNormals[ j ];
  738. const destVertexNormal = {};
  739. destVertexNormal.a = srcVertexNormal.a.clone();
  740. destVertexNormal.b = srcVertexNormal.b.clone();
  741. destVertexNormal.c = srcVertexNormal.c.clone();
  742. morphNormal.vertexNormals.push( destVertexNormal );
  743. }
  744. }
  745. // face normals
  746. if ( morphNormals[ i ].faceNormals !== undefined ) {
  747. morphNormal.faceNormals = [];
  748. for ( let j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) {
  749. morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() );
  750. }
  751. }
  752. this.morphNormals.push( morphNormal );
  753. }
  754. // skin weights
  755. const skinWeights = source.skinWeights;
  756. for ( let i = 0, il = skinWeights.length; i < il; i ++ ) {
  757. this.skinWeights.push( skinWeights[ i ].clone() );
  758. }
  759. // skin indices
  760. const skinIndices = source.skinIndices;
  761. for ( let i = 0, il = skinIndices.length; i < il; i ++ ) {
  762. this.skinIndices.push( skinIndices[ i ].clone() );
  763. }
  764. // line distances
  765. const lineDistances = source.lineDistances;
  766. for ( let i = 0, il = lineDistances.length; i < il; i ++ ) {
  767. this.lineDistances.push( lineDistances[ i ] );
  768. }
  769. // bounding box
  770. const boundingBox = source.boundingBox;
  771. if ( boundingBox !== null ) {
  772. this.boundingBox = boundingBox.clone();
  773. }
  774. // bounding sphere
  775. const boundingSphere = source.boundingSphere;
  776. if ( boundingSphere !== null ) {
  777. this.boundingSphere = boundingSphere.clone();
  778. }
  779. // update flags
  780. this.elementsNeedUpdate = source.elementsNeedUpdate;
  781. this.verticesNeedUpdate = source.verticesNeedUpdate;
  782. this.uvsNeedUpdate = source.uvsNeedUpdate;
  783. this.normalsNeedUpdate = source.normalsNeedUpdate;
  784. this.colorsNeedUpdate = source.colorsNeedUpdate;
  785. this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate;
  786. this.groupsNeedUpdate = source.groupsNeedUpdate;
  787. return this;
  788. },
  789. toBufferGeometry: function () {
  790. const geometry = new DirectGeometry().fromGeometry( this );
  791. const buffergeometry = new BufferGeometry();
  792. const positions = new Float32Array( geometry.vertices.length * 3 );
  793. buffergeometry.setAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  794. if ( geometry.normals.length > 0 ) {
  795. const normals = new Float32Array( geometry.normals.length * 3 );
  796. buffergeometry.setAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  797. }
  798. if ( geometry.colors.length > 0 ) {
  799. const colors = new Float32Array( geometry.colors.length * 3 );
  800. buffergeometry.setAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  801. }
  802. if ( geometry.uvs.length > 0 ) {
  803. const uvs = new Float32Array( geometry.uvs.length * 2 );
  804. buffergeometry.setAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  805. }
  806. if ( geometry.uvs2.length > 0 ) {
  807. const uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  808. buffergeometry.setAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  809. }
  810. // groups
  811. buffergeometry.groups = geometry.groups;
  812. // morphs
  813. for ( const name in geometry.morphTargets ) {
  814. const array = [];
  815. const morphTargets = geometry.morphTargets[ name ];
  816. for ( let i = 0, l = morphTargets.length; i < l; i ++ ) {
  817. const morphTarget = morphTargets[ i ];
  818. const attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 );
  819. attribute.name = morphTarget.name;
  820. array.push( attribute.copyVector3sArray( morphTarget.data ) );
  821. }
  822. buffergeometry.morphAttributes[ name ] = array;
  823. }
  824. // skinning
  825. if ( geometry.skinIndices.length > 0 ) {
  826. const skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  827. buffergeometry.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  828. }
  829. if ( geometry.skinWeights.length > 0 ) {
  830. const skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  831. buffergeometry.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  832. }
  833. //
  834. if ( geometry.boundingSphere !== null ) {
  835. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  836. }
  837. if ( geometry.boundingBox !== null ) {
  838. buffergeometry.boundingBox = geometry.boundingBox.clone();
  839. }
  840. return buffergeometry;
  841. },
  842. computeTangents: function () {
  843. console.error( 'THREE.Geometry: .computeTangents() has been removed.' );
  844. },
  845. computeLineDistances: function () {
  846. console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' );
  847. },
  848. applyMatrix: function ( matrix ) {
  849. console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' );
  850. return this.applyMatrix4( matrix );
  851. },
  852. dispose: function () {
  853. this.dispatchEvent( { type: 'dispose' } );
  854. }
  855. } );
  856. Geometry.createBufferGeometryFromObject = function ( object ) {
  857. let buffergeometry = new BufferGeometry();
  858. const geometry = object.geometry;
  859. if ( object.isPoints || object.isLine ) {
  860. const positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  861. const colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
  862. buffergeometry.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  863. buffergeometry.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  864. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  865. const lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
  866. buffergeometry.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  867. }
  868. if ( geometry.boundingSphere !== null ) {
  869. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  870. }
  871. if ( geometry.boundingBox !== null ) {
  872. buffergeometry.boundingBox = geometry.boundingBox.clone();
  873. }
  874. } else if ( object.isMesh ) {
  875. buffergeometry = geometry.toBufferGeometry();
  876. }
  877. return buffergeometry;
  878. };
  879. class DirectGeometry {
  880. constructor() {
  881. this.vertices = [];
  882. this.normals = [];
  883. this.colors = [];
  884. this.uvs = [];
  885. this.uvs2 = [];
  886. this.groups = [];
  887. this.morphTargets = {};
  888. this.skinWeights = [];
  889. this.skinIndices = [];
  890. // this.lineDistances = [];
  891. this.boundingBox = null;
  892. this.boundingSphere = null;
  893. // update flags
  894. this.verticesNeedUpdate = false;
  895. this.normalsNeedUpdate = false;
  896. this.colorsNeedUpdate = false;
  897. this.uvsNeedUpdate = false;
  898. this.groupsNeedUpdate = false;
  899. }
  900. computeGroups( geometry ) {
  901. const groups = [];
  902. let group, i;
  903. let materialIndex = undefined;
  904. const faces = geometry.faces;
  905. for ( i = 0; i < faces.length; i ++ ) {
  906. const face = faces[ i ];
  907. // materials
  908. if ( face.materialIndex !== materialIndex ) {
  909. materialIndex = face.materialIndex;
  910. if ( group !== undefined ) {
  911. group.count = ( i * 3 ) - group.start;
  912. groups.push( group );
  913. }
  914. group = {
  915. start: i * 3,
  916. materialIndex: materialIndex
  917. };
  918. }
  919. }
  920. if ( group !== undefined ) {
  921. group.count = ( i * 3 ) - group.start;
  922. groups.push( group );
  923. }
  924. this.groups = groups;
  925. }
  926. fromGeometry( geometry ) {
  927. const faces = geometry.faces;
  928. const vertices = geometry.vertices;
  929. const faceVertexUvs = geometry.faceVertexUvs;
  930. const hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  931. const hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
  932. // morphs
  933. const morphTargets = geometry.morphTargets;
  934. const morphTargetsLength = morphTargets.length;
  935. let morphTargetsPosition;
  936. if ( morphTargetsLength > 0 ) {
  937. morphTargetsPosition = [];
  938. for ( let i = 0; i < morphTargetsLength; i ++ ) {
  939. morphTargetsPosition[ i ] = {
  940. name: morphTargets[ i ].name,
  941. data: []
  942. };
  943. }
  944. this.morphTargets.position = morphTargetsPosition;
  945. }
  946. const morphNormals = geometry.morphNormals;
  947. const morphNormalsLength = morphNormals.length;
  948. let morphTargetsNormal;
  949. if ( morphNormalsLength > 0 ) {
  950. morphTargetsNormal = [];
  951. for ( let i = 0; i < morphNormalsLength; i ++ ) {
  952. morphTargetsNormal[ i ] = {
  953. name: morphNormals[ i ].name,
  954. data: []
  955. };
  956. }
  957. this.morphTargets.normal = morphTargetsNormal;
  958. }
  959. // skins
  960. const skinIndices = geometry.skinIndices;
  961. const skinWeights = geometry.skinWeights;
  962. const hasSkinIndices = skinIndices.length === vertices.length;
  963. const hasSkinWeights = skinWeights.length === vertices.length;
  964. //
  965. if ( vertices.length > 0 && faces.length === 0 ) {
  966. console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
  967. }
  968. for ( let i = 0; i < faces.length; i ++ ) {
  969. const face = faces[ i ];
  970. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  971. const vertexNormals = face.vertexNormals;
  972. if ( vertexNormals.length === 3 ) {
  973. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  974. } else {
  975. const normal = face.normal;
  976. this.normals.push( normal, normal, normal );
  977. }
  978. const vertexColors = face.vertexColors;
  979. if ( vertexColors.length === 3 ) {
  980. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  981. } else {
  982. const color = face.color;
  983. this.colors.push( color, color, color );
  984. }
  985. if ( hasFaceVertexUv === true ) {
  986. const vertexUvs = faceVertexUvs[ 0 ][ i ];
  987. if ( vertexUvs !== undefined ) {
  988. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  989. } else {
  990. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
  991. this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
  992. }
  993. }
  994. if ( hasFaceVertexUv2 === true ) {
  995. const vertexUvs = faceVertexUvs[ 1 ][ i ];
  996. if ( vertexUvs !== undefined ) {
  997. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  998. } else {
  999. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
  1000. this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
  1001. }
  1002. }
  1003. // morphs
  1004. for ( let j = 0; j < morphTargetsLength; j ++ ) {
  1005. const morphTarget = morphTargets[ j ].vertices;
  1006. morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  1007. }
  1008. for ( let j = 0; j < morphNormalsLength; j ++ ) {
  1009. const morphNormal = morphNormals[ j ].vertexNormals[ i ];
  1010. morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
  1011. }
  1012. // skins
  1013. if ( hasSkinIndices ) {
  1014. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  1015. }
  1016. if ( hasSkinWeights ) {
  1017. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  1018. }
  1019. }
  1020. this.computeGroups( geometry );
  1021. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  1022. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  1023. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  1024. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  1025. this.groupsNeedUpdate = geometry.groupsNeedUpdate;
  1026. if ( geometry.boundingSphere !== null ) {
  1027. this.boundingSphere = geometry.boundingSphere.clone();
  1028. }
  1029. if ( geometry.boundingBox !== null ) {
  1030. this.boundingBox = geometry.boundingBox.clone();
  1031. }
  1032. return this;
  1033. }
  1034. }
  1035. export { Geometry };