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