BufferAttribute.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import { Vector4 } from '../math/Vector4.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Vector2 } from '../math/Vector2.js';
  4. import { Color } from '../math/Color.js';
  5. import { StaticDrawUsage } from '../constants.js';
  6. const _vector = /*@__PURE__*/ new Vector3();
  7. const _vector2 = /*@__PURE__*/ new Vector2();
  8. class BufferAttribute {
  9. constructor( array, itemSize, normalized ) {
  10. if ( Array.isArray( array ) ) {
  11. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  12. }
  13. this.name = '';
  14. this.array = array;
  15. this.itemSize = itemSize;
  16. this.count = array !== undefined ? array.length / itemSize : 0;
  17. this.normalized = normalized === true;
  18. this.usage = StaticDrawUsage;
  19. this.updateRange = { offset: 0, count: - 1 };
  20. this.version = 0;
  21. this.onUploadCallback = function () {};
  22. }
  23. set needsUpdate( value ) {
  24. if ( value === true ) this.version ++;
  25. }
  26. setUsage( value ) {
  27. this.usage = value;
  28. return this;
  29. }
  30. copy( source ) {
  31. this.name = source.name;
  32. this.array = new source.array.constructor( source.array );
  33. this.itemSize = source.itemSize;
  34. this.count = source.count;
  35. this.normalized = source.normalized;
  36. this.usage = source.usage;
  37. return this;
  38. }
  39. copyAt( index1, attribute, index2 ) {
  40. index1 *= this.itemSize;
  41. index2 *= attribute.itemSize;
  42. for ( let i = 0, l = this.itemSize; i < l; i ++ ) {
  43. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  44. }
  45. return this;
  46. }
  47. copyArray( array ) {
  48. this.array.set( array );
  49. return this;
  50. }
  51. copyColorsArray( colors ) {
  52. const array = this.array;
  53. let offset = 0;
  54. for ( let i = 0, l = colors.length; i < l; i ++ ) {
  55. let color = colors[ i ];
  56. if ( color === undefined ) {
  57. console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
  58. color = new Color();
  59. }
  60. array[ offset ++ ] = color.r;
  61. array[ offset ++ ] = color.g;
  62. array[ offset ++ ] = color.b;
  63. }
  64. return this;
  65. }
  66. copyVector2sArray( vectors ) {
  67. const array = this.array;
  68. let offset = 0;
  69. for ( let i = 0, l = vectors.length; i < l; i ++ ) {
  70. let vector = vectors[ i ];
  71. if ( vector === undefined ) {
  72. console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
  73. vector = new Vector2();
  74. }
  75. array[ offset ++ ] = vector.x;
  76. array[ offset ++ ] = vector.y;
  77. }
  78. return this;
  79. }
  80. copyVector3sArray( vectors ) {
  81. const array = this.array;
  82. let offset = 0;
  83. for ( let i = 0, l = vectors.length; i < l; i ++ ) {
  84. let vector = vectors[ i ];
  85. if ( vector === undefined ) {
  86. console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
  87. vector = new Vector3();
  88. }
  89. array[ offset ++ ] = vector.x;
  90. array[ offset ++ ] = vector.y;
  91. array[ offset ++ ] = vector.z;
  92. }
  93. return this;
  94. }
  95. copyVector4sArray( vectors ) {
  96. const array = this.array;
  97. let offset = 0;
  98. for ( let i = 0, l = vectors.length; i < l; i ++ ) {
  99. let vector = vectors[ i ];
  100. if ( vector === undefined ) {
  101. console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
  102. vector = new Vector4();
  103. }
  104. array[ offset ++ ] = vector.x;
  105. array[ offset ++ ] = vector.y;
  106. array[ offset ++ ] = vector.z;
  107. array[ offset ++ ] = vector.w;
  108. }
  109. return this;
  110. }
  111. applyMatrix3( m ) {
  112. if ( this.itemSize === 2 ) {
  113. for ( let i = 0, l = this.count; i < l; i ++ ) {
  114. _vector2.fromBufferAttribute( this, i );
  115. _vector2.applyMatrix3( m );
  116. this.setXY( i, _vector2.x, _vector2.y );
  117. }
  118. } else if ( this.itemSize === 3 ) {
  119. for ( let i = 0, l = this.count; i < l; i ++ ) {
  120. _vector.fromBufferAttribute( this, i );
  121. _vector.applyMatrix3( m );
  122. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  123. }
  124. }
  125. return this;
  126. }
  127. applyMatrix4( m ) {
  128. for ( let i = 0, l = this.count; i < l; i ++ ) {
  129. _vector.x = this.getX( i );
  130. _vector.y = this.getY( i );
  131. _vector.z = this.getZ( i );
  132. _vector.applyMatrix4( m );
  133. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  134. }
  135. return this;
  136. }
  137. applyNormalMatrix( m ) {
  138. for ( let i = 0, l = this.count; i < l; i ++ ) {
  139. _vector.x = this.getX( i );
  140. _vector.y = this.getY( i );
  141. _vector.z = this.getZ( i );
  142. _vector.applyNormalMatrix( m );
  143. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  144. }
  145. return this;
  146. }
  147. transformDirection( m ) {
  148. for ( let i = 0, l = this.count; i < l; i ++ ) {
  149. _vector.x = this.getX( i );
  150. _vector.y = this.getY( i );
  151. _vector.z = this.getZ( i );
  152. _vector.transformDirection( m );
  153. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  154. }
  155. return this;
  156. }
  157. set( value, offset = 0 ) {
  158. this.array.set( value, offset );
  159. return this;
  160. }
  161. getX( index ) {
  162. return this.array[ index * this.itemSize ];
  163. }
  164. setX( index, x ) {
  165. this.array[ index * this.itemSize ] = x;
  166. return this;
  167. }
  168. getY( index ) {
  169. return this.array[ index * this.itemSize + 1 ];
  170. }
  171. setY( index, y ) {
  172. this.array[ index * this.itemSize + 1 ] = y;
  173. return this;
  174. }
  175. getZ( index ) {
  176. return this.array[ index * this.itemSize + 2 ];
  177. }
  178. setZ( index, z ) {
  179. this.array[ index * this.itemSize + 2 ] = z;
  180. return this;
  181. }
  182. getW( index ) {
  183. return this.array[ index * this.itemSize + 3 ];
  184. }
  185. setW( index, w ) {
  186. this.array[ index * this.itemSize + 3 ] = w;
  187. return this;
  188. }
  189. setXY( index, x, y ) {
  190. index *= this.itemSize;
  191. this.array[ index + 0 ] = x;
  192. this.array[ index + 1 ] = y;
  193. return this;
  194. }
  195. setXYZ( index, x, y, z ) {
  196. index *= this.itemSize;
  197. this.array[ index + 0 ] = x;
  198. this.array[ index + 1 ] = y;
  199. this.array[ index + 2 ] = z;
  200. return this;
  201. }
  202. setXYZW( index, x, y, z, w ) {
  203. index *= this.itemSize;
  204. this.array[ index + 0 ] = x;
  205. this.array[ index + 1 ] = y;
  206. this.array[ index + 2 ] = z;
  207. this.array[ index + 3 ] = w;
  208. return this;
  209. }
  210. onUpload( callback ) {
  211. this.onUploadCallback = callback;
  212. return this;
  213. }
  214. clone() {
  215. return new this.constructor( this.array, this.itemSize ).copy( this );
  216. }
  217. toJSON() {
  218. const data = {
  219. itemSize: this.itemSize,
  220. type: this.array.constructor.name,
  221. array: Array.prototype.slice.call( this.array ),
  222. normalized: this.normalized
  223. };
  224. if ( this.name !== '' ) data.name = this.name;
  225. if ( this.usage !== StaticDrawUsage ) data.usage = this.usage;
  226. if ( this.updateRange.offset !== 0 || this.updateRange.count !== - 1 ) data.updateRange = this.updateRange;
  227. return data;
  228. }
  229. }
  230. BufferAttribute.prototype.isBufferAttribute = true;
  231. //
  232. class Int8BufferAttribute extends BufferAttribute {
  233. constructor( array, itemSize, normalized ) {
  234. super( new Int8Array( array ), itemSize, normalized );
  235. }
  236. }
  237. class Uint8BufferAttribute extends BufferAttribute {
  238. constructor( array, itemSize, normalized ) {
  239. super( new Uint8Array( array ), itemSize, normalized );
  240. }
  241. }
  242. class Uint8ClampedBufferAttribute extends BufferAttribute {
  243. constructor( array, itemSize, normalized ) {
  244. super( new Uint8ClampedArray( array ), itemSize, normalized );
  245. }
  246. }
  247. class Int16BufferAttribute extends BufferAttribute {
  248. constructor( array, itemSize, normalized ) {
  249. super( new Int16Array( array ), itemSize, normalized );
  250. }
  251. }
  252. class Uint16BufferAttribute extends BufferAttribute {
  253. constructor( array, itemSize, normalized ) {
  254. super( new Uint16Array( array ), itemSize, normalized );
  255. }
  256. }
  257. class Int32BufferAttribute extends BufferAttribute {
  258. constructor( array, itemSize, normalized ) {
  259. super( new Int32Array( array ), itemSize, normalized );
  260. }
  261. }
  262. class Uint32BufferAttribute extends BufferAttribute {
  263. constructor( array, itemSize, normalized ) {
  264. super( new Uint32Array( array ), itemSize, normalized );
  265. }
  266. }
  267. class Float16BufferAttribute extends BufferAttribute {
  268. constructor( array, itemSize, normalized ) {
  269. super( new Uint16Array( array ), itemSize, normalized );
  270. }
  271. }
  272. Float16BufferAttribute.prototype.isFloat16BufferAttribute = true;
  273. class Float32BufferAttribute extends BufferAttribute {
  274. constructor( array, itemSize, normalized ) {
  275. super( new Float32Array( array ), itemSize, normalized );
  276. }
  277. }
  278. class Float64BufferAttribute extends BufferAttribute {
  279. constructor( array, itemSize, normalized ) {
  280. super( new Float64Array( array ), itemSize, normalized );
  281. }
  282. }
  283. //
  284. export {
  285. Float64BufferAttribute,
  286. Float32BufferAttribute,
  287. Float16BufferAttribute,
  288. Uint32BufferAttribute,
  289. Int32BufferAttribute,
  290. Uint16BufferAttribute,
  291. Int16BufferAttribute,
  292. Uint8ClampedBufferAttribute,
  293. Uint8BufferAttribute,
  294. Int8BufferAttribute,
  295. BufferAttribute
  296. };