BufferAttribute.js 5.7 KB

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