BufferGeometry.js 19 KB

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