BufferGeometry.js 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. if ( this.boundingBox !== null ) {
  91. this.computeBoundingBox();
  92. }
  93. if ( this.boundingSphere !== null ) {
  94. this.computeBoundingSphere();
  95. }
  96. return this;
  97. },
  98. rotateX: function () {
  99. // rotate geometry around world x-axis
  100. var m1 = new Matrix4();
  101. return function rotateX( angle ) {
  102. m1.makeRotationX( angle );
  103. this.applyMatrix( m1 );
  104. return this;
  105. };
  106. }(),
  107. rotateY: function () {
  108. // rotate geometry around world y-axis
  109. var m1 = new Matrix4();
  110. return function rotateY( angle ) {
  111. m1.makeRotationY( angle );
  112. this.applyMatrix( m1 );
  113. return this;
  114. };
  115. }(),
  116. rotateZ: function () {
  117. // rotate geometry around world z-axis
  118. var m1 = new Matrix4();
  119. return function rotateZ( angle ) {
  120. m1.makeRotationZ( angle );
  121. this.applyMatrix( m1 );
  122. return this;
  123. };
  124. }(),
  125. translate: function () {
  126. // translate geometry
  127. var m1 = new Matrix4();
  128. return function translate( x, y, z ) {
  129. m1.makeTranslation( x, y, z );
  130. this.applyMatrix( m1 );
  131. return this;
  132. };
  133. }(),
  134. scale: function () {
  135. // scale geometry
  136. var m1 = new Matrix4();
  137. return function scale( x, y, z ) {
  138. m1.makeScale( x, y, z );
  139. this.applyMatrix( m1 );
  140. return this;
  141. };
  142. }(),
  143. lookAt: function () {
  144. var obj = new Object3D();
  145. return function lookAt( vector ) {
  146. obj.lookAt( vector );
  147. obj.updateMatrix();
  148. this.applyMatrix( obj.matrix );
  149. };
  150. }(),
  151. center: function () {
  152. var offset = new Vector3();
  153. return function center() {
  154. this.computeBoundingBox();
  155. this.boundingBox.getCenter( offset ).negate();
  156. this.translate( offset.x, offset.y, offset.z );
  157. return this;
  158. };
  159. }(),
  160. setFromObject: function ( object ) {
  161. // console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
  162. var geometry = object.geometry;
  163. if ( object.isPoints || object.isLine ) {
  164. var positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  165. var colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
  166. this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  167. this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  168. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  169. var lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
  170. this.addAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  171. }
  172. if ( geometry.boundingSphere !== null ) {
  173. this.boundingSphere = geometry.boundingSphere.clone();
  174. }
  175. if ( geometry.boundingBox !== null ) {
  176. this.boundingBox = geometry.boundingBox.clone();
  177. }
  178. } else if ( object.isMesh ) {
  179. if ( geometry && geometry.isGeometry ) {
  180. this.fromGeometry( geometry );
  181. }
  182. }
  183. return this;
  184. },
  185. setFromPoints: function ( points ) {
  186. var position = [];
  187. for ( var i = 0, l = points.length; i < l; i ++ ) {
  188. var point = points[ i ];
  189. position.push( point.x, point.y, point.z || 0 );
  190. }
  191. this.addAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  192. return this;
  193. },
  194. updateFromObject: function ( object ) {
  195. var geometry = object.geometry;
  196. if ( object.isMesh ) {
  197. var direct = geometry.__directGeometry;
  198. if ( geometry.elementsNeedUpdate === true ) {
  199. direct = undefined;
  200. geometry.elementsNeedUpdate = false;
  201. }
  202. if ( direct === undefined ) {
  203. return this.fromGeometry( geometry );
  204. }
  205. direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
  206. direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
  207. direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
  208. direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
  209. direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
  210. geometry.verticesNeedUpdate = false;
  211. geometry.normalsNeedUpdate = false;
  212. geometry.colorsNeedUpdate = false;
  213. geometry.uvsNeedUpdate = false;
  214. geometry.groupsNeedUpdate = false;
  215. geometry = direct;
  216. }
  217. var attribute;
  218. if ( geometry.verticesNeedUpdate === true ) {
  219. attribute = this.attributes.position;
  220. if ( attribute !== undefined ) {
  221. attribute.copyVector3sArray( geometry.vertices );
  222. attribute.needsUpdate = true;
  223. }
  224. geometry.verticesNeedUpdate = false;
  225. }
  226. if ( geometry.normalsNeedUpdate === true ) {
  227. attribute = this.attributes.normal;
  228. if ( attribute !== undefined ) {
  229. attribute.copyVector3sArray( geometry.normals );
  230. attribute.needsUpdate = true;
  231. }
  232. geometry.normalsNeedUpdate = false;
  233. }
  234. if ( geometry.colorsNeedUpdate === true ) {
  235. attribute = this.attributes.color;
  236. if ( attribute !== undefined ) {
  237. attribute.copyColorsArray( geometry.colors );
  238. attribute.needsUpdate = true;
  239. }
  240. geometry.colorsNeedUpdate = false;
  241. }
  242. if ( geometry.uvsNeedUpdate ) {
  243. attribute = this.attributes.uv;
  244. if ( attribute !== undefined ) {
  245. attribute.copyVector2sArray( geometry.uvs );
  246. attribute.needsUpdate = true;
  247. }
  248. geometry.uvsNeedUpdate = false;
  249. }
  250. if ( geometry.lineDistancesNeedUpdate ) {
  251. attribute = this.attributes.lineDistance;
  252. if ( attribute !== undefined ) {
  253. attribute.copyArray( geometry.lineDistances );
  254. attribute.needsUpdate = true;
  255. }
  256. geometry.lineDistancesNeedUpdate = false;
  257. }
  258. if ( geometry.groupsNeedUpdate ) {
  259. geometry.computeGroups( object.geometry );
  260. this.groups = geometry.groups;
  261. geometry.groupsNeedUpdate = false;
  262. }
  263. return this;
  264. },
  265. fromGeometry: function ( geometry ) {
  266. geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
  267. return this.fromDirectGeometry( geometry.__directGeometry );
  268. },
  269. fromDirectGeometry: function ( geometry ) {
  270. var positions = new Float32Array( geometry.vertices.length * 3 );
  271. this.addAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  272. if ( geometry.normals.length > 0 ) {
  273. var normals = new Float32Array( geometry.normals.length * 3 );
  274. this.addAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  275. }
  276. if ( geometry.colors.length > 0 ) {
  277. var colors = new Float32Array( geometry.colors.length * 3 );
  278. this.addAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  279. }
  280. if ( geometry.uvs.length > 0 ) {
  281. var uvs = new Float32Array( geometry.uvs.length * 2 );
  282. this.addAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  283. }
  284. if ( geometry.uvs2.length > 0 ) {
  285. var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  286. this.addAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  287. }
  288. // groups
  289. this.groups = geometry.groups;
  290. // morphs
  291. for ( var name in geometry.morphTargets ) {
  292. var array = [];
  293. var morphTargets = geometry.morphTargets[ name ];
  294. for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
  295. var morphTarget = morphTargets[ i ];
  296. var attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 );
  297. attribute.name = morphTarget.name;
  298. array.push( attribute.copyVector3sArray( morphTarget.data ) );
  299. }
  300. this.morphAttributes[ name ] = array;
  301. }
  302. // skinning
  303. if ( geometry.skinIndices.length > 0 ) {
  304. var skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  305. this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  306. }
  307. if ( geometry.skinWeights.length > 0 ) {
  308. var skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  309. this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  310. }
  311. //
  312. if ( geometry.boundingSphere !== null ) {
  313. this.boundingSphere = geometry.boundingSphere.clone();
  314. }
  315. if ( geometry.boundingBox !== null ) {
  316. this.boundingBox = geometry.boundingBox.clone();
  317. }
  318. return this;
  319. },
  320. computeBoundingBox: function () {
  321. if ( this.boundingBox === null ) {
  322. this.boundingBox = new Box3();
  323. }
  324. var position = this.attributes.position;
  325. if ( position !== undefined ) {
  326. this.boundingBox.setFromBufferAttribute( position );
  327. } else {
  328. this.boundingBox.makeEmpty();
  329. }
  330. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  331. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  332. }
  333. },
  334. computeBoundingSphere: function () {
  335. var box = new Box3();
  336. var vector = new Vector3();
  337. return function computeBoundingSphere() {
  338. if ( this.boundingSphere === null ) {
  339. this.boundingSphere = new Sphere();
  340. }
  341. var position = this.attributes.position;
  342. if ( position ) {
  343. var center = this.boundingSphere.center;
  344. box.setFromBufferAttribute( position );
  345. box.getCenter( center );
  346. // hoping to find a boundingSphere with a radius smaller than the
  347. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  348. var maxRadiusSq = 0;
  349. for ( var i = 0, il = position.count; i < il; i ++ ) {
  350. vector.x = position.getX( i );
  351. vector.y = position.getY( i );
  352. vector.z = position.getZ( i );
  353. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  354. }
  355. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  356. if ( isNaN( this.boundingSphere.radius ) ) {
  357. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  358. }
  359. }
  360. };
  361. }(),
  362. computeFaceNormals: function () {
  363. // backwards compatibility
  364. },
  365. computeVertexNormals: function () {
  366. var index = this.index;
  367. var attributes = this.attributes;
  368. var groups = this.groups;
  369. if ( attributes.position ) {
  370. var positions = attributes.position.array;
  371. if ( attributes.normal === undefined ) {
  372. this.addAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
  373. } else {
  374. // reset existing normals to zero
  375. var array = attributes.normal.array;
  376. for ( var i = 0, il = array.length; i < il; i ++ ) {
  377. array[ i ] = 0;
  378. }
  379. }
  380. var normals = attributes.normal.array;
  381. var vA, vB, vC;
  382. var pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  383. var cb = new Vector3(), ab = new Vector3();
  384. // indexed elements
  385. if ( index ) {
  386. var indices = index.array;
  387. if ( groups.length === 0 ) {
  388. this.addGroup( 0, indices.length );
  389. }
  390. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  391. var group = groups[ j ];
  392. var start = group.start;
  393. var count = group.count;
  394. for ( var i = start, il = start + count; i < il; i += 3 ) {
  395. vA = indices[ i + 0 ] * 3;
  396. vB = indices[ i + 1 ] * 3;
  397. vC = indices[ i + 2 ] * 3;
  398. pA.fromArray( positions, vA );
  399. pB.fromArray( positions, vB );
  400. pC.fromArray( positions, vC );
  401. cb.subVectors( pC, pB );
  402. ab.subVectors( pA, pB );
  403. cb.cross( ab );
  404. normals[ vA ] += cb.x;
  405. normals[ vA + 1 ] += cb.y;
  406. normals[ vA + 2 ] += cb.z;
  407. normals[ vB ] += cb.x;
  408. normals[ vB + 1 ] += cb.y;
  409. normals[ vB + 2 ] += cb.z;
  410. normals[ vC ] += cb.x;
  411. normals[ vC + 1 ] += cb.y;
  412. normals[ vC + 2 ] += cb.z;
  413. }
  414. }
  415. } else {
  416. // non-indexed elements (unconnected triangle soup)
  417. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  418. pA.fromArray( positions, i );
  419. pB.fromArray( positions, i + 3 );
  420. pC.fromArray( positions, i + 6 );
  421. cb.subVectors( pC, pB );
  422. ab.subVectors( pA, pB );
  423. cb.cross( ab );
  424. normals[ i ] = cb.x;
  425. normals[ i + 1 ] = cb.y;
  426. normals[ i + 2 ] = cb.z;
  427. normals[ i + 3 ] = cb.x;
  428. normals[ i + 4 ] = cb.y;
  429. normals[ i + 5 ] = cb.z;
  430. normals[ i + 6 ] = cb.x;
  431. normals[ i + 7 ] = cb.y;
  432. normals[ i + 8 ] = cb.z;
  433. }
  434. }
  435. this.normalizeNormals();
  436. attributes.normal.needsUpdate = true;
  437. }
  438. },
  439. merge: function ( geometry, offset ) {
  440. if ( ! ( geometry && geometry.isBufferGeometry ) ) {
  441. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  442. return;
  443. }
  444. if ( offset === undefined ) {
  445. offset = 0;
  446. console.warn(
  447. 'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. '
  448. + 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.'
  449. );
  450. }
  451. var attributes = this.attributes;
  452. for ( var key in attributes ) {
  453. if ( geometry.attributes[ key ] === undefined ) continue;
  454. var attribute1 = attributes[ key ];
  455. var attributeArray1 = attribute1.array;
  456. var attribute2 = geometry.attributes[ key ];
  457. var attributeArray2 = attribute2.array;
  458. var attributeSize = attribute2.itemSize;
  459. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  460. attributeArray1[ j ] = attributeArray2[ i ];
  461. }
  462. }
  463. return this;
  464. },
  465. normalizeNormals: function () {
  466. var vector = new Vector3();
  467. return function normalizeNormals() {
  468. var normals = this.attributes.normal;
  469. for ( var i = 0, il = normals.count; i < il; i ++ ) {
  470. vector.x = normals.getX( i );
  471. vector.y = normals.getY( i );
  472. vector.z = normals.getZ( i );
  473. vector.normalize();
  474. normals.setXYZ( i, vector.x, vector.y, vector.z );
  475. }
  476. };
  477. }(),
  478. toNonIndexed: function () {
  479. if ( this.index === null ) {
  480. console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
  481. return this;
  482. }
  483. var geometry2 = new BufferGeometry();
  484. var indices = this.index.array;
  485. var attributes = this.attributes;
  486. for ( var name in attributes ) {
  487. var attribute = attributes[ name ];
  488. var array = attribute.array;
  489. var itemSize = attribute.itemSize;
  490. var array2 = new array.constructor( indices.length * itemSize );
  491. var index = 0, index2 = 0;
  492. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  493. index = indices[ i ] * itemSize;
  494. for ( var j = 0; j < itemSize; j ++ ) {
  495. array2[ index2 ++ ] = array[ index ++ ];
  496. }
  497. }
  498. geometry2.addAttribute( name, new BufferAttribute( array2, itemSize ) );
  499. }
  500. var groups = this.groups;
  501. for ( var i = 0, l = groups.length; i < l; i ++ ) {
  502. var group = groups[ i ];
  503. geometry2.addGroup( group.start, group.count, group.materialIndex );
  504. }
  505. return geometry2;
  506. },
  507. toJSON: function () {
  508. var data = {
  509. metadata: {
  510. version: 4.5,
  511. type: 'BufferGeometry',
  512. generator: 'BufferGeometry.toJSON'
  513. }
  514. };
  515. // standard BufferGeometry serialization
  516. data.uuid = this.uuid;
  517. data.type = this.type;
  518. if ( this.name !== '' ) data.name = this.name;
  519. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  520. if ( this.parameters !== undefined ) {
  521. var parameters = this.parameters;
  522. for ( var key in parameters ) {
  523. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  524. }
  525. return data;
  526. }
  527. data.data = { attributes: {} };
  528. var index = this.index;
  529. if ( index !== null ) {
  530. var array = Array.prototype.slice.call( index.array );
  531. data.data.index = {
  532. type: index.array.constructor.name,
  533. array: array
  534. };
  535. }
  536. var attributes = this.attributes;
  537. for ( var key in attributes ) {
  538. var attribute = attributes[ key ];
  539. var array = Array.prototype.slice.call( attribute.array );
  540. data.data.attributes[ key ] = {
  541. itemSize: attribute.itemSize,
  542. type: attribute.array.constructor.name,
  543. array: array,
  544. normalized: attribute.normalized
  545. };
  546. }
  547. var groups = this.groups;
  548. if ( groups.length > 0 ) {
  549. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  550. }
  551. var boundingSphere = this.boundingSphere;
  552. if ( boundingSphere !== null ) {
  553. data.data.boundingSphere = {
  554. center: boundingSphere.center.toArray(),
  555. radius: boundingSphere.radius
  556. };
  557. }
  558. return data;
  559. },
  560. clone: function () {
  561. /*
  562. // Handle primitives
  563. var parameters = this.parameters;
  564. if ( parameters !== undefined ) {
  565. var values = [];
  566. for ( var key in parameters ) {
  567. values.push( parameters[ key ] );
  568. }
  569. var geometry = Object.create( this.constructor.prototype );
  570. this.constructor.apply( geometry, values );
  571. return geometry;
  572. }
  573. return new this.constructor().copy( this );
  574. */
  575. return new BufferGeometry().copy( this );
  576. },
  577. copy: function ( source ) {
  578. var name, i, l;
  579. // reset
  580. this.index = null;
  581. this.attributes = {};
  582. this.morphAttributes = {};
  583. this.groups = [];
  584. this.boundingBox = null;
  585. this.boundingSphere = null;
  586. // name
  587. this.name = source.name;
  588. // index
  589. var index = source.index;
  590. if ( index !== null ) {
  591. this.setIndex( index.clone() );
  592. }
  593. // attributes
  594. var attributes = source.attributes;
  595. for ( name in attributes ) {
  596. var attribute = attributes[ name ];
  597. this.addAttribute( name, attribute.clone() );
  598. }
  599. // morph attributes
  600. var morphAttributes = source.morphAttributes;
  601. for ( name in morphAttributes ) {
  602. var array = [];
  603. var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  604. for ( i = 0, l = morphAttribute.length; i < l; i ++ ) {
  605. array.push( morphAttribute[ i ].clone() );
  606. }
  607. this.morphAttributes[ name ] = array;
  608. }
  609. // groups
  610. var groups = source.groups;
  611. for ( i = 0, l = groups.length; i < l; i ++ ) {
  612. var group = groups[ i ];
  613. this.addGroup( group.start, group.count, group.materialIndex );
  614. }
  615. // bounding box
  616. var boundingBox = source.boundingBox;
  617. if ( boundingBox !== null ) {
  618. this.boundingBox = boundingBox.clone();
  619. }
  620. // bounding sphere
  621. var boundingSphere = source.boundingSphere;
  622. if ( boundingSphere !== null ) {
  623. this.boundingSphere = boundingSphere.clone();
  624. }
  625. // draw range
  626. this.drawRange.start = source.drawRange.start;
  627. this.drawRange.count = source.drawRange.count;
  628. // user data
  629. this.userData = source.userData;
  630. return this;
  631. },
  632. dispose: function () {
  633. this.dispatchEvent( { type: 'dispose' } );
  634. }
  635. } );
  636. export { BufferGeometry };
粤ICP备19079148号