BufferAttribute.tests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* global QUnit */
  2. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  3. import {
  4. Int8BufferAttribute,
  5. Uint8BufferAttribute,
  6. Uint8ClampedBufferAttribute,
  7. Int16BufferAttribute,
  8. Uint16BufferAttribute,
  9. Int32BufferAttribute,
  10. Uint32BufferAttribute,
  11. Float16BufferAttribute,
  12. Float32BufferAttribute
  13. } from '../../../../src/core/BufferAttribute.js';
  14. import { DynamicDrawUsage } from '../../../../src/constants.js';
  15. import { toHalfFloat, fromHalfFloat } from '../../../../src/extras/DataUtils.js';
  16. export default QUnit.module( 'Core', () => {
  17. QUnit.module( 'BufferAttribute', () => {
  18. // INSTANCING
  19. QUnit.test( 'Instancing', ( assert ) => {
  20. assert.throws(
  21. function () {
  22. new BufferAttribute( [ 1, 2, 3, 4 ], 2, false );
  23. },
  24. /array should be a Typed Array/,
  25. 'Calling constructor with a simple array throws Error'
  26. );
  27. } );
  28. // PROPERTIES
  29. QUnit.todo( 'name', ( assert ) => {
  30. assert.ok( false, 'everything\'s gonna be alright' );
  31. } );
  32. QUnit.todo( 'array', ( assert ) => {
  33. assert.ok( false, 'everything\'s gonna be alright' );
  34. } );
  35. QUnit.todo( 'itemSize', ( assert ) => {
  36. assert.ok( false, 'everything\'s gonna be alright' );
  37. } );
  38. QUnit.todo( 'count', ( assert ) => {
  39. assert.ok( false, 'everything\'s gonna be alright' );
  40. } );
  41. QUnit.todo( 'normalized', ( assert ) => {
  42. assert.ok( false, 'everything\'s gonna be alright' );
  43. } );
  44. QUnit.todo( 'usage', ( assert ) => {
  45. assert.ok( false, 'everything\'s gonna be alright' );
  46. } );
  47. QUnit.todo( 'updateRanges', ( assert ) => {
  48. assert.ok( false, 'everything\'s gonna be alright' );
  49. } );
  50. QUnit.todo( 'version', ( assert ) => {
  51. assert.ok( false, 'everything\'s gonna be alright' );
  52. } );
  53. QUnit.todo( 'onUploadCallback', ( assert ) => {
  54. // onUploadCallback() {}
  55. // defined as member function but set property. refactor req
  56. assert.ok( false, 'everything\'s gonna be alright' );
  57. } );
  58. QUnit.todo( 'needsUpdate', ( assert ) => {
  59. // set needsUpdate( value )
  60. assert.ok( false, 'everything\'s gonna be alright' );
  61. } );
  62. // PUBLIC
  63. QUnit.test( 'isBufferAttribute', ( assert ) => {
  64. const object = new BufferAttribute();
  65. assert.ok(
  66. object.isBufferAttribute,
  67. 'BufferAttribute.isBufferAttribute should be true'
  68. );
  69. } );
  70. QUnit.test( 'setUsage', ( assert ) => {
  71. const attr = new BufferAttribute();
  72. attr.setUsage( DynamicDrawUsage );
  73. assert.strictEqual( attr.usage, DynamicDrawUsage, 'Usage was set' );
  74. } );
  75. QUnit.test( 'copy', ( assert ) => {
  76. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
  77. attr.setUsage( DynamicDrawUsage );
  78. attr.needsUpdate = true;
  79. const attrCopy = new BufferAttribute().copy( attr );
  80. assert.ok( attr.count === attrCopy.count, 'count is equal' );
  81. assert.ok( attr.itemSize === attrCopy.itemSize, 'itemSize is equal' );
  82. assert.ok( attr.usage === attrCopy.usage, 'usage is equal' );
  83. assert.ok( attr.array.length === attrCopy.array.length, 'array length is equal' );
  84. assert.ok( attr.version === 1 && attrCopy.version === 0, 'version is not copied which is good' );
  85. } );
  86. QUnit.test( 'copyAt', ( assert ) => {
  87. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), 3 );
  88. const attr2 = new BufferAttribute( new Float32Array( 9 ), 3 );
  89. attr2.copyAt( 1, attr, 2 );
  90. attr2.copyAt( 0, attr, 1 );
  91. attr2.copyAt( 2, attr, 0 );
  92. const i = attr.array;
  93. const i2 = attr2.array; // should be [4, 5, 6, 7, 8, 9, 1, 2, 3]
  94. assert.ok( i2[ 0 ] === i[ 3 ] && i2[ 1 ] === i[ 4 ] && i2[ 2 ] === i[ 5 ], 'chunck copied to correct place' );
  95. assert.ok( i2[ 3 ] === i[ 6 ] && i2[ 4 ] === i[ 7 ] && i2[ 5 ] === i[ 8 ], 'chunck copied to correct place' );
  96. assert.ok( i2[ 6 ] === i[ 0 ] && i2[ 7 ] === i[ 1 ] && i2[ 8 ] === i[ 2 ], 'chunck copied to correct place' );
  97. } );
  98. QUnit.test( 'copyArray', ( assert ) => {
  99. const f32a = new Float32Array( [ 5, 6, 7, 8 ] );
  100. const a = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4 ] ), 2, false );
  101. a.copyArray( f32a );
  102. assert.deepEqual( a.array, f32a, 'Check array has new values' );
  103. } );
  104. QUnit.todo( 'applyMatrix3', ( assert ) => {
  105. // applyMatrix3( m )
  106. assert.ok( false, 'everything\'s gonna be alright' );
  107. } );
  108. QUnit.todo( 'applyMatrix4', ( assert ) => {
  109. // applyMatrix4( m )
  110. assert.ok( false, 'everything\'s gonna be alright' );
  111. } );
  112. QUnit.todo( 'applyNormalMatrix', ( assert ) => {
  113. // applyNormalMatrix( m )
  114. assert.ok( false, 'everything\'s gonna be alright' );
  115. } );
  116. QUnit.todo( 'transformDirection', ( assert ) => {
  117. // transformDirection( m )
  118. assert.ok( false, 'everything\'s gonna be alright' );
  119. } );
  120. QUnit.test( 'set', ( assert ) => {
  121. const f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  122. const a = new BufferAttribute( f32a, 2, false );
  123. const expected = new Float32Array( [ 9, 2, 8, 4 ] );
  124. a.set( [ 9 ] );
  125. a.set( [ 8 ], 2 );
  126. assert.deepEqual( a.array, expected, 'Check array has expected values' );
  127. } );
  128. QUnit.test( 'set[X, Y, Z, W, XYZ, XYZW]/get[X, Y, Z, W]', ( assert ) => {
  129. const f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
  130. const a = new BufferAttribute( f32a, 4, false );
  131. const expected = new Float32Array( [ 1, 2, - 3, - 4, - 5, - 6, 7, 8 ] );
  132. a.setX( 1, a.getX( 1 ) * - 1 );
  133. a.setY( 1, a.getY( 1 ) * - 1 );
  134. a.setZ( 0, a.getZ( 0 ) * - 1 );
  135. a.setW( 0, a.getW( 0 ) * - 1 );
  136. assert.deepEqual( a.array, expected, 'Check all set* calls set the correct values' );
  137. } );
  138. QUnit.test( 'setXY', ( assert ) => {
  139. const f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  140. const a = new BufferAttribute( f32a, 2, false );
  141. const expected = new Float32Array( [ - 1, - 2, 3, 4 ] );
  142. a.setXY( 0, - 1, - 2 );
  143. assert.deepEqual( a.array, expected, 'Check for the correct values' );
  144. } );
  145. QUnit.test( 'setXYZ', ( assert ) => {
  146. const f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6 ] );
  147. const a = new BufferAttribute( f32a, 3, false );
  148. const expected = new Float32Array( [ 1, 2, 3, - 4, - 5, - 6 ] );
  149. a.setXYZ( 1, - 4, - 5, - 6 );
  150. assert.deepEqual( a.array, expected, 'Check for the correct values' );
  151. } );
  152. QUnit.test( 'setXYZW', ( assert ) => {
  153. const f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  154. const a = new BufferAttribute( f32a, 4, false );
  155. const expected = new Float32Array( [ - 1, - 2, - 3, - 4 ] );
  156. a.setXYZW( 0, - 1, - 2, - 3, - 4 );
  157. assert.deepEqual( a.array, expected, 'Check for the correct values' );
  158. } );
  159. QUnit.test( 'onUpload', ( assert ) => {
  160. const a = new BufferAttribute();
  161. const func = function () { };
  162. a.onUpload( func );
  163. assert.strictEqual( a.onUploadCallback, func, 'Check callback was set properly' );
  164. } );
  165. QUnit.test( 'clone', ( assert ) => {
  166. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 0.12, - 12 ] ), 2 );
  167. const attrCopy = attr.clone();
  168. assert.ok( attr.array.length === attrCopy.array.length, 'attribute was cloned' );
  169. for ( let i = 0; i < attr.array.length; i ++ ) {
  170. assert.ok( attr.array[ i ] === attrCopy.array[ i ], 'array item is equal' );
  171. }
  172. } );
  173. QUnit.test( 'toJSON', ( assert ) => {
  174. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
  175. assert.deepEqual( attr.toJSON(), {
  176. itemSize: 3,
  177. type: 'Float32Array',
  178. array: [ 1, 2, 3, 4, 5, 6 ],
  179. normalized: false
  180. }, 'Serialized to JSON as expected' );
  181. const attr2 = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3, true );
  182. attr2.name = 'attributeName';
  183. attr2.setUsage( DynamicDrawUsage );
  184. attr2.addUpdateRange( 1, 2 );
  185. assert.deepEqual( attr2.toJSON(), {
  186. itemSize: 3,
  187. type: 'Float32Array',
  188. array: [ 1, 2, 3, 4, 5, 6 ],
  189. normalized: true,
  190. name: 'attributeName',
  191. usage: DynamicDrawUsage,
  192. }, 'Serialized to JSON as expected with non-default values' );
  193. } );
  194. // OTHERS
  195. QUnit.test( 'count', ( assert ) => {
  196. assert.ok(
  197. new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ).count === 2,
  198. 'count is equal to the number of chunks'
  199. );
  200. } );
  201. } );
  202. QUnit.module( 'Int8BufferAttribute', () => {
  203. // INHERITANCE
  204. QUnit.test( 'Extending', ( assert ) => {
  205. const object = new Int8BufferAttribute();
  206. assert.strictEqual(
  207. object instanceof BufferAttribute, true,
  208. 'Int8BufferAttribute extends from BufferAttribute'
  209. );
  210. } );
  211. // INSTANCING
  212. QUnit.test( 'Instancing', ( assert ) => {
  213. const object = new Int8BufferAttribute();
  214. assert.ok( object, 'Can instantiate an Int8BufferAttribute.' );
  215. } );
  216. } );
  217. QUnit.module( 'Uint8BufferAttribute', () => {
  218. // INHERITANCE
  219. QUnit.test( 'Extending', ( assert ) => {
  220. const object = new Uint8BufferAttribute();
  221. assert.strictEqual(
  222. object instanceof BufferAttribute, true,
  223. 'Uint8BufferAttribute extends from BufferAttribute'
  224. );
  225. } );
  226. // INSTANCING
  227. QUnit.test( 'Instancing', ( assert ) => {
  228. const object = new Uint8BufferAttribute();
  229. assert.ok( object, 'Can instantiate a Uint8BufferAttribute.' );
  230. } );
  231. } );
  232. QUnit.module( 'Uint8ClampedBufferAttribute', () => {
  233. // INHERITANCE
  234. QUnit.test( 'Extending', ( assert ) => {
  235. const object = new Uint8ClampedBufferAttribute();
  236. assert.strictEqual(
  237. object instanceof BufferAttribute, true,
  238. 'Uint8ClampedBufferAttribute extends from BufferAttribute'
  239. );
  240. } );
  241. // INSTANCING
  242. QUnit.test( 'Instancing', ( assert ) => {
  243. const object = new Uint8ClampedBufferAttribute();
  244. assert.ok( object, 'Can instantiate a Uint8ClampedBufferAttribute.' );
  245. } );
  246. } );
  247. QUnit.module( 'Int16BufferAttribute', () => {
  248. // INHERITANCE
  249. QUnit.test( 'Extending', ( assert ) => {
  250. const object = new Int16BufferAttribute();
  251. assert.strictEqual(
  252. object instanceof BufferAttribute, true,
  253. 'Int16BufferAttribute extends from BufferAttribute'
  254. );
  255. } );
  256. // INSTANCING
  257. QUnit.test( 'Instancing', ( assert ) => {
  258. const object = new Int16BufferAttribute();
  259. assert.ok( object, 'Can instantiate an Int16BufferAttribute.' );
  260. } );
  261. } );
  262. QUnit.module( 'Uint16BufferAttribute', () => {
  263. // INHERITANCE
  264. QUnit.test( 'Extending', ( assert ) => {
  265. const object = new Uint16BufferAttribute();
  266. assert.strictEqual(
  267. object instanceof BufferAttribute, true,
  268. 'Uint16BufferAttribute extends from BufferAttribute'
  269. );
  270. } );
  271. // INSTANCING
  272. QUnit.test( 'Instancing', ( assert ) => {
  273. const object = new Uint16BufferAttribute();
  274. assert.ok( object, 'Can instantiate a Uint16BufferAttribute.' );
  275. } );
  276. } );
  277. QUnit.module( 'Int32BufferAttribute', () => {
  278. // INHERITANCE
  279. QUnit.test( 'Extending', ( assert ) => {
  280. const object = new Int32BufferAttribute();
  281. assert.strictEqual(
  282. object instanceof BufferAttribute, true,
  283. 'Int32BufferAttribute extends from BufferAttribute'
  284. );
  285. } );
  286. // INSTANCING
  287. QUnit.test( 'Instancing', ( assert ) => {
  288. const object = new Int32BufferAttribute();
  289. assert.ok( object, 'Can instantiate an Int32BufferAttribute.' );
  290. } );
  291. } );
  292. QUnit.module( 'Uint32BufferAttribute', () => {
  293. // INHERITANCE
  294. QUnit.test( 'Extending', ( assert ) => {
  295. const object = new Uint32BufferAttribute();
  296. assert.strictEqual(
  297. object instanceof BufferAttribute, true,
  298. 'Uint32BufferAttribute extends from BufferAttribute'
  299. );
  300. } );
  301. // INSTANCING
  302. QUnit.test( 'Instancing', ( assert ) => {
  303. const object = new Uint32BufferAttribute();
  304. assert.ok( object, 'Can instantiate a Uint32BufferAttribute.' );
  305. } );
  306. } );
  307. QUnit.module( 'Float16BufferAttribute', () => {
  308. // INHERITANCE
  309. QUnit.test( 'Extending', ( assert ) => {
  310. const object = new Float16BufferAttribute();
  311. assert.strictEqual(
  312. object instanceof BufferAttribute, true,
  313. 'Float16BufferAttribute extends from BufferAttribute'
  314. );
  315. } );
  316. // INSTANCING
  317. QUnit.test( 'Instancing', ( assert ) => {
  318. const object = new Float16BufferAttribute();
  319. assert.ok( object, 'Can instantiate a Float16BufferAttribute.' );
  320. } );
  321. const toHalfFloatArray = ( f32Array ) => {
  322. const f16Array = new Uint16Array( f32Array.length );
  323. for ( let i = 0, n = f32Array.length; i < n; ++ i ) {
  324. f16Array[ i ] = toHalfFloat( f32Array[ i ] );
  325. }
  326. return f16Array;
  327. };
  328. const fromHalfFloatArray = ( f16Array ) => {
  329. const f32Array = new Float32Array( f16Array.length );
  330. for ( let i = 0, n = f16Array.length; i < n; ++ i ) {
  331. f32Array[ i ] = fromHalfFloat( f16Array[ i ] );
  332. }
  333. return f32Array;
  334. };
  335. QUnit.test( 'set[X, Y, Z, W, XYZ, XYZW]/get[X, Y, Z, W]', ( assert ) => {
  336. const f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
  337. const a = new Float16BufferAttribute( toHalfFloatArray( f32a ), 4, false );
  338. const expected = new Float32Array( [ 1, 2, - 3, - 4, - 5, - 6, 7, 8 ] );
  339. a.setX( 1, a.getX( 1 ) * - 1 );
  340. a.setY( 1, a.getY( 1 ) * - 1 );
  341. a.setZ( 0, a.getZ( 0 ) * - 1 );
  342. a.setW( 0, a.getW( 0 ) * - 1 );
  343. assert.deepEqual( fromHalfFloatArray( a.array ), expected, 'Check all set* calls set the correct values' );
  344. } );
  345. QUnit.test( 'setXY', ( assert ) => {
  346. const f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  347. const a = new Float16BufferAttribute( toHalfFloatArray( f32a ), 2, false );
  348. const expected = new Float32Array( [ - 1, - 2, 3, 4 ] );
  349. a.setXY( 0, - 1, - 2 );
  350. assert.deepEqual( fromHalfFloatArray( a.array ), expected, 'Check for the correct values' );
  351. } );
  352. QUnit.test( 'setXYZ', ( assert ) => {
  353. const f32a = new Float32Array( [ 1, 2, 3, 4, 5, 6 ] );
  354. const a = new Float16BufferAttribute( toHalfFloatArray( f32a ), 3, false );
  355. const expected = new Float32Array( [ 1, 2, 3, - 4, - 5, - 6 ] );
  356. a.setXYZ( 1, - 4, - 5, - 6 );
  357. assert.deepEqual( fromHalfFloatArray( a.array ), expected, 'Check for the correct values' );
  358. } );
  359. QUnit.test( 'setXYZW', ( assert ) => {
  360. const f32a = new Float32Array( [ 1, 2, 3, 4 ] );
  361. const a = new Float16BufferAttribute( toHalfFloatArray( f32a ), 4, false );
  362. const expected = new Float32Array( [ - 1, - 2, - 3, - 4 ] );
  363. a.setXYZW( 0, - 1, - 2, - 3, - 4 );
  364. assert.deepEqual( fromHalfFloatArray( a.array ), expected, 'Check for the correct values' );
  365. } );
  366. } );
  367. QUnit.module( 'Float32BufferAttribute', () => {
  368. // INHERITANCE
  369. QUnit.test( 'Extending', ( assert ) => {
  370. const object = new Float32BufferAttribute();
  371. assert.strictEqual(
  372. object instanceof BufferAttribute, true,
  373. 'Float32BufferAttribute extends from BufferAttribute'
  374. );
  375. } );
  376. // INSTANCING
  377. QUnit.test( 'Instancing', ( assert ) => {
  378. const object = new Float32BufferAttribute();
  379. assert.ok( object, 'Can instantiate a Float32BufferAttribute.' );
  380. } );
  381. } );
  382. } );