ObjToSchematic/tests/voxel_mesh.test.ts

68 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-06-12 09:45:23 +08:00
import { RGBAColours } from '../src/colour';
import { ASSERT } from '../src/util/error_util';
import { Vector3 } from '../src/vector';
import { VoxelMesh } from '../src/voxel_mesh';
import { TEST_PREAMBLE } from './preamble';
test('Voxel neighbours', () => {
TEST_PREAMBLE();
2022-06-19 10:28:06 +08:00
const voxelMesh = new VoxelMesh({
voxelOverlapRule: 'first',
enableAmbientOcclusion: true,
2022-06-19 10:28:06 +08:00
});
2022-06-12 09:45:23 +08:00
voxelMesh.addVoxel(new Vector3(1, 2, 3), RGBAColours.WHITE);
expect(voxelMesh.getNeighbours(new Vector3(1, 2, 3)).value).toBe(0);
// Even though this neighbour does exist, it is not calculated as
// this relationship is never tested
expect(voxelMesh.hasNeighbour(new Vector3(1, 2, 3).add(new Vector3(1, 0, 0)), new Vector3(-1, 0, 0))).toBe(false);
expect(voxelMesh.hasNeighbour(new Vector3(1, 2, 3).add(new Vector3(1, 1, 0)), new Vector3(-1, -1, 0))).toBe(true);
expect(voxelMesh.hasNeighbour(new Vector3(1, 2, 3).add(new Vector3(-1, -1, 0)), new Vector3(1, 1, 0))).toBe(true);
expect(voxelMesh.hasNeighbour(new Vector3(1, 2, 3).add(new Vector3(1, -1, 1)), new Vector3(-1, 1, -1))).toBe(true);
expect(voxelMesh.hasNeighbour(new Vector3(1, 2, 3).add(new Vector3(-1, 0, 1)), new Vector3(1, 0, -1))).toBe(true);
});
2022-06-19 10:28:06 +08:00
test('Add voxel', () => {
const voxelMesh = new VoxelMesh({
voxelOverlapRule: 'first',
enableAmbientOcclusion: true,
2022-06-19 10:28:06 +08:00
});
2022-07-15 00:59:35 +08:00
voxelMesh.addVoxel(new Vector3(1, 2, 3), RGBAColours.RED);
2022-06-19 10:28:06 +08:00
expect(voxelMesh.isVoxelAt(new Vector3(1, 2, 3))).toBe(true);
expect(voxelMesh.getVoxelCount()).toBe(1);
const voxel = voxelMesh.getVoxelAt(new Vector3(1, 2, 3));
expect(voxel).toBeDefined(); ASSERT(voxel);
expect(voxel.position.equals(new Vector3(1, 2, 3))).toBe(true);
2022-07-15 00:59:35 +08:00
expect(voxel.colour).toEqual(RGBAColours.RED);
2022-06-19 10:28:06 +08:00
});
test('Voxel overlap first', () => {
const voxelMesh = new VoxelMesh({
voxelOverlapRule: 'first',
enableAmbientOcclusion: false,
2022-06-19 10:28:06 +08:00
});
2022-07-15 00:59:35 +08:00
voxelMesh.addVoxel(new Vector3(1, 2, 3), RGBAColours.RED);
voxelMesh.addVoxel(new Vector3(1, 2, 3), RGBAColours.BLUE);
2022-06-19 10:28:06 +08:00
2022-07-15 00:59:35 +08:00
expect(voxelMesh.getVoxelAt(new Vector3(1, 2, 3))?.colour).toEqual(RGBAColours.RED);
2022-06-19 10:28:06 +08:00
});
test('Voxel overlap average', () => {
const voxelMesh = new VoxelMesh({
voxelOverlapRule: 'average',
enableAmbientOcclusion: false,
2022-06-19 10:28:06 +08:00
});
2022-07-15 00:59:35 +08:00
voxelMesh.addVoxel(new Vector3(1, 2, 3), { r: 1.0, g: 0.5, b: 0.25, a: 1.0 });
voxelMesh.addVoxel(new Vector3(1, 2, 3), { r: 0.0, g: 0.5, b: 0.75, a: 1.0 });
2022-06-19 10:28:06 +08:00
expect(voxelMesh.getVoxelAt(new Vector3(1, 2, 3))?.colour).toEqual({ r: 0.5, g: 0.5, b: 0.5, a: 1.0 });
2022-06-19 10:28:06 +08:00
});