BufferAttribute.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 { _Math } from '../math/Math.js';
  6. /**
  7. * @author mrdoob / http://mrdoob.com/
  8. */
  9. function BufferAttribute( array, itemSize, normalized ) {
  10. if ( Array.isArray( array ) ) {
  11. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  12. }
  13. this.uuid = _Math.generateUUID();
  14. this.name = '';
  15. this.array = array;
  16. this.itemSize = itemSize;
  17. this.count = array !== undefined ? array.length / itemSize : 0;
  18. this.normalized = normalized === true;
  19. this.dynamic = false;
  20. this.updateRange = { offset: 0, count: - 1 };
  21. this.onUploadCallback = function () {};
  22. this.version = 0;
  23. }
  24. Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', {
  25. set: function ( value ) {
  26. if ( value === true ) this.version ++;
  27. }
  28. } );
  29. Object.assign( BufferAttribute.prototype, {
  30. isBufferAttribute: true,
  31. setArray: function ( array ) {
  32. if ( Array.isArray( array ) ) {
  33. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  34. }
  35. this.count = array !== undefined ? array.length / this.itemSize : 0;
  36. this.array = array;
  37. },
  38. setDynamic: function ( value ) {
  39. this.dynamic = value;
  40. return this;
  41. },
  42. copy: function ( source ) {
  43. this.array = new source.array.constructor( source.array );
  44. this.itemSize = source.itemSize;
  45. this.count = source.count;
  46. this.normalized = source.normalized;
  47. this.dynamic = source.dynamic;
  48. return this;
  49. },
  50. copyAt: function ( index1, attribute, index2 ) {
  51. index1 *= this.itemSize;
  52. index2 *= attribute.itemSize;
  53. for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
  54. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  55. }
  56. return this;
  57. },
  58. copyArray: function ( array ) {
  59. this.array.set( array );
  60. return this;
  61. },
  62. copyColorsArray: function ( colors ) {
  63. var array = this.array, offset = 0;
  64. for ( var i = 0, l = colors.length; i < l; i ++ ) {
  65. var color = colors[ i ];
  66. if ( color === undefined ) {
  67. console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
  68. color = new Color();
  69. }
  70. array[ offset ++ ] = color.r;
  71. array[ offset ++ ] = color.g;
  72. array[ offset ++ ] = color.b;
  73. }
  74. return this;
  75. },
  76. copyIndicesArray: function ( indices ) {
  77. var array = this.array, offset = 0;
  78. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  79. var index = indices[ i ];
  80. array[ offset ++ ] = index.a;
  81. array[ offset ++ ] = index.b;
  82. array[ offset ++ ] = index.c;
  83. }
  84. return this;
  85. },
  86. copyVector2sArray: function ( vectors ) {
  87. var array = this.array, offset = 0;
  88. for ( var i = 0, l = vectors.length; i < l; i ++ ) {
  89. var vector = vectors[ i ];
  90. if ( vector === undefined ) {
  91. console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
  92. vector = new Vector2();
  93. }
  94. array[ offset ++ ] = vector.x;
  95. array[ offset ++ ] = vector.y;
  96. }
  97. return this;
  98. },
  99. copyVector3sArray: function ( vectors ) {
  100. var array = this.array, offset = 0;
  101. for ( var i = 0, l = vectors.length; i < l; i ++ ) {
  102. var vector = vectors[ i ];
  103. if ( vector === undefined ) {
  104. console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
  105. vector = new Vector3();
  106. }
  107. array[ offset ++ ] = vector.x;
  108. array[ offset ++ ] = vector.y;
  109. array[ offset ++ ] = vector.z;
  110. }
  111. return this;
  112. },
  113. copyVector4sArray: function ( vectors ) {
  114. var array = this.array, offset = 0;
  115. for ( var i = 0, l = vectors.length; i < l; i ++ ) {
  116. var vector = vectors[ i ];
  117. if ( vector === undefined ) {
  118. console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
  119. vector = new Vector4();
  120. }
  121. array[ offset ++ ] = vector.x;
  122. array[ offset ++ ] = vector.y;
  123. array[ offset ++ ] = vector.z;
  124. array[ offset ++ ] = vector.w;
  125. }
  126. return this;
  127. },
  128. set: function ( value, offset ) {
  129. if ( offset === undefined ) offset = 0;
  130. this.array.set( value, offset );
  131. return this;
  132. },
  133. getX: function ( index ) {
  134. return this.array[ index * this.itemSize ];
  135. },
  136. setX: function ( index, x ) {
  137. this.array[ index * this.itemSize ] = x;
  138. return this;
  139. },
  140. getY: function ( index ) {
  141. return this.array[ index * this.itemSize + 1 ];
  142. },
  143. setY: function ( index, y ) {
  144. this.array[ index * this.itemSize + 1 ] = y;
  145. return this;
  146. },
  147. getZ: function ( index ) {
  148. return this.array[ index * this.itemSize + 2 ];
  149. },
  150. setZ: function ( index, z ) {
  151. this.array[ index * this.itemSize + 2 ] = z;
  152. return this;
  153. },
  154. getW: function ( index ) {
  155. return this.array[ index * this.itemSize + 3 ];
  156. },
  157. setW: function ( index, w ) {
  158. this.array[ index * this.itemSize + 3 ] = w;
  159. return this;
  160. },
  161. setXY: function ( index, x, y ) {
  162. index *= this.itemSize;
  163. this.array[ index + 0 ] = x;
  164. this.array[ index + 1 ] = y;
  165. return this;
  166. },
  167. setXYZ: function ( index, x, y, z ) {
  168. index *= this.itemSize;
  169. this.array[ index + 0 ] = x;
  170. this.array[ index + 1 ] = y;
  171. this.array[ index + 2 ] = z;
  172. return this;
  173. },
  174. setXYZW: function ( index, x, y, z, w ) {
  175. index *= this.itemSize;
  176. this.array[ index + 0 ] = x;
  177. this.array[ index + 1 ] = y;
  178. this.array[ index + 2 ] = z;
  179. this.array[ index + 3 ] = w;
  180. return this;
  181. },
  182. onUpload: function ( callback ) {
  183. this.onUploadCallback = callback;
  184. return this;
  185. },
  186. clone: function () {
  187. return new this.constructor( this.array, this.itemSize ).copy( this );
  188. }
  189. } );
  190. //
  191. function Int8BufferAttribute( array, itemSize ) {
  192. BufferAttribute.call( this, new Int8Array( array ), itemSize );
  193. }
  194. Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  195. Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
  196. function Uint8BufferAttribute( array, itemSize ) {
  197. BufferAttribute.call( this, new Uint8Array( array ), itemSize );
  198. }
  199. Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  200. Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
  201. function Uint8ClampedBufferAttribute( array, itemSize ) {
  202. BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize );
  203. }
  204. Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  205. Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
  206. function Int16BufferAttribute( array, itemSize ) {
  207. BufferAttribute.call( this, new Int16Array( array ), itemSize );
  208. }
  209. Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  210. Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
  211. function Uint16BufferAttribute( array, itemSize ) {
  212. BufferAttribute.call( this, new Uint16Array( array ), itemSize );
  213. }
  214. Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  215. Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
  216. function Int32BufferAttribute( array, itemSize ) {
  217. BufferAttribute.call( this, new Int32Array( array ), itemSize );
  218. }
  219. Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  220. Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
  221. function Uint32BufferAttribute( array, itemSize ) {
  222. BufferAttribute.call( this, new Uint32Array( array ), itemSize );
  223. }
  224. Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  225. Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
  226. function Float32BufferAttribute( array, itemSize ) {
  227. BufferAttribute.call( this, new Float32Array( array ), itemSize );
  228. }
  229. Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  230. Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
  231. function Float64BufferAttribute( array, itemSize ) {
  232. BufferAttribute.call( this, new Float64Array( array ), itemSize );
  233. }
  234. Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  235. Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
  236. //
  237. export {
  238. Float64BufferAttribute,
  239. Float32BufferAttribute,
  240. Uint32BufferAttribute,
  241. Int32BufferAttribute,
  242. Uint16BufferAttribute,
  243. Int16BufferAttribute,
  244. Uint8ClampedBufferAttribute,
  245. Uint8BufferAttribute,
  246. Int8BufferAttribute,
  247. BufferAttribute
  248. };