BufferAttribute.js 8.1 KB

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