BufferAttribute.tests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * @author simonThiele / https://github.com/simonThiele
  3. */
  4. /* global QUnit */
  5. import { BufferAttribute } from '../../../../src/core/BufferAttribute';
  6. import { Color } from '../../../../src/math/Color';
  7. import { Vector2 } from '../../../../src/math/Vector2';
  8. import { Vector3 } from '../../../../src/math/Vector3';
  9. import { Vector4 } from '../../../../src/math/Vector4';
  10. export default QUnit.module( 'Core', () => {
  11. QUnit.module( 'BufferAttribute', () => {
  12. // INSTANCING
  13. QUnit.test( "Instancing", ( assert ) => {
  14. assert.throws(
  15. function () {
  16. var a = new BufferAttribute( [ 1, 2, 3, 4 ], 2, false );
  17. },
  18. /array should be a Typed Array/,
  19. "Calling constructor with a simple array throws Error"
  20. );
  21. } );
  22. // PROPERTIES
  23. QUnit.todo( "needsUpdate", ( assert ) => {
  24. assert.ok( false, "everything's gonna be alright" );
  25. } );
  26. // PUBLIC STUFF
  27. QUnit.todo( "isBufferAttribute", ( assert ) => {
  28. assert.ok( false, "everything's gonna be alright" );
  29. } );
  30. QUnit.test( "setArray", ( assert ) => {
  31. var f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  32. var a = new BufferAttribute( f32a, 2, false );
  33. a.setArray( f32a, 2 );
  34. assert.strictEqual( a.count, 2, "Check item count" );
  35. assert.strictEqual( a.array, f32a, "Check array" );
  36. assert.throws(
  37. function () {
  38. a.setArray( [ 1, 2, 3, 4 ] );
  39. },
  40. /array should be a Typed Array/,
  41. "Calling setArray with a simple array throws Error"
  42. );
  43. } );
  44. QUnit.todo( "setDynamic", ( assert ) => {
  45. assert.ok( false, "everything's gonna be alright" );
  46. } );
  47. QUnit.test( "copy", ( assert ) => {
  48. var attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
  49. attr.setDynamic( true );
  50. attr.needsUpdate = true;
  51. var attrCopy = new BufferAttribute().copy( attr );
  52. assert.ok( attr.count === attrCopy.count, 'count is equal' );
  53. assert.ok( attr.itemSize === attrCopy.itemSize, 'itemSize is equal' );
  54. assert.ok( attr.dynamic === attrCopy.dynamic, 'dynamic is equal' );
  55. assert.ok( attr.array.length === attrCopy.array.length, 'array length is equal' );
  56. assert.ok( attr.version === 1 && attrCopy.version === 0, 'version is not copied which is good' );
  57. } );
  58. QUnit.test( "copyAt", ( assert ) => {
  59. var attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), 3 );
  60. var attr2 = new BufferAttribute( new Float32Array( 9 ), 3 );
  61. attr2.copyAt( 1, attr, 2 );
  62. attr2.copyAt( 0, attr, 1 );
  63. attr2.copyAt( 2, attr, 0 );
  64. var i = attr.array;
  65. var i2 = attr2.array; // should be [4, 5, 6, 7, 8, 9, 1, 2, 3]
  66. assert.ok( i2[ 0 ] === i[ 3 ] && i2[ 1 ] === i[ 4 ] && i2[ 2 ] === i[ 5 ], 'chunck copied to correct place' );
  67. assert.ok( i2[ 3 ] === i[ 6 ] && i2[ 4 ] === i[ 7 ] && i2[ 5 ] === i[ 8 ], 'chunck copied to correct place' );
  68. assert.ok( i2[ 6 ] === i[ 0 ] && i2[ 7 ] === i[ 1 ] && i2[ 8 ] === i[ 2 ], 'chunck copied to correct place' );
  69. } );
  70. QUnit.test( "copyArray", ( assert ) => {
  71. var f32a = new Float32Array( [ 5, 6, 7, 8 ] );
  72. var a = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4 ] ), 2, false );
  73. a.copyArray( f32a );
  74. assert.deepEqual( a.array, f32a, "Check array has new values" );
  75. } );
  76. QUnit.test( "copyColorsArray", ( assert ) => {
  77. var attr = new BufferAttribute( new Float32Array( 6 ), 3 );
  78. attr.copyColorsArray( [
  79. new Color( 0, 0.5, 1 ),
  80. new Color( 0.25, 1, 0 )
  81. ] );
  82. var i = attr.array;
  83. assert.ok( i[ 0 ] === 0 && i[ 1 ] === 0.5 && i[ 2 ] === 1, 'first color was copied correctly' );
  84. assert.ok( i[ 3 ] === 0.25 && i[ 4 ] === 1 && i[ 5 ] === 0, 'second color was copied correctly' );
  85. } );
  86. QUnit.test( "copyVector2sArray", ( assert ) => {
  87. var attr = new BufferAttribute( new Float32Array( 4 ), 2 );
  88. attr.copyVector2sArray( [
  89. new Vector2( 1, 2 ),
  90. new Vector2( 4, 5 )
  91. ] );
  92. var i = attr.array;
  93. assert.ok( i[ 0 ] === 1 && i[ 1 ] === 2, 'first vector was copied correctly' );
  94. assert.ok( i[ 2 ] === 4 && i[ 3 ] === 5, 'second vector was copied correctly' );
  95. } );
  96. QUnit.test( "copyVector3sArray", ( assert ) => {
  97. var attr = new BufferAttribute( new Float32Array( 6 ), 2 );
  98. attr.copyVector3sArray( [
  99. new Vector3( 1, 2, 3 ),
  100. new Vector3( 10, 20, 30 )
  101. ] );
  102. var i = attr.array;
  103. assert.ok( i[ 0 ] === 1 && i[ 1 ] === 2 && i[ 2 ] === 3, 'first vector was copied correctly' );
  104. assert.ok( i[ 3 ] === 10 && i[ 4 ] === 20 && i[ 5 ] === 30, 'second vector was copied correctly' );
  105. } );
  106. QUnit.test( "copyVector4sArray", ( assert ) => {
  107. var attr = new BufferAttribute( new Float32Array( 8 ), 2 );
  108. attr.copyVector4sArray( [
  109. new Vector4( 1, 2, 3, 4 ),
  110. new Vector4( 10, 20, 30, 40 )
  111. ] );
  112. var i = attr.array;
  113. assert.ok( i[ 0 ] === 1 && i[ 1 ] === 2 && i[ 2 ] === 3 && i[ 3 ] === 4, 'first vector was copied correctly' );
  114. assert.ok( i[ 4 ] === 10 && i[ 5 ] === 20 && i[ 6 ] === 30 && i[ 7 ] === 40, 'second vector was copied correctly' );
  115. } );
  116. QUnit.test( "set", ( assert ) => {
  117. var f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  118. var a = new BufferAttribute( f32a, 2, false );
  119. var expected = new Float32Array( [ 9, 2, 8, 4 ] );
  120. a.set( [ 9 ] );
  121. a.set( [ 8 ], 2 );
  122. assert.deepEqual( a.array, expected, "Check array has expected values" );
  123. } );
  124. QUnit.test( "set[X, Y, Z, W, XYZ, XYZW]/get[X, Y, Z, W]", ( assert ) => {
  125. var f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
  126. var a = new BufferAttribute( f32a, 4, false );
  127. var expected = new Float32Array( [ 1, 2, - 3, - 4, - 5, - 6, 7, 8 ] );
  128. a.setX( 1, a.getX( 1 ) * - 1 );
  129. a.setY( 1, a.getY( 1 ) * - 1 );
  130. a.setZ( 0, a.getZ( 0 ) * - 1 );
  131. a.setW( 0, a.getW( 0 ) * - 1 );
  132. assert.deepEqual( a.array, expected, "Check all set* calls set the correct values" );
  133. } );
  134. QUnit.test( "setXY", ( assert ) => {
  135. var f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  136. var a = new BufferAttribute( f32a, 2, false );
  137. var expected = new Float32Array( [ - 1, - 2, 3, 4 ] );
  138. a.setXY( 0, - 1, - 2 );
  139. assert.deepEqual( a.array, expected, "Check for the correct values" );
  140. } );
  141. QUnit.test( "setXYZ", ( assert ) => {
  142. var f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6 ] );
  143. var a = new BufferAttribute( f32a, 3, false );
  144. var expected = new Float32Array( [ 1, 2, 3, - 4, - 5, - 6 ] );
  145. a.setXYZ( 1, - 4, - 5, - 6 );
  146. assert.deepEqual( a.array, expected, "Check for the correct values" );
  147. } );
  148. QUnit.test( "setXYZW", ( assert ) => {
  149. var f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  150. var a = new BufferAttribute( f32a, 4, false );
  151. var expected = new Float32Array( [ - 1, - 2, - 3, - 4 ] );
  152. a.setXYZW( 0, - 1, - 2, - 3, - 4 );
  153. assert.deepEqual( a.array, expected, "Check for the correct values" );
  154. } );
  155. QUnit.test( "onUpload", ( assert ) => {
  156. var a = new BufferAttribute();
  157. var func = function () { };
  158. a.onUpload( func );
  159. assert.strictEqual( a.onUploadCallback, func, "Check callback was set properly" );
  160. } );
  161. QUnit.test( "clone", ( assert ) => {
  162. var attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 0.12, - 12 ] ), 2 );
  163. var attrCopy = attr.clone();
  164. assert.ok( attr.array.length === attrCopy.array.length, 'attribute was cloned' );
  165. for ( var i = 0; i < attr.array.length; i ++ ) {
  166. assert.ok( attr.array[ i ] === attrCopy.array[ i ], 'array item is equal' );
  167. }
  168. } );
  169. // OTHERS
  170. QUnit.test( "count", ( assert ) => {
  171. assert.ok(
  172. new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ).count === 2,
  173. 'count is equal to the number of chunks'
  174. );
  175. } );
  176. } );
  177. QUnit.module( 'Int8BufferAttribute', () => {
  178. // INHERITANCE
  179. QUnit.todo( "Extending", ( assert ) => {
  180. assert.ok( false, "everything's gonna be alright" );
  181. } );
  182. // INSTANCING
  183. QUnit.todo( "Instancing", ( assert ) => {
  184. assert.ok( false, "everything's gonna be alright" );
  185. } );
  186. } );
  187. QUnit.module( 'Uint8BufferAttribute', () => {
  188. // INHERITANCE
  189. QUnit.todo( "Extending", ( assert ) => {
  190. assert.ok( false, "everything's gonna be alright" );
  191. } );
  192. // INSTANCING
  193. QUnit.todo( "Instancing", ( assert ) => {
  194. assert.ok( false, "everything's gonna be alright" );
  195. } );
  196. } );
  197. QUnit.module( 'Uint8ClampedBufferAttribute', () => {
  198. // INHERITANCE
  199. QUnit.todo( "Extending", ( assert ) => {
  200. assert.ok( false, "everything's gonna be alright" );
  201. } );
  202. // INSTANCING
  203. QUnit.todo( "Instancing", ( assert ) => {
  204. assert.ok( false, "everything's gonna be alright" );
  205. } );
  206. } );
  207. QUnit.module( 'Int16BufferAttribute', () => {
  208. // INHERITANCE
  209. QUnit.todo( "Extending", ( assert ) => {
  210. assert.ok( false, "everything's gonna be alright" );
  211. } );
  212. // INSTANCING
  213. QUnit.todo( "Instancing", ( assert ) => {
  214. assert.ok( false, "everything's gonna be alright" );
  215. } );
  216. } );
  217. QUnit.module( 'Uint16BufferAttribute', () => {
  218. // INHERITANCE
  219. QUnit.todo( "Extending", ( assert ) => {
  220. assert.ok( false, "everything's gonna be alright" );
  221. } );
  222. // INSTANCING
  223. QUnit.todo( "Instancing", ( assert ) => {
  224. assert.ok( false, "everything's gonna be alright" );
  225. } );
  226. } );
  227. QUnit.module( 'Int32BufferAttribute', () => {
  228. // INHERITANCE
  229. QUnit.todo( "Extending", ( assert ) => {
  230. assert.ok( false, "everything's gonna be alright" );
  231. } );
  232. // INSTANCING
  233. QUnit.todo( "Instancing", ( assert ) => {
  234. assert.ok( false, "everything's gonna be alright" );
  235. } );
  236. } );
  237. QUnit.module( 'Uint32BufferAttribute', () => {
  238. // INHERITANCE
  239. QUnit.todo( "Extending", ( assert ) => {
  240. assert.ok( false, "everything's gonna be alright" );
  241. } );
  242. // INSTANCING
  243. QUnit.todo( "Instancing", ( assert ) => {
  244. assert.ok( false, "everything's gonna be alright" );
  245. } );
  246. } );
  247. QUnit.module( 'Float32BufferAttribute', () => {
  248. // INHERITANCE
  249. QUnit.todo( "Extending", ( assert ) => {
  250. assert.ok( false, "everything's gonna be alright" );
  251. } );
  252. // INSTANCING
  253. QUnit.todo( "Instancing", ( assert ) => {
  254. assert.ok( false, "everything's gonna be alright" );
  255. } );
  256. } );
  257. QUnit.module( 'Float64BufferAttribute', () => {
  258. // INHERITANCE
  259. QUnit.todo( "Extending", ( assert ) => {
  260. assert.ok( false, "everything's gonna be alright" );
  261. } );
  262. // INSTANCING
  263. QUnit.todo( "Instancing", ( assert ) => {
  264. assert.ok( false, "everything's gonna be alright" );
  265. } );
  266. } );
  267. } );
粤ICP备19079148号