BufferGeometry.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Box3 } from '../math/Box3.js';
  3. import { EventDispatcher } from './EventDispatcher.js';
  4. import { BufferAttribute, Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from './BufferAttribute.js';
  5. import { Sphere } from '../math/Sphere.js';
  6. import { DirectGeometry } from './DirectGeometry.js';
  7. import { Object3D } from './Object3D.js';
  8. import { Matrix4 } from '../math/Matrix4.js';
  9. import { Matrix3 } from '../math/Matrix3.js';
  10. import { MathUtils } from '../math/MathUtils.js';
  11. import { arrayMax } from '../utils.js';
  12. /**
  13. * @author alteredq / http://alteredqualia.com/
  14. * @author mrdoob / http://mrdoob.com/
  15. */
  16. let _bufferGeometryId = 1; // BufferGeometry uses odd numbers as Id
  17. const _m1 = new Matrix4();
  18. const _obj = new Object3D();
  19. const _offset = new Vector3();
  20. const _box = new Box3();
  21. const _boxMorphTargets = new Box3();
  22. const _vector = new Vector3();
  23. function BufferGeometry() {
  24. Object.defineProperty( this, 'id', { value: _bufferGeometryId += 2 } );
  25. this.uuid = MathUtils.generateUUID();
  26. this.name = '';
  27. this.type = 'BufferGeometry';
  28. this.index = null;
  29. this.attributes = {};
  30. this.morphAttributes = {};
  31. this.morphTargetsRelative = false;
  32. this.groups = [];
  33. this.boundingBox = null;
  34. this.boundingSphere = null;
  35. this.drawRange = { start: 0, count: Infinity };
  36. this.userData = {};
  37. }
  38. BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  39. constructor: BufferGeometry,
  40. isBufferGeometry: true,
  41. getIndex: function () {
  42. return this.index;
  43. },
  44. setIndex: function ( index ) {
  45. if ( Array.isArray( index ) ) {
  46. this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
  47. } else {
  48. this.index = index;
  49. }
  50. },
  51. getAttribute: function ( name ) {
  52. return this.attributes[ name ];
  53. },
  54. setAttribute: function ( name, attribute ) {
  55. this.attributes[ name ] = attribute;
  56. return this;
  57. },
  58. deleteAttribute: function ( name ) {
  59. delete this.attributes[ name ];
  60. return this;
  61. },
  62. addGroup: function ( start, count, materialIndex ) {
  63. this.groups.push( {
  64. start: start,
  65. count: count,
  66. materialIndex: materialIndex !== undefined ? materialIndex : 0
  67. } );
  68. },
  69. clearGroups: function () {
  70. this.groups = [];
  71. },
  72. setDrawRange: function ( start, count ) {
  73. this.drawRange.start = start;
  74. this.drawRange.count = count;
  75. },
  76. applyMatrix4: function ( matrix ) {
  77. const position = this.attributes.position;
  78. if ( position !== undefined ) {
  79. position.applyMatrix4( matrix );
  80. position.needsUpdate = true;
  81. }
  82. const normal = this.attributes.normal;
  83. if ( normal !== undefined ) {
  84. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  85. normal.applyNormalMatrix( normalMatrix );
  86. normal.needsUpdate = true;
  87. }
  88. const tangent = this.attributes.tangent;
  89. if ( tangent !== undefined ) {
  90. tangent.transformDirection( matrix );
  91. tangent.needsUpdate = true;
  92. }
  93. if ( this.boundingBox !== null ) {
  94. this.computeBoundingBox();
  95. }
  96. if ( this.boundingSphere !== null ) {
  97. this.computeBoundingSphere();
  98. }
  99. return this;
  100. },
  101. rotateX: function ( angle ) {
  102. // rotate geometry around world x-axis
  103. _m1.makeRotationX( angle );
  104. this.applyMatrix4( _m1 );
  105. return this;
  106. },
  107. rotateY: function ( angle ) {
  108. // rotate geometry around world y-axis
  109. _m1.makeRotationY( angle );
  110. this.applyMatrix4( _m1 );
  111. return this;
  112. },
  113. rotateZ: function ( angle ) {
  114. // rotate geometry around world z-axis
  115. _m1.makeRotationZ( angle );
  116. this.applyMatrix4( _m1 );
  117. return this;
  118. },
  119. translate: function ( x, y, z ) {
  120. // translate geometry
  121. _m1.makeTranslation( x, y, z );
  122. this.applyMatrix4( _m1 );
  123. return this;
  124. },
  125. scale: function ( x, y, z ) {
  126. // scale geometry
  127. _m1.makeScale( x, y, z );
  128. this.applyMatrix4( _m1 );
  129. return this;
  130. },
  131. lookAt: function ( vector ) {
  132. _obj.lookAt( vector );
  133. _obj.updateMatrix();
  134. this.applyMatrix4( _obj.matrix );
  135. return this;
  136. },
  137. center: function () {
  138. this.computeBoundingBox();
  139. this.boundingBox.getCenter( _offset ).negate();
  140. this.translate( _offset.x, _offset.y, _offset.z );
  141. return this;
  142. },
  143. setFromObject: function ( object ) {
  144. // console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
  145. const geometry = object.geometry;
  146. if ( object.isPoints || object.isLine ) {
  147. const positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  148. const colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
  149. this.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  150. this.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  151. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  152. const lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
  153. this.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  154. }
  155. if ( geometry.boundingSphere !== null ) {
  156. this.boundingSphere = geometry.boundingSphere.clone();
  157. }
  158. if ( geometry.boundingBox !== null ) {
  159. this.boundingBox = geometry.boundingBox.clone();
  160. }
  161. } else if ( object.isMesh ) {
  162. if ( geometry && geometry.isGeometry ) {
  163. this.fromGeometry( geometry );
  164. }
  165. }
  166. return this;
  167. },
  168. setFromPoints: function ( points ) {
  169. const position = [];
  170. for ( let i = 0, l = points.length; i < l; i ++ ) {
  171. const point = points[ i ];
  172. position.push( point.x, point.y, point.z || 0 );
  173. }
  174. this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  175. return this;
  176. },
  177. updateFromObject: function ( object ) {
  178. let geometry = object.geometry;
  179. if ( object.isMesh ) {
  180. let direct = geometry.__directGeometry;
  181. if ( geometry.elementsNeedUpdate === true ) {
  182. direct = undefined;
  183. geometry.elementsNeedUpdate = false;
  184. }
  185. if ( direct === undefined ) {
  186. return this.fromGeometry( geometry );
  187. }
  188. direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
  189. direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
  190. direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
  191. direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
  192. direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
  193. geometry.verticesNeedUpdate = false;
  194. geometry.normalsNeedUpdate = false;
  195. geometry.colorsNeedUpdate = false;
  196. geometry.uvsNeedUpdate = false;
  197. geometry.groupsNeedUpdate = false;
  198. geometry = direct;
  199. }
  200. if ( geometry.verticesNeedUpdate === true ) {
  201. const attribute = this.attributes.position;
  202. if ( attribute !== undefined ) {
  203. attribute.copyVector3sArray( geometry.vertices );
  204. attribute.needsUpdate = true;
  205. }
  206. geometry.verticesNeedUpdate = false;
  207. }
  208. if ( geometry.normalsNeedUpdate === true ) {
  209. const attribute = this.attributes.normal;
  210. if ( attribute !== undefined ) {
  211. attribute.copyVector3sArray( geometry.normals );
  212. attribute.needsUpdate = true;
  213. }
  214. geometry.normalsNeedUpdate = false;
  215. }
  216. if ( geometry.colorsNeedUpdate === true ) {
  217. const attribute = this.attributes.color;
  218. if ( attribute !== undefined ) {
  219. attribute.copyColorsArray( geometry.colors );
  220. attribute.needsUpdate = true;
  221. }
  222. geometry.colorsNeedUpdate = false;
  223. }
  224. if ( geometry.uvsNeedUpdate ) {
  225. const attribute = this.attributes.uv;
  226. if ( attribute !== undefined ) {
  227. attribute.copyVector2sArray( geometry.uvs );
  228. attribute.needsUpdate = true;
  229. }
  230. geometry.uvsNeedUpdate = false;
  231. }
  232. if ( geometry.lineDistancesNeedUpdate ) {
  233. const attribute = this.attributes.lineDistance;
  234. if ( attribute !== undefined ) {
  235. attribute.copyArray( geometry.lineDistances );
  236. attribute.needsUpdate = true;
  237. }
  238. geometry.lineDistancesNeedUpdate = false;
  239. }
  240. if ( geometry.groupsNeedUpdate ) {
  241. geometry.computeGroups( object.geometry );
  242. this.groups = geometry.groups;
  243. geometry.groupsNeedUpdate = false;
  244. }
  245. return this;
  246. },
  247. fromGeometry: function ( geometry ) {
  248. geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
  249. return this.fromDirectGeometry( geometry.__directGeometry );
  250. },
  251. fromDirectGeometry: function ( geometry ) {
  252. const positions = new Float32Array( geometry.vertices.length * 3 );
  253. this.setAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  254. if ( geometry.normals.length > 0 ) {
  255. const normals = new Float32Array( geometry.normals.length * 3 );
  256. this.setAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  257. }
  258. if ( geometry.colors.length > 0 ) {
  259. const colors = new Float32Array( geometry.colors.length * 3 );
  260. this.setAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  261. }
  262. if ( geometry.uvs.length > 0 ) {
  263. const uvs = new Float32Array( geometry.uvs.length * 2 );
  264. this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  265. }
  266. if ( geometry.uvs2.length > 0 ) {
  267. const uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  268. this.setAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  269. }
  270. // groups
  271. this.groups = geometry.groups;
  272. // morphs
  273. for ( const name in geometry.morphTargets ) {
  274. const array = [];
  275. const morphTargets = geometry.morphTargets[ name ];
  276. for ( let i = 0, l = morphTargets.length; i < l; i ++ ) {
  277. const morphTarget = morphTargets[ i ];
  278. const attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 );
  279. attribute.name = morphTarget.name;
  280. array.push( attribute.copyVector3sArray( morphTarget.data ) );
  281. }
  282. this.morphAttributes[ name ] = array;
  283. }
  284. // skinning
  285. if ( geometry.skinIndices.length > 0 ) {
  286. const skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  287. this.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  288. }
  289. if ( geometry.skinWeights.length > 0 ) {
  290. const skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  291. this.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  292. }
  293. //
  294. if ( geometry.boundingSphere !== null ) {
  295. this.boundingSphere = geometry.boundingSphere.clone();
  296. }
  297. if ( geometry.boundingBox !== null ) {
  298. this.boundingBox = geometry.boundingBox.clone();
  299. }
  300. return this;
  301. },
  302. computeBoundingBox: function () {
  303. if ( this.boundingBox === null ) {
  304. this.boundingBox = new Box3();
  305. }
  306. const position = this.attributes.position;
  307. const morphAttributesPosition = this.morphAttributes.position;
  308. if ( position !== undefined ) {
  309. this.boundingBox.setFromBufferAttribute( position );
  310. // process morph attributes if present
  311. if ( morphAttributesPosition ) {
  312. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  313. const morphAttribute = morphAttributesPosition[ i ];
  314. _box.setFromBufferAttribute( morphAttribute );
  315. if ( this.morphTargetsRelative ) {
  316. _vector.addVectors( this.boundingBox.min, _box.min );
  317. this.boundingBox.expandByPoint( _vector );
  318. _vector.addVectors( this.boundingBox.max, _box.max );
  319. this.boundingBox.expandByPoint( _vector );
  320. } else {
  321. this.boundingBox.expandByPoint( _box.min );
  322. this.boundingBox.expandByPoint( _box.max );
  323. }
  324. }
  325. }
  326. } else {
  327. this.boundingBox.makeEmpty();
  328. }
  329. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  330. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  331. }
  332. },
  333. computeBoundingSphere: function () {
  334. if ( this.boundingSphere === null ) {
  335. this.boundingSphere = new Sphere();
  336. }
  337. const position = this.attributes.position;
  338. const morphAttributesPosition = this.morphAttributes.position;
  339. if ( position ) {
  340. // first, find the center of the bounding sphere
  341. const center = this.boundingSphere.center;
  342. _box.setFromBufferAttribute( position );
  343. // process morph attributes if present
  344. if ( morphAttributesPosition ) {
  345. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  346. const morphAttribute = morphAttributesPosition[ i ];
  347. _boxMorphTargets.setFromBufferAttribute( morphAttribute );
  348. if ( this.morphTargetsRelative ) {
  349. _vector.addVectors( _box.min, _boxMorphTargets.min );
  350. _box.expandByPoint( _vector );
  351. _vector.addVectors( _box.max, _boxMorphTargets.max );
  352. _box.expandByPoint( _vector );
  353. } else {
  354. _box.expandByPoint( _boxMorphTargets.min );
  355. _box.expandByPoint( _boxMorphTargets.max );
  356. }
  357. }
  358. }
  359. _box.getCenter( center );
  360. // second, try to find a boundingSphere with a radius smaller than the
  361. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  362. let maxRadiusSq = 0;
  363. for ( let i = 0, il = position.count; i < il; i ++ ) {
  364. _vector.fromBufferAttribute( position, i );
  365. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  366. }
  367. // process morph attributes if present
  368. if ( morphAttributesPosition ) {
  369. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  370. const morphAttribute = morphAttributesPosition[ i ];
  371. const morphTargetsRelative = this.morphTargetsRelative;
  372. for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
  373. _vector.fromBufferAttribute( morphAttribute, j );
  374. if ( morphTargetsRelative ) {
  375. _offset.fromBufferAttribute( position, j );
  376. _vector.add( _offset );
  377. }
  378. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  379. }
  380. }
  381. }
  382. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  383. if ( isNaN( this.boundingSphere.radius ) ) {
  384. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  385. }
  386. }
  387. },
  388. computeFaceNormals: function () {
  389. // backwards compatibility
  390. },
  391. computeVertexNormals: function () {
  392. const index = this.index;
  393. const positionAttribute = this.getAttribute( 'position' );
  394. if ( positionAttribute !== undefined ) {
  395. let normalAttribute = this.getAttribute( 'normal' );
  396. if ( normalAttribute === undefined ) {
  397. normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
  398. this.setAttribute( 'normal', normalAttribute );
  399. } else {
  400. // reset existing normals to zero
  401. for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
  402. normalAttribute.setXYZ( i, 0, 0, 0 );
  403. }
  404. }
  405. const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  406. const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
  407. const cb = new Vector3(), ab = new Vector3();
  408. // indexed elements
  409. if ( index ) {
  410. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  411. const vA = index.getX( i + 0 );
  412. const vB = index.getX( i + 1 );
  413. const vC = index.getX( i + 2 );
  414. pA.fromBufferAttribute( positionAttribute, vA );
  415. pB.fromBufferAttribute( positionAttribute, vB );
  416. pC.fromBufferAttribute( positionAttribute, vC );
  417. cb.subVectors( pC, pB );
  418. ab.subVectors( pA, pB );
  419. cb.cross( ab );
  420. nA.fromBufferAttribute( normalAttribute, vA );
  421. nB.fromBufferAttribute( normalAttribute, vB );
  422. nC.fromBufferAttribute( normalAttribute, vC );
  423. nA.add( cb );
  424. nB.add( cb );
  425. nC.add( cb );
  426. normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
  427. normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
  428. normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
  429. }
  430. } else {
  431. // non-indexed elements (unconnected triangle soup)
  432. for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
  433. pA.fromBufferAttribute( positionAttribute, i + 0 );
  434. pB.fromBufferAttribute( positionAttribute, i + 1 );
  435. pC.fromBufferAttribute( positionAttribute, i + 2 );
  436. cb.subVectors( pC, pB );
  437. ab.subVectors( pA, pB );
  438. cb.cross( ab );
  439. normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
  440. normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
  441. normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
  442. }
  443. }
  444. this.normalizeNormals();
  445. normalAttribute.needsUpdate = true;
  446. }
  447. },
  448. merge: function ( geometry, offset ) {
  449. if ( ! ( geometry && geometry.isBufferGeometry ) ) {
  450. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  451. return;
  452. }
  453. if ( offset === undefined ) {
  454. offset = 0;
  455. console.warn(
  456. 'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. '
  457. + 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.'
  458. );
  459. }
  460. const attributes = this.attributes;
  461. for ( const key in attributes ) {
  462. if ( geometry.attributes[ key ] === undefined ) continue;
  463. const attribute1 = attributes[ key ];
  464. const attributeArray1 = attribute1.array;
  465. const attribute2 = geometry.attributes[ key ];
  466. const attributeArray2 = attribute2.array;
  467. const attributeOffset = attribute2.itemSize * offset;
  468. const length = Math.min( attributeArray2.length, attributeArray1.length - attributeOffset );
  469. for ( let i = 0, j = attributeOffset; i < length; i ++, j ++ ) {
  470. attributeArray1[ j ] = attributeArray2[ i ];
  471. }
  472. }
  473. return this;
  474. },
  475. normalizeNormals: function () {
  476. const normals = this.attributes.normal;
  477. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  478. _vector.fromBufferAttribute( normals, i );
  479. _vector.normalize();
  480. normals.setXYZ( i, _vector.x, _vector.y, _vector.z );
  481. }
  482. },
  483. toNonIndexed: function () {
  484. function convertBufferAttribute( attribute, indices ) {
  485. const array = attribute.array;
  486. const itemSize = attribute.itemSize;
  487. const normalized = attribute.normalized;
  488. const array2 = new array.constructor( indices.length * itemSize );
  489. let index = 0, index2 = 0;
  490. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  491. index = indices[ i ] * itemSize;
  492. for ( let j = 0; j < itemSize; j ++ ) {
  493. array2[ index2 ++ ] = array[ index ++ ];
  494. }
  495. }
  496. return new BufferAttribute( array2, itemSize, normalized );
  497. }
  498. //
  499. if ( this.index === null ) {
  500. console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
  501. return this;
  502. }
  503. const geometry2 = new BufferGeometry();
  504. const indices = this.index.array;
  505. const attributes = this.attributes;
  506. // attributes
  507. for ( const name in attributes ) {
  508. const attribute = attributes[ name ];
  509. const newAttribute = convertBufferAttribute( attribute, indices );
  510. geometry2.setAttribute( name, newAttribute );
  511. }
  512. // morph attributes
  513. const morphAttributes = this.morphAttributes;
  514. for ( const name in morphAttributes ) {
  515. const morphArray = [];
  516. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  517. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  518. const attribute = morphAttribute[ i ];
  519. const newAttribute = convertBufferAttribute( attribute, indices );
  520. morphArray.push( newAttribute );
  521. }
  522. geometry2.morphAttributes[ name ] = morphArray;
  523. }
  524. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  525. // groups
  526. const groups = this.groups;
  527. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  528. const group = groups[ i ];
  529. geometry2.addGroup( group.start, group.count, group.materialIndex );
  530. }
  531. return geometry2;
  532. },
  533. toJSON: function () {
  534. const data = {
  535. metadata: {
  536. version: 4.5,
  537. type: 'BufferGeometry',
  538. generator: 'BufferGeometry.toJSON'
  539. }
  540. };
  541. // standard BufferGeometry serialization
  542. data.uuid = this.uuid;
  543. data.type = this.type;
  544. if ( this.name !== '' ) data.name = this.name;
  545. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  546. if ( this.parameters !== undefined ) {
  547. const parameters = this.parameters;
  548. for ( const key in parameters ) {
  549. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  550. }
  551. return data;
  552. }
  553. data.data = { attributes: {} };
  554. const index = this.index;
  555. if ( index !== null ) {
  556. data.data.index = {
  557. type: index.array.constructor.name,
  558. array: Array.prototype.slice.call( index.array )
  559. };
  560. }
  561. const attributes = this.attributes;
  562. for ( const key in attributes ) {
  563. const attribute = attributes[ key ];
  564. const attributeData = attribute.toJSON( data.data );
  565. if ( attribute.name !== '' ) attributeData.name = attribute.name;
  566. data.data.attributes[ key ] = attributeData;
  567. }
  568. const morphAttributes = {};
  569. let hasMorphAttributes = false;
  570. for ( const key in this.morphAttributes ) {
  571. const attributeArray = this.morphAttributes[ key ];
  572. const array = [];
  573. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  574. const attribute = attributeArray[ i ];
  575. const attributeData = attribute.toJSON( data.data );
  576. if ( attribute.name !== '' ) attributeData.name = attribute.name;
  577. array.push( attributeData );
  578. }
  579. if ( array.length > 0 ) {
  580. morphAttributes[ key ] = array;
  581. hasMorphAttributes = true;
  582. }
  583. }
  584. if ( hasMorphAttributes ) {
  585. data.data.morphAttributes = morphAttributes;
  586. data.data.morphTargetsRelative = this.morphTargetsRelative;
  587. }
  588. const groups = this.groups;
  589. if ( groups.length > 0 ) {
  590. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  591. }
  592. const boundingSphere = this.boundingSphere;
  593. if ( boundingSphere !== null ) {
  594. data.data.boundingSphere = {
  595. center: boundingSphere.center.toArray(),
  596. radius: boundingSphere.radius
  597. };
  598. }
  599. return data;
  600. },
  601. clone: function () {
  602. /*
  603. // Handle primitives
  604. const parameters = this.parameters;
  605. if ( parameters !== undefined ) {
  606. const values = [];
  607. for ( const key in parameters ) {
  608. values.push( parameters[ key ] );
  609. }
  610. const geometry = Object.create( this.constructor.prototype );
  611. this.constructor.apply( geometry, values );
  612. return geometry;
  613. }
  614. return new this.constructor().copy( this );
  615. */
  616. return new BufferGeometry().copy( this );
  617. },
  618. copy: function ( source ) {
  619. // reset
  620. this.index = null;
  621. this.attributes = {};
  622. this.morphAttributes = {};
  623. this.groups = [];
  624. this.boundingBox = null;
  625. this.boundingSphere = null;
  626. // used for storing cloned, shared data
  627. const data = {};
  628. // name
  629. this.name = source.name;
  630. // index
  631. const index = source.index;
  632. if ( index !== null ) {
  633. this.setIndex( index.clone( data ) );
  634. }
  635. // attributes
  636. const attributes = source.attributes;
  637. for ( const name in attributes ) {
  638. const attribute = attributes[ name ];
  639. this.setAttribute( name, attribute.clone( data ) );
  640. }
  641. // morph attributes
  642. const morphAttributes = source.morphAttributes;
  643. for ( const name in morphAttributes ) {
  644. const array = [];
  645. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  646. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  647. array.push( morphAttribute[ i ].clone( data ) );
  648. }
  649. this.morphAttributes[ name ] = array;
  650. }
  651. this.morphTargetsRelative = source.morphTargetsRelative;
  652. // groups
  653. const groups = source.groups;
  654. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  655. const group = groups[ i ];
  656. this.addGroup( group.start, group.count, group.materialIndex );
  657. }
  658. // bounding box
  659. const boundingBox = source.boundingBox;
  660. if ( boundingBox !== null ) {
  661. this.boundingBox = boundingBox.clone();
  662. }
  663. // bounding sphere
  664. const boundingSphere = source.boundingSphere;
  665. if ( boundingSphere !== null ) {
  666. this.boundingSphere = boundingSphere.clone();
  667. }
  668. // draw range
  669. this.drawRange.start = source.drawRange.start;
  670. this.drawRange.count = source.drawRange.count;
  671. // user data
  672. this.userData = source.userData;
  673. return this;
  674. },
  675. dispose: function () {
  676. this.dispatchEvent( { type: 'dispose' } );
  677. }
  678. } );
  679. export { BufferGeometry };
粤ICP备19079148号