BufferGeometry.js 23 KB

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