Geometry.js 37 KB

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