BufferGeometry.js 24 KB

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