BufferAttribute.js 9.2 KB

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