BufferAttribute.js 6.5 KB

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