BufferAttribute.js 6.5 KB

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