BufferAttribute.js 9.5 KB

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