BufferGeometry.js 25 KB

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