BufferGeometry.js 24 KB

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