Geometry.js 37 KB

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