BufferAttribute.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 = 0 ) {
  161. this.array.set( value, offset );
  162. return this;
  163. },
  164. getX: function ( index ) {
  165. return this.array[ index * this.itemSize ];
  166. },
  167. setX: function ( index, x ) {
  168. this.array[ index * this.itemSize ] = x;
  169. return this;
  170. },
  171. getY: function ( index ) {
  172. return this.array[ index * this.itemSize + 1 ];
  173. },
  174. setY: function ( index, y ) {
  175. this.array[ index * this.itemSize + 1 ] = y;
  176. return this;
  177. },
  178. getZ: function ( index ) {
  179. return this.array[ index * this.itemSize + 2 ];
  180. },
  181. setZ: function ( index, z ) {
  182. this.array[ index * this.itemSize + 2 ] = z;
  183. return this;
  184. },
  185. getW: function ( index ) {
  186. return this.array[ index * this.itemSize + 3 ];
  187. },
  188. setW: function ( index, w ) {
  189. this.array[ index * this.itemSize + 3 ] = w;
  190. return this;
  191. },
  192. setXY: function ( index, x, y ) {
  193. index *= this.itemSize;
  194. this.array[ index + 0 ] = x;
  195. this.array[ index + 1 ] = y;
  196. return this;
  197. },
  198. setXYZ: function ( index, x, y, z ) {
  199. index *= this.itemSize;
  200. this.array[ index + 0 ] = x;
  201. this.array[ index + 1 ] = y;
  202. this.array[ index + 2 ] = z;
  203. return this;
  204. },
  205. setXYZW: function ( index, x, y, z, w ) {
  206. index *= this.itemSize;
  207. this.array[ index + 0 ] = x;
  208. this.array[ index + 1 ] = y;
  209. this.array[ index + 2 ] = z;
  210. this.array[ index + 3 ] = w;
  211. return this;
  212. },
  213. onUpload: function ( callback ) {
  214. this.onUploadCallback = callback;
  215. return this;
  216. },
  217. clone: function () {
  218. return new this.constructor( this.array, this.itemSize ).copy( this );
  219. },
  220. toJSON: function () {
  221. return {
  222. itemSize: this.itemSize,
  223. type: this.array.constructor.name,
  224. array: Array.prototype.slice.call( this.array ),
  225. normalized: this.normalized
  226. };
  227. }
  228. } );
  229. //
  230. function Int8BufferAttribute( array, itemSize, normalized ) {
  231. BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
  232. }
  233. Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  234. Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
  235. function Uint8BufferAttribute( array, itemSize, normalized ) {
  236. BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
  237. }
  238. Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  239. Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
  240. function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
  241. BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
  242. }
  243. Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  244. Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
  245. function Int16BufferAttribute( array, itemSize, normalized ) {
  246. BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
  247. }
  248. Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  249. Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
  250. function Uint16BufferAttribute( array, itemSize, normalized ) {
  251. BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
  252. }
  253. Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  254. Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
  255. function Int32BufferAttribute( array, itemSize, normalized ) {
  256. BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
  257. }
  258. Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  259. Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
  260. function Uint32BufferAttribute( array, itemSize, normalized ) {
  261. BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
  262. }
  263. Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  264. Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
  265. function Float16BufferAttribute( array, itemSize, normalized ) {
  266. BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
  267. }
  268. Float16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  269. Float16BufferAttribute.prototype.constructor = Float16BufferAttribute;
  270. Float16BufferAttribute.prototype.isFloat16BufferAttribute = true;
  271. function Float32BufferAttribute( array, itemSize, normalized ) {
  272. BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
  273. }
  274. Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  275. Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
  276. function Float64BufferAttribute( array, itemSize, normalized ) {
  277. BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
  278. }
  279. Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
  280. Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
  281. //
  282. export {
  283. Float64BufferAttribute,
  284. Float32BufferAttribute,
  285. Float16BufferAttribute,
  286. Uint32BufferAttribute,
  287. Int32BufferAttribute,
  288. Uint16BufferAttribute,
  289. Int16BufferAttribute,
  290. Uint8ClampedBufferAttribute,
  291. Uint8BufferAttribute,
  292. Int8BufferAttribute,
  293. BufferAttribute
  294. };