diff --git a/src/mesh.ts b/src/mesh.ts index 935a0b4..b3bbcee 100644 --- a/src/mesh.ts +++ b/src/mesh.ts @@ -51,6 +51,7 @@ export class Mesh extends Warnable { this._loadedTextures = {}; } + // TODO: Always check public processMesh() { this._checkMesh(); this._checkMaterials(); diff --git a/src/util.ts b/src/util.ts index db0a829..d2a4542 100644 --- a/src/util.ts +++ b/src/util.ts @@ -295,6 +295,7 @@ export const PALETTES_DIR = path.join(RESOURCES_DIR, './palettes/'); export const STATIC_DIR = path.join(RESOURCES_DIR, './static/'); export const SHADERS_DIR = path.join(RESOURCES_DIR, './shaders/'); export const TOOLS_DIR = path.join(BASE_DIR, './tools/'); +export const TESTS_DATA_DIR = path.join(BASE_DIR, './tests/data/'); export function getRandomID(): string { return (Math.random() + 1).toString(36).substring(7); diff --git a/src/voxel_mesh.ts b/src/voxel_mesh.ts index a5d3d9e..c8f0a73 100644 --- a/src/voxel_mesh.ts +++ b/src/voxel_mesh.ts @@ -18,7 +18,7 @@ export interface VoxelMeshParams { desiredHeight: number, useMultisampleColouring: boolean, textureFiltering: TextureFiltering, - ambientOcclusionEnabled: boolean, + ambientOcclusionEnabled: boolean, // TODO: Refactor into rendering } export class VoxelMesh { @@ -55,6 +55,13 @@ export class VoxelMesh { return this._voxelsHash.has(pos); } + public getVoxelAt(pos: Vector3) { + const voxelIndex = this._voxelsHash.get(pos); + if (voxelIndex !== undefined) { + return this._voxels[voxelIndex]; + } + } + public getMesh() { return this._mesh; } diff --git a/tests/data/cube.mtl b/tests/data/cube.mtl new file mode 100644 index 0000000..8272758 --- /dev/null +++ b/tests/data/cube.mtl @@ -0,0 +1,82 @@ +# Blender 3.1.0 MTL File: 'None' +# www.blender.org + +newmtl black +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.000000 0.000000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl blue +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.000000 0.000000 1.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl cyan +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.000000 1.000000 1.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl green +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.000000 1.000000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl magenta +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 1.000000 0.000000 1.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl red +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 1.000000 0.000000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl white +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 1.000000 1.000000 1.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 + +newmtl yellow +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 1.000000 1.000000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/tests/data/cube.obj b/tests/data/cube.obj new file mode 100644 index 0000000..52f9728 --- /dev/null +++ b/tests/data/cube.obj @@ -0,0 +1,123 @@ +# Blender 3.1.0 +# www.blender.org +mtllib cube.mtl +o Cube +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +v 1.000000 1.000000 0.000000 +v -1.000000 0.000000 1.000000 +v -1.000000 0.000000 -1.000000 +v 1.000000 0.000000 -1.000000 +v 1.000000 0.000000 1.000000 +v 1.000000 0.000000 0.000000 +v -1.000000 0.000000 0.000000 +v 0.000000 -1.000000 -1.000000 +v 0.000000 1.000000 -1.000000 +v 0.000000 -1.000000 1.000000 +v 0.000000 1.000000 1.000000 +v 0.000000 1.000000 0.000000 +v 0.000000 -1.000000 0.000000 +v 0.000000 0.000000 1.000000 +v 0.000000 0.000000 -1.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.375000 0.000000 +vt 0.375000 1.000000 +vt 0.125000 0.750000 +vt 0.625000 1.000000 +vt 0.875000 0.750000 +vt 0.625000 0.000000 +vt 0.375000 0.250000 +vt 0.125000 0.500000 +vt 0.875000 0.500000 +vt 0.625000 0.250000 +vt 0.375000 0.750000 +vt 0.625000 0.750000 +vt 0.375000 0.500000 +vt 0.625000 0.500000 +vt 0.375000 0.125000 +vt 0.125000 0.625000 +vt 0.875000 0.625000 +vt 0.625000 0.125000 +vt 0.375000 0.625000 +vt 0.625000 0.625000 +vt 0.500000 0.000000 +vt 0.500000 1.000000 +vt 0.500000 0.250000 +vt 0.500000 0.500000 +vt 0.500000 0.750000 +vt 0.500000 0.625000 +vt 0.500000 0.125000 +vt 0.375000 0.375000 +vt 0.250000 0.500000 +vt 0.750000 0.500000 +vt 0.625000 0.375000 +vt 0.375000 0.875000 +vt 0.250000 0.750000 +vt 0.750000 0.750000 +vt 0.625000 0.875000 +vt 0.750000 0.625000 +vt 0.250000 0.625000 +vt 0.500000 0.875000 +vt 0.500000 0.375000 +s 0 +usemtl blue +f 18/27/1 10/18/1 4/10/1 14/23/1 +usemtl green +f 26/39/2 20/31/2 8/14/2 15/24/2 +usemtl red +f 17/26/3 12/20/3 6/12/3 16/25/3 +usemtl white +f 25/38/4 22/35/4 2/4/4 13/22/4 +usemtl yellow +f 24/37/5 11/19/5 5/11/5 21/33/5 +usemtl white +f 23/36/6 10/17/6 2/5/6 22/34/6 +usemtl blue +f 20/30/6 4/9/6 10/17/6 23/36/6 +usemtl cyan +f 19/29/5 7/13/5 11/19/5 24/37/5 +usemtl green +f 15/24/3 8/14/3 12/20/3 17/26/3 +usemtl white +f 13/21/1 2/6/1 10/18/1 18/27/1 +usemtl black +f 1/1/1 13/21/1 18/27/1 9/15/1 +usemtl cyan +f 7/13/3 15/24/3 17/26/3 11/19/3 +usemtl black +f 21/32/4 25/38/4 13/22/4 1/2/4 +usemtl yellow +f 11/19/3 17/26/3 16/25/3 5/11/3 +usemtl cyan +f 19/28/2 26/39/2 15/24/2 7/13/2 +usemtl magenta +f 9/15/1 18/27/1 14/23/1 3/7/1 +f 3/7/2 14/23/2 26/39/2 19/28/2 +usemtl yellow +f 5/11/4 16/25/4 25/38/4 21/32/4 +usemtl magenta +f 3/8/5 19/29/5 24/37/5 9/16/5 +usemtl green +f 8/14/6 20/30/6 23/36/6 12/20/6 +usemtl red +f 12/20/6 23/36/6 22/34/6 6/12/6 +usemtl black +f 9/16/5 24/37/5 21/33/5 1/3/5 +usemtl red +f 16/25/4 6/12/4 22/35/4 25/38/4 +usemtl blue +f 14/23/2 4/10/2 20/31/2 26/39/2 diff --git a/tests/normal-corrected-ray-voxeliser.test.ts b/tests/normal-corrected-ray-voxeliser.test.ts new file mode 100644 index 0000000..54a728b --- /dev/null +++ b/tests/normal-corrected-ray-voxeliser.test.ts @@ -0,0 +1,64 @@ +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('Parse vertex #1', () => { + 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())); + } +});