BufferAttribute.js 6.1 KB

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