ObjToSchematic/tests/normal-corrected-ray-voxeliser.test.ts
2022-03-25 18:26:09 +00:00

82 lines
2.5 KiB
TypeScript

import { NormalCorrectedRayVoxeliser } from '../src/voxelisers/normal-corrected-ray-voxeliser';
import { ObjImporter } from '../src/importers/obj_importer';
import { TextureFiltering } from '../src/texture';
import { ASSERT, RGB } from '../src/util';
import { Vector3 } from '../src/vector';
import path from 'path';
test('Voxelise solid 2x2 cube', () => {
const importer = new ObjImporter();
importer.parseFile(path.join(__dirname, './data/cube.obj'));
const mesh = importer.toMesh();
mesh.processMesh();
const voxeliser = new NormalCorrectedRayVoxeliser();
const voxelMesh = voxeliser.voxelise(mesh, {
desiredHeight: 2,
useMultisampleColouring: false,
textureFiltering: TextureFiltering.Nearest,
ambientOcclusionEnabled: false,
});
const expectedVoxels = [
{
position: new Vector3(0, 1, 0),
colour: new RGB(0, 0, 1),
},
{
position: new Vector3(1, 1, 0),
colour: new RGB(0, 1, 0),
},
{
position: new Vector3(1, 1, 1),
colour: new RGB(1, 0, 0),
},
{
position: new Vector3(0, 1, 1),
colour: new RGB(1, 1, 1),
},
{
position: new Vector3(1, 0, 1),
colour: new RGB(1, 1, 0),
},
{
position: new Vector3(1, 0, 0),
colour: new RGB(0, 1, 1),
},
{
position: new Vector3(0, 0, 1),
colour: new RGB(0, 0, 0),
},
{
position: new Vector3(0, 0, 0),
colour: new RGB(1, 0, 1),
},
];
for (const expected of expectedVoxels) {
expect(voxelMesh.isVoxelAt(expected.position)).toBe(true);
const voxel = voxelMesh.getVoxelAt(expected.position);
expect(voxel).toBeDefined(); ASSERT(voxel);
expect(voxel.colour.toVector3().equals(expected.colour.toVector3()));
}
});
test('Voxelise textured 2x2 cube', () => {
const importer = new ObjImporter();
importer.parseFile(path.join(__dirname, './data/textured-cube.obj'));
const mesh = importer.toMesh();
mesh.processMesh();
const voxeliser = new NormalCorrectedRayVoxeliser();
const voxelMesh = voxeliser.voxelise(mesh, {
desiredHeight: 2,
useMultisampleColouring: false,
textureFiltering: TextureFiltering.Nearest,
ambientOcclusionEnabled: false,
});
console.log(voxelMesh.getVoxels());
});