BufferAttribute.js 6.1 KB

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