BufferGeometry.js 24 KB

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