BufferGeometry.js 24 KB

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