BufferGeometryUtils.tests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @author Don McCurdy / https://www.donmccurdy.com
  3. */
  4. /* global QUnit */
  5. import { BufferGeometryUtils } from '../../../../examples/jsm/utils/BufferGeometryUtils';
  6. import { BufferAttribute } from '../../../../src/core/BufferAttribute';
  7. import { BufferGeometry } from '../../../../src/core/BufferGeometry';
  8. import { TriangleStripDrawMode, TriangleFanDrawMode } from '../../../../src/constants';
  9. export default QUnit.module( 'BufferGeometryUtils', () => {
  10. QUnit.test( 'mergeBufferAttributes - basic', ( assert ) => {
  11. var array1 = new Float32Array( [ 1, 2, 3, 4 ] );
  12. var attr1 = new BufferAttribute( array1, 2, false );
  13. var array2 = new Float32Array( [ 5, 6, 7, 8 ] );
  14. var attr2 = new BufferAttribute( array2, 2, false );
  15. var mergedAttr = BufferGeometryUtils.mergeBufferAttributes( [ attr1, attr2 ] );
  16. assert.smartEqual( Array.from( mergedAttr.array ), [ 1, 2, 3, 4, 5, 6, 7, 8 ], 'merges elements' );
  17. assert.equal( mergedAttr.itemSize, 2, 'retains .itemSize' );
  18. assert.equal( mergedAttr.normalized, false, 'retains .normalized' );
  19. } );
  20. QUnit.test( 'mergeBufferAttributes - invalid', ( assert ) => {
  21. var array1 = new Float32Array( [ 1, 2, 3, 4 ] );
  22. var attr1 = new BufferAttribute( array1, 2, false );
  23. var array2 = new Float32Array( [ 5, 6, 7, 8 ] );
  24. var attr2 = new BufferAttribute( array2, 4, false );
  25. assert.notOk( BufferGeometryUtils.mergeBufferAttributes( [ attr1, attr2 ] ) );
  26. attr2.itemSize = 2;
  27. attr2.normalized = true;
  28. assert.notOk( BufferGeometryUtils.mergeBufferAttributes( [ attr1, attr2 ] ) );
  29. attr2.normalized = false;
  30. assert.ok( BufferGeometryUtils.mergeBufferAttributes( [ attr1, attr2 ] ) );
  31. } );
  32. QUnit.test( 'mergeBufferGeometries - basic', ( assert ) => {
  33. var geometry1 = new BufferGeometry();
  34. geometry1.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
  35. var geometry2 = new BufferGeometry();
  36. geometry2.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 1, false ) );
  37. var mergedGeometry = BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] );
  38. assert.ok( mergedGeometry, 'merge succeeds' );
  39. assert.smartEqual( Array.from( mergedGeometry.attributes.position.array ), [ 1, 2, 3, 4, 5, 6 ], 'merges elements' );
  40. assert.equal( mergedGeometry.attributes.position.itemSize, 1, 'retains .itemSize' );
  41. } );
  42. QUnit.test( 'mergeBufferGeometries - indexed', ( assert ) => {
  43. var geometry1 = new BufferGeometry();
  44. geometry1.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
  45. geometry1.setIndex( new BufferAttribute( new Uint16Array( [ 0, 1, 2, 2, 1, 0 ] ), 1, false ) );
  46. var geometry2 = new BufferGeometry();
  47. geometry2.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 1, false ) );
  48. geometry2.setIndex( new BufferAttribute( new Uint16Array( [ 0, 1, 2 ] ), 1, false ) );
  49. var mergedGeometry = BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] );
  50. assert.ok( mergedGeometry, 'merge succeeds' );
  51. assert.smartEqual( Array.from( mergedGeometry.attributes.position.array ), [ 1, 2, 3, 4, 5, 6 ], 'merges elements' );
  52. assert.smartEqual( Array.from( mergedGeometry.index.array ), [ 0, 1, 2, 2, 1, 0, 3, 4, 5 ], 'merges indices' );
  53. assert.equal( mergedGeometry.attributes.position.itemSize, 1, 'retains .itemSize' );
  54. } );
  55. QUnit.test( 'mergeBufferGeometries - morph targets', ( assert ) => {
  56. var geometry1 = new BufferGeometry();
  57. geometry1.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
  58. geometry1.morphAttributes.position = [
  59. new BufferAttribute( new Float32Array( [ 10, 20, 30 ] ), 1, false ),
  60. new BufferAttribute( new Float32Array( [ 100, 200, 300 ] ), 1, false )
  61. ];
  62. var geometry2 = new BufferGeometry();
  63. geometry2.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 1, false ) );
  64. geometry2.morphAttributes.position = [
  65. new BufferAttribute( new Float32Array( [ 40, 50, 60 ] ), 1, false ),
  66. new BufferAttribute( new Float32Array( [ 400, 500, 600 ] ), 1, false )
  67. ];
  68. var mergedGeometry = BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] );
  69. assert.ok( mergedGeometry, 'merge succeeds' );
  70. assert.smartEqual( Array.from( mergedGeometry.attributes.position.array ), [ 1, 2, 3, 4, 5, 6 ], 'merges elements' );
  71. assert.smartEqual( Array.from( mergedGeometry.morphAttributes.position[ 0 ].array ), [ 10, 20, 30, 40, 50, 60 ], 'merges morph targets' );
  72. assert.smartEqual( Array.from( mergedGeometry.morphAttributes.position[ 1 ].array ), [ 100, 200, 300, 400, 500, 600 ], 'merges morph targets' );
  73. assert.equal( mergedGeometry.attributes.position.itemSize, 1, 'retains .itemSize' );
  74. } );
  75. QUnit.test( 'mergeBufferGeometries - invalid', ( assert ) => {
  76. var geometry1 = new BufferGeometry();
  77. geometry1.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
  78. geometry1.setIndex( new BufferAttribute( new Uint16Array( [ 0, 1, 2 ] ), 1, false ) );
  79. var geometry2 = new BufferGeometry();
  80. geometry2.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 4, 5, 6 ] ), 1, false ) );
  81. assert.notOk( BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] ) );
  82. geometry2.setIndex( new BufferAttribute( new Uint16Array( [ 0, 1, 2 ] ), 1, false ) );
  83. assert.ok( BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] ) );
  84. geometry2.setAttribute( 'foo', new BufferAttribute( new Float32Array( [ 1, 2, 3 ] ), 1, false ) );
  85. assert.notOk( BufferGeometryUtils.mergeBufferGeometries( [ geometry1, geometry2 ] ) );
  86. } );
  87. QUnit.test( 'toTrianglesDrawMode()', ( assert ) => {
  88. // TRIANGLE_STRIP
  89. const vertices1 = [];
  90. vertices1.push( 0, 0, 0 ); // v0
  91. vertices1.push( 1, 0, 0 ); // v1
  92. vertices1.push( 0, 1, 0 ); // v2
  93. vertices1.push( 2, 0, 0 ); // v3
  94. vertices1.push( 2, 1, 0 ); // v4
  95. vertices1.push( 3, 0, 0 ); // v5
  96. var geometry1 = new BufferGeometry();
  97. geometry1.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices1 ), 3 ) );
  98. geometry1 = BufferGeometryUtils.toTrianglesDrawMode( geometry1, TriangleStripDrawMode );
  99. assert.deepEqual( geometry1.index.array, new Uint16Array( [ 0, 1, 2, 3, 2, 1, 2, 3, 4, 5, 4, 3 ] ), 'correct triangle indices from triangle strip' );
  100. // TRIANGLE_FAN
  101. const vertices2 = [];
  102. vertices2.push( 0, 0, 0 ); // v0
  103. vertices2.push( 1, 0, 0 ); // v1
  104. vertices2.push( 1, 1, 0 ); // v2
  105. vertices2.push( 0, 1, 0 ); // v3
  106. vertices2.push( - 1, 1, 0 ); // v4
  107. vertices2.push( - 1, 0, 0 ); // v5
  108. var geometry2 = new BufferGeometry();
  109. geometry2.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices2 ), 3 ) );
  110. geometry2 = BufferGeometryUtils.toTrianglesDrawMode( geometry2, TriangleFanDrawMode );
  111. assert.deepEqual( geometry2.index.array, new Uint16Array( [ 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5 ] ), 'correct triangle indices from triangle fan' );
  112. } );
  113. } );