BufferAttribute.js 8.3 KB

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