BufferAttribute.tests.js 15 KB

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