BufferAttribute.js 9.5 KB

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