BufferGeometry.js 24 KB

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