BufferGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.BufferGeometry = function () {
  6. Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );
  7. this.uuid = THREE.Math.generateUUID();
  8. this.name = '';
  9. this.type = 'BufferGeometry';
  10. this.index = null;
  11. this.attributes = {};
  12. this.morphAttributes = {};
  13. this.groups = [];
  14. this.boundingBox = null;
  15. this.boundingSphere = null;
  16. this.drawRange = { start: 0, count: Infinity };
  17. };
  18. THREE.BufferGeometry.prototype = {
  19. constructor: THREE.BufferGeometry,
  20. addIndex: function ( index ) {
  21. console.warn( 'THREE.BufferGeometry: .addIndex() has been renamed to .setIndex().' );
  22. this.setIndex( index );
  23. },
  24. getIndex: function () {
  25. return this.index;
  26. },
  27. setIndex: function ( index ) {
  28. this.index = index;
  29. },
  30. addAttribute: function ( name, attribute ) {
  31. if ( attribute instanceof THREE.BufferAttribute === false && attribute instanceof THREE.InterleavedBufferAttribute === false ) {
  32. console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
  33. this.addAttribute( name, new THREE.BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
  34. return;
  35. }
  36. if ( name === 'index' ) {
  37. console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' );
  38. this.setIndex( attribute );
  39. return;
  40. }
  41. this.attributes[ name ] = attribute;
  42. },
  43. getAttribute: function ( name ) {
  44. return this.attributes[ name ];
  45. },
  46. removeAttribute: function ( name ) {
  47. delete this.attributes[ name ];
  48. },
  49. get drawcalls() {
  50. console.error( 'THREE.BufferGeometry: .drawcalls has been renamed to .groups.' );
  51. return this.groups;
  52. },
  53. get offsets() {
  54. console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .groups.' );
  55. return this.groups;
  56. },
  57. addDrawCall: function ( start, count, indexOffset ) {
  58. if ( indexOffset !== undefined ) {
  59. console.warn( 'THREE.BufferGeometry: .addDrawCall() no longer supports indexOffset.' );
  60. }
  61. console.warn( 'THREE.BufferGeometry: .addDrawCall() is now .addGroup().' );
  62. this.addGroup( start, count );
  63. },
  64. clearDrawCalls: function () {
  65. console.warn( 'THREE.BufferGeometry: .clearDrawCalls() is now .clearGroups().' );
  66. this.clearGroups();
  67. },
  68. addGroup: function ( start, count, materialIndex ) {
  69. this.groups.push( {
  70. start: start,
  71. count: count,
  72. materialIndex: materialIndex !== undefined ? materialIndex : 0
  73. } );
  74. },
  75. clearGroups: function () {
  76. this.groups = [];
  77. },
  78. setDrawRange: function ( start, count ) {
  79. this.drawRange.start = start;
  80. this.drawRange.count = count;
  81. },
  82. applyMatrix: function ( matrix ) {
  83. var position = this.attributes.position;
  84. if ( position !== undefined ) {
  85. matrix.applyToVector3Array( position.array );
  86. position.needsUpdate = true;
  87. }
  88. var normal = this.attributes.normal;
  89. if ( normal !== undefined ) {
  90. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  91. normalMatrix.applyToVector3Array( normal.array );
  92. normal.needsUpdate = true;
  93. }
  94. if ( this.boundingBox !== null ) {
  95. this.computeBoundingBox();
  96. }
  97. if ( this.boundingSphere !== null ) {
  98. this.computeBoundingSphere();
  99. }
  100. },
  101. rotateX: function () {
  102. // rotate geometry around world x-axis
  103. var m1;
  104. return function rotateX( angle ) {
  105. if ( m1 === undefined ) m1 = new THREE.Matrix4();
  106. m1.makeRotationX( angle );
  107. this.applyMatrix( m1 );
  108. return this;
  109. };
  110. }(),
  111. rotateY: function () {
  112. // rotate geometry around world y-axis
  113. var m1;
  114. return function rotateY( angle ) {
  115. if ( m1 === undefined ) m1 = new THREE.Matrix4();
  116. m1.makeRotationY( angle );
  117. this.applyMatrix( m1 );
  118. return this;
  119. };
  120. }(),
  121. rotateZ: function () {
  122. // rotate geometry around world z-axis
  123. var m1;
  124. return function rotateZ( angle ) {
  125. if ( m1 === undefined ) m1 = new THREE.Matrix4();
  126. m1.makeRotationZ( angle );
  127. this.applyMatrix( m1 );
  128. return this;
  129. };
  130. }(),
  131. translate: function () {
  132. // translate geometry
  133. var m1;
  134. return function translate( x, y, z ) {
  135. if ( m1 === undefined ) m1 = new THREE.Matrix4();
  136. m1.makeTranslation( x, y, z );
  137. this.applyMatrix( m1 );
  138. return this;
  139. };
  140. }(),
  141. scale: function () {
  142. // scale geometry
  143. var m1;
  144. return function scale( x, y, z ) {
  145. if ( m1 === undefined ) m1 = new THREE.Matrix4();
  146. m1.makeScale( x, y, z );
  147. this.applyMatrix( m1 );
  148. return this;
  149. };
  150. }(),
  151. lookAt: function () {
  152. var obj;
  153. return function lookAt( vector ) {
  154. if ( obj === undefined ) obj = new THREE.Object3D();
  155. obj.lookAt( vector );
  156. obj.updateMatrix();
  157. this.applyMatrix( obj.matrix );
  158. };
  159. }(),
  160. center: function () {
  161. this.computeBoundingBox();
  162. var offset = this.boundingBox.center().negate();
  163. this.translate( offset.x, offset.y, offset.z );
  164. return offset;
  165. },
  166. setFromObject: function ( object ) {
  167. // console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
  168. var geometry = object.geometry;
  169. if ( object instanceof THREE.Points || object instanceof THREE.Line ) {
  170. var positions = new THREE.Float32Attribute( geometry.vertices.length * 3, 3 );
  171. var colors = new THREE.Float32Attribute( geometry.colors.length * 3, 3 );
  172. this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  173. this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  174. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  175. var lineDistances = new THREE.Float32Attribute( geometry.lineDistances.length, 1 );
  176. this.addAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  177. }
  178. if ( geometry.boundingSphere !== null ) {
  179. this.boundingSphere = geometry.boundingSphere.clone();
  180. }
  181. if ( geometry.boundingBox !== null ) {
  182. this.boundingBox = geometry.boundingBox.clone();
  183. }
  184. } else if ( object instanceof THREE.Mesh ) {
  185. if ( geometry instanceof THREE.Geometry ) {
  186. this.fromGeometry( geometry );
  187. }
  188. }
  189. return this;
  190. },
  191. updateFromObject: function ( object ) {
  192. var geometry = object.geometry;
  193. if ( object instanceof THREE.Mesh ) {
  194. var direct = geometry.__directGeometry;
  195. if ( direct === undefined ) {
  196. return this.fromGeometry( geometry );
  197. }
  198. direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
  199. direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
  200. direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
  201. direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
  202. direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
  203. geometry.verticesNeedUpdate = false;
  204. geometry.normalsNeedUpdate = false;
  205. geometry.colorsNeedUpdate = false;
  206. geometry.uvsNeedUpdate = false;
  207. geometry.groupsNeedUpdate = false;
  208. geometry = direct;
  209. }
  210. if ( geometry.verticesNeedUpdate === true ) {
  211. var attribute = this.attributes.position;
  212. if ( attribute !== undefined ) {
  213. attribute.copyVector3sArray( geometry.vertices );
  214. attribute.needsUpdate = true;
  215. }
  216. geometry.verticesNeedUpdate = false;
  217. }
  218. if ( geometry.normalsNeedUpdate === true ) {
  219. var attribute = this.attributes.normal;
  220. if ( attribute !== undefined ) {
  221. attribute.copyVector3sArray( geometry.normals );
  222. attribute.needsUpdate = true;
  223. }
  224. geometry.normalsNeedUpdate = false;
  225. }
  226. if ( geometry.colorsNeedUpdate === true ) {
  227. var attribute = this.attributes.color;
  228. if ( attribute !== undefined ) {
  229. attribute.copyColorsArray( geometry.colors );
  230. attribute.needsUpdate = true;
  231. }
  232. geometry.colorsNeedUpdate = false;
  233. }
  234. if ( geometry.uvsNeedUpdate ) {
  235. var attribute = this.attributes.uv;
  236. if ( attribute !== undefined ) {
  237. attribute.copyVector2sArray( geometry.uvs );
  238. attribute.needsUpdate = true;
  239. }
  240. geometry.uvsNeedUpdate = false;
  241. }
  242. if ( geometry.lineDistancesNeedUpdate ) {
  243. var attribute = this.attributes.lineDistance;
  244. if ( attribute !== undefined ) {
  245. attribute.copyArray( geometry.lineDistances );
  246. attribute.needsUpdate = true;
  247. }
  248. geometry.lineDistancesNeedUpdate = false;
  249. }
  250. if ( geometry.groupsNeedUpdate ) {
  251. geometry.computeGroups( object.geometry );
  252. this.groups = geometry.groups;
  253. geometry.groupsNeedUpdate = false;
  254. }
  255. return this;
  256. },
  257. fromGeometry: function ( geometry ) {
  258. geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
  259. return this.fromDirectGeometry( geometry.__directGeometry );
  260. },
  261. fromDirectGeometry: function ( geometry ) {
  262. var positions = new Float32Array( geometry.vertices.length * 3 );
  263. this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  264. if ( geometry.normals.length > 0 ) {
  265. var normals = new Float32Array( geometry.normals.length * 3 );
  266. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  267. }
  268. if ( geometry.colors.length > 0 ) {
  269. var colors = new Float32Array( geometry.colors.length * 3 );
  270. this.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  271. }
  272. if ( geometry.uvs.length > 0 ) {
  273. var uvs = new Float32Array( geometry.uvs.length * 2 );
  274. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  275. }
  276. if ( geometry.uvs2.length > 0 ) {
  277. var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  278. this.addAttribute( 'uv2', new THREE.BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  279. }
  280. if ( geometry.indices.length > 0 ) {
  281. var TypeArray = geometry.vertices.length > 65535 ? Uint32Array : Uint16Array;
  282. var indices = new TypeArray( geometry.indices.length * 3 );
  283. this.setIndex( new THREE.BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
  284. }
  285. // groups
  286. this.groups = geometry.groups;
  287. // morphs
  288. for ( var name in geometry.morphTargets ) {
  289. var array = [];
  290. var morphTargets = geometry.morphTargets[ name ];
  291. for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
  292. var morphTarget = morphTargets[ i ];
  293. var attribute = new THREE.Float32Attribute( morphTarget.length * 3, 3 );
  294. array.push( attribute.copyVector3sArray( morphTarget ) );
  295. }
  296. this.morphAttributes[ name ] = array;
  297. }
  298. // skinning
  299. if ( geometry.skinIndices.length > 0 ) {
  300. var skinIndices = new THREE.Float32Attribute( geometry.skinIndices.length * 4, 4 );
  301. this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  302. }
  303. if ( geometry.skinWeights.length > 0 ) {
  304. var skinWeights = new THREE.Float32Attribute( geometry.skinWeights.length * 4, 4 );
  305. this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  306. }
  307. //
  308. if ( geometry.boundingSphere !== null ) {
  309. this.boundingSphere = geometry.boundingSphere.clone();
  310. }
  311. if ( geometry.boundingBox !== null ) {
  312. this.boundingBox = geometry.boundingBox.clone();
  313. }
  314. return this;
  315. },
  316. computeBoundingBox: function () {
  317. var vector = new THREE.Vector3();
  318. return function () {
  319. if ( this.boundingBox === null ) {
  320. this.boundingBox = new THREE.Box3();
  321. }
  322. var positions = this.attributes.position.array;
  323. if ( positions ) {
  324. var bb = this.boundingBox;
  325. bb.makeEmpty();
  326. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  327. vector.fromArray( positions, i );
  328. bb.expandByPoint( vector );
  329. }
  330. }
  331. if ( positions === undefined || positions.length === 0 ) {
  332. this.boundingBox.min.set( 0, 0, 0 );
  333. this.boundingBox.max.set( 0, 0, 0 );
  334. }
  335. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  336. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  337. }
  338. };
  339. }(),
  340. computeBoundingSphere: function () {
  341. var box = new THREE.Box3();
  342. var vector = new THREE.Vector3();
  343. return function () {
  344. if ( this.boundingSphere === null ) {
  345. this.boundingSphere = new THREE.Sphere();
  346. }
  347. var positions = this.attributes.position.array;
  348. if ( positions ) {
  349. box.makeEmpty();
  350. var center = this.boundingSphere.center;
  351. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  352. vector.fromArray( positions, i );
  353. box.expandByPoint( vector );
  354. }
  355. box.center( center );
  356. // hoping to find a boundingSphere with a radius smaller than the
  357. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  358. var maxRadiusSq = 0;
  359. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  360. vector.fromArray( positions, i );
  361. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  362. }
  363. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  364. if ( isNaN( this.boundingSphere.radius ) ) {
  365. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  366. }
  367. }
  368. };
  369. }(),
  370. computeFaceNormals: function () {
  371. // backwards compatibility
  372. },
  373. computeVertexNormals: function () {
  374. var index = this.index;
  375. var attributes = this.attributes;
  376. var groups = this.groups;
  377. if ( attributes.position ) {
  378. var positions = attributes.position.array;
  379. if ( attributes.normal === undefined ) {
  380. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( positions.length ), 3 ) );
  381. } else {
  382. // reset existing normals to zero
  383. var normals = attributes.normal.array;
  384. for ( var i = 0, il = normals.length; i < il; i ++ ) {
  385. normals[ i ] = 0;
  386. }
  387. }
  388. var normals = attributes.normal.array;
  389. var vA, vB, vC,
  390. pA = new THREE.Vector3(),
  391. pB = new THREE.Vector3(),
  392. pC = new THREE.Vector3(),
  393. cb = new THREE.Vector3(),
  394. ab = new THREE.Vector3();
  395. // indexed elements
  396. if ( index ) {
  397. var indices = index.array;
  398. if ( groups.length === 0 ) {
  399. this.addGroup( 0, indices.length );
  400. }
  401. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  402. var group = groups[ j ];
  403. var start = group.start;
  404. var count = group.count;
  405. for ( var i = start, il = start + count; i < il; i += 3 ) {
  406. vA = indices[ i + 0 ] * 3;
  407. vB = indices[ i + 1 ] * 3;
  408. vC = indices[ i + 2 ] * 3;
  409. pA.fromArray( positions, vA );
  410. pB.fromArray( positions, vB );
  411. pC.fromArray( positions, vC );
  412. cb.subVectors( pC, pB );
  413. ab.subVectors( pA, pB );
  414. cb.cross( ab );
  415. normals[ vA ] += cb.x;
  416. normals[ vA + 1 ] += cb.y;
  417. normals[ vA + 2 ] += cb.z;
  418. normals[ vB ] += cb.x;
  419. normals[ vB + 1 ] += cb.y;
  420. normals[ vB + 2 ] += cb.z;
  421. normals[ vC ] += cb.x;
  422. normals[ vC + 1 ] += cb.y;
  423. normals[ vC + 2 ] += cb.z;
  424. }
  425. }
  426. } else {
  427. // non-indexed elements (unconnected triangle soup)
  428. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  429. pA.fromArray( positions, i );
  430. pB.fromArray( positions, i + 3 );
  431. pC.fromArray( positions, i + 6 );
  432. cb.subVectors( pC, pB );
  433. ab.subVectors( pA, pB );
  434. cb.cross( ab );
  435. normals[ i ] = cb.x;
  436. normals[ i + 1 ] = cb.y;
  437. normals[ i + 2 ] = cb.z;
  438. normals[ i + 3 ] = cb.x;
  439. normals[ i + 4 ] = cb.y;
  440. normals[ i + 5 ] = cb.z;
  441. normals[ i + 6 ] = cb.x;
  442. normals[ i + 7 ] = cb.y;
  443. normals[ i + 8 ] = cb.z;
  444. }
  445. }
  446. this.normalizeNormals();
  447. attributes.normal.needsUpdate = true;
  448. }
  449. },
  450. computeTangents: function () {
  451. console.warn( 'THREE.BufferGeometry: .computeTangents() has been removed.' );
  452. },
  453. computeOffsets: function ( size ) {
  454. console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.');
  455. },
  456. merge: function ( geometry, offset ) {
  457. if ( geometry instanceof THREE.BufferGeometry === false ) {
  458. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  459. return;
  460. }
  461. if ( offset === undefined ) offset = 0;
  462. var attributes = this.attributes;
  463. for ( var key in attributes ) {
  464. if ( geometry.attributes[ key ] === undefined ) continue;
  465. var attribute1 = attributes[ key ];
  466. var attributeArray1 = attribute1.array;
  467. var attribute2 = geometry.attributes[ key ];
  468. var attributeArray2 = attribute2.array;
  469. var attributeSize = attribute2.itemSize;
  470. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  471. attributeArray1[ j ] = attributeArray2[ i ];
  472. }
  473. }
  474. return this;
  475. },
  476. normalizeNormals: function () {
  477. var normals = this.attributes.normal.array;
  478. var x, y, z, n;
  479. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  480. x = normals[ i ];
  481. y = normals[ i + 1 ];
  482. z = normals[ i + 2 ];
  483. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  484. normals[ i ] *= n;
  485. normals[ i + 1 ] *= n;
  486. normals[ i + 2 ] *= n;
  487. }
  488. },
  489. toJSON: function () {
  490. var data = {
  491. metadata: {
  492. version: 4.4,
  493. type: 'BufferGeometry',
  494. generator: 'BufferGeometry.toJSON'
  495. }
  496. };
  497. // standard BufferGeometry serialization
  498. data.uuid = this.uuid;
  499. data.type = this.type;
  500. if ( this.name !== '' ) data.name = this.name;
  501. if ( this.parameters !== undefined ) {
  502. var parameters = this.parameters;
  503. for ( var key in parameters ) {
  504. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  505. }
  506. return data;
  507. }
  508. data.data = { attributes: {} };
  509. var index = this.index;
  510. if ( index !== null ) {
  511. var array = Array.prototype.slice.call( index.array );
  512. data.data.index = {
  513. type: index.array.constructor.name,
  514. array: array
  515. };
  516. }
  517. var attributes = this.attributes;
  518. for ( var key in attributes ) {
  519. var attribute = attributes[ key ];
  520. var array = Array.prototype.slice.call( attribute.array );
  521. data.data.attributes[ key ] = {
  522. itemSize: attribute.itemSize,
  523. type: attribute.array.constructor.name,
  524. array: array
  525. };
  526. }
  527. var groups = this.groups;
  528. if ( groups.length > 0 ) {
  529. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  530. }
  531. var boundingSphere = this.boundingSphere;
  532. if ( boundingSphere !== null ) {
  533. data.data.boundingSphere = {
  534. center: boundingSphere.center.toArray(),
  535. radius: boundingSphere.radius
  536. };
  537. }
  538. return data;
  539. },
  540. clone: function () {
  541. // Handle primitives
  542. var parameters = this.parameters;
  543. if ( parameters !== undefined ) {
  544. var values = [];
  545. for ( var key in parameters ) {
  546. values.push( parameters[ key ] );
  547. }
  548. var geometry = Object.create( this.constructor.prototype );
  549. this.constructor.apply( geometry, values );
  550. return geometry;
  551. }
  552. return new this.constructor().copy( this );
  553. },
  554. copy: function ( source ) {
  555. var index = source.index;
  556. if ( index !== null ) {
  557. this.setIndex( index.clone() );
  558. }
  559. var attributes = source.attributes;
  560. for ( var name in attributes ) {
  561. var attribute = attributes[ name ];
  562. this.addAttribute( name, attribute.clone() );
  563. }
  564. var groups = source.groups;
  565. for ( var i = 0, l = groups.length; i < l; i ++ ) {
  566. var group = groups[ i ];
  567. this.addGroup( group.start, group.count );
  568. }
  569. return this;
  570. },
  571. dispose: function () {
  572. this.dispatchEvent( { type: 'dispose' } );
  573. }
  574. };
  575. THREE.EventDispatcher.prototype.apply( THREE.BufferGeometry.prototype );
  576. THREE.BufferGeometry.MaxIndex = 65535;
粤ICP备19079148号