BufferAttribute.js 6.2 KB

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