智慧水务管理系统 - 精河县供水工程综合管理平台

meshopt_clusterizer.test.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import assert from 'assert/strict';
  2. import { MeshoptClusterizer as clusterizer } from './meshopt_clusterizer.js';
  3. process.on('unhandledRejection', (error) => {
  4. console.log('unhandledRejection', error);
  5. process.exit(1);
  6. });
  7. const cubeWithNormals = {
  8. vertices: new Float32Array([
  9. // n = (0, 0, 1)
  10. -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0,
  11. // n = (0, 0, -1)
  12. -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, -1.0,
  13. // n = (1, 0, 0)
  14. 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
  15. // n = (-1, 0, 0)
  16. -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0,
  17. // n = (0, 1, 0)
  18. 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0,
  19. // n = (0, -1, 0)
  20. 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, -1.0, 0.0,
  21. ]),
  22. indices: new Uint32Array([
  23. // n = (0, 0, 1)
  24. 0, 1, 2, 2, 3, 0,
  25. // n = (0, 0, -1)
  26. 4, 5, 6, 6, 7, 4,
  27. // n = (1, 0, 0)
  28. 8, 9, 10, 10, 11, 8,
  29. // n = (-1, 0, 0)
  30. 12, 13, 14, 14, 15, 12,
  31. // n = (0, 1, 0)
  32. 16, 17, 18, 18, 19, 16,
  33. // n = (0, -1, 0)
  34. 20, 21, 22, 22, 23, 20,
  35. ]),
  36. vertexStride: 6, // in floats
  37. };
  38. const tests = {
  39. buildMeshlets: function () {
  40. const maxVertices = 4;
  41. const buffers = clusterizer.buildMeshlets(cubeWithNormals.indices, cubeWithNormals.vertices, cubeWithNormals.vertexStride, maxVertices, 512);
  42. const expectedVertices = [
  43. new Uint32Array([6, 7, 4, 5]),
  44. new Uint32Array([14, 15, 12, 13]),
  45. new Uint32Array([2, 3, 0, 1]),
  46. new Uint32Array([20, 21, 22, 23]),
  47. new Uint32Array([10, 11, 8, 9]),
  48. new Uint32Array([18, 19, 16, 17]),
  49. ];
  50. const expectedTriangles = new Uint8Array([0, 1, 2, 2, 3, 0]);
  51. assert.equal(buffers.meshletCount, 6);
  52. for (let i = 0; i < buffers.meshletCount; ++i) {
  53. const m = clusterizer.extractMeshlet(buffers, i);
  54. assert.deepStrictEqual(m.vertices, expectedVertices[i]);
  55. assert.deepStrictEqual(m.triangles, expectedTriangles);
  56. }
  57. },
  58. computeClusterBounds: function () {
  59. for (let i = 0; i < 6; ++i) {
  60. const indexOffset = i * 6;
  61. const normalOffset = i * 4 * cubeWithNormals.vertexStride;
  62. const bounds = clusterizer.computeClusterBounds(
  63. cubeWithNormals.indices.subarray(indexOffset, 6 + indexOffset),
  64. cubeWithNormals.vertices,
  65. cubeWithNormals.vertexStride
  66. );
  67. assert.deepStrictEqual(
  68. new Int32Array([bounds.coneAxisX, bounds.coneAxisY, bounds.coneAxisZ]),
  69. new Int32Array(cubeWithNormals.vertices.subarray(3 + normalOffset, 6 + normalOffset))
  70. );
  71. }
  72. },
  73. computeMeshletBounds: function () {
  74. const maxVertices = 4;
  75. const buffers = clusterizer.buildMeshlets(cubeWithNormals.indices, cubeWithNormals.vertices, cubeWithNormals.vertexStride, maxVertices, 512);
  76. const expectedNormals = [
  77. new Int32Array([0, 0, -1]),
  78. new Int32Array([-1, 0, 0]),
  79. new Int32Array([0, 0, 1]),
  80. new Int32Array([0, -1, 0]),
  81. new Int32Array([1, 0, 0]),
  82. new Int32Array([0, 1, 0]),
  83. ];
  84. const bounds = clusterizer.computeMeshletBounds(buffers, cubeWithNormals.vertices, cubeWithNormals.vertexStride);
  85. assert(bounds.length === 6);
  86. assert(bounds.length === buffers.meshletCount);
  87. bounds.forEach((b, i) => {
  88. const normal = new Int32Array([b.coneAxisX, b.coneAxisY, b.coneAxisZ]);
  89. assert.deepStrictEqual(normal, expectedNormals[i]);
  90. });
  91. },
  92. computeSphereBounds: function () {
  93. // positions without per-point radii (tetrahedron)
  94. const positions = new Float32Array([0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1]);
  95. const bp = clusterizer.computeSphereBounds(positions, 3);
  96. assert(Math.abs(bp.centerX - 0.5) < 1e-3);
  97. assert(Math.abs(bp.centerY - 0.5) < 1e-3);
  98. assert(Math.abs(bp.centerZ - 0.5) < 1e-3);
  99. assert(bp.radius < 0.87);
  100. // use 4th float of each element as radius (last point has radius=3 enveloping others)
  101. const radii = new Float32Array([0, 1, 2, 3]);
  102. const br = clusterizer.computeSphereBounds(positions, 3, radii, 1);
  103. assert(Math.abs(br.centerX - 1.0) < 1e-3);
  104. assert(Math.abs(br.centerY - 0.0) < 1e-3);
  105. assert(Math.abs(br.centerZ - 1.0) < 1e-3);
  106. assert(Math.abs(br.radius - 3.0) < 1e-3);
  107. },
  108. };
  109. clusterizer.ready.then(() => {
  110. var count = 0;
  111. for (var key in tests) {
  112. tests[key]();
  113. count++;
  114. }
  115. console.log(count, 'tests passed');
  116. });