Renamed VoxelMeshParams to VoxeliseParams

This commit is contained in:
Lucas Dower 2022-06-19 02:42:56 +01:00
parent 324efd2cd8
commit e3d0622417
10 changed files with 60 additions and 49 deletions

View File

@ -5,7 +5,7 @@ import { ObjImporter } from './importers/obj_importer';
import { ASSERT, ColourSpace, AppError, LOG, LOG_ERROR, TIME_START, TIME_END } from './util';
import { remote } from 'electron';
import { VoxelMesh, VoxelMeshParams } from './voxel_mesh';
import { VoxelMesh } from './voxel_mesh';
import { BlockMesh, BlockMeshParams, FallableBehaviour } from './block_mesh';
import { TextureFiltering } from './texture';
import { IVoxeliser } from './voxelisers/base-voxeliser';
@ -13,7 +13,7 @@ import { StatusHandler } from './status';
import { UIMessageBuilder } from './ui/misc';
import { OutputStyle } from './ui/elements/output';
import { IExporter } from './exporters/base_exporter';
import { TVoxelisers, VoxeliserFactory } from './voxelisers/voxelisers';
import { TVoxelisers, VoxeliseParams, VoxeliserFactory } from './voxelisers/voxelisers';
import { ExporterFactory, TExporters } from './exporters/exporters';
/* eslint-disable */
@ -151,7 +151,7 @@ export class AppContext {
ASSERT(this._loadedMesh);
const uiElements = this._ui.layout.build.elements;
const voxelMeshParams: VoxelMeshParams = {
const voxeliseParams: VoxeliseParams = {
desiredHeight: uiElements.height.getDisplayValue(),
useMultisampleColouring: uiElements.multisampleColouring.getCachedValue() === 'on',
textureFiltering: uiElements.textureFiltering.getCachedValue() === 'linear' ? TextureFiltering.Linear : TextureFiltering.Nearest,
@ -163,13 +163,13 @@ export class AppContext {
TIME_START('Voxelising');
{
this._loadedVoxelMesh = voxeliser.voxelise(this._loadedMesh, voxelMeshParams);
this._loadedVoxelMesh = voxeliser.voxelise(this._loadedMesh, voxeliseParams);
}
TIME_END('Voxelising');
TIME_START('Render Voxel Mesh');
{
const voxelSize = 8.0 / voxelMeshParams.desiredHeight;
Renderer.Get.useVoxelMesh(this._loadedVoxelMesh, voxelSize, voxelMeshParams.enableAmbientOcclusion);
const voxelSize = 8.0 / voxeliseParams.desiredHeight;
Renderer.Get.useVoxelMesh(this._loadedVoxelMesh, voxelSize, voxeliseParams.enableAmbientOcclusion);
}
TIME_END('Render Voxel Mesh');
}

View File

@ -1,6 +1,6 @@
import { BlockMesh } from '../block_mesh';
import { IExporter } from './base_exporter';
import { ASSERT, ATLASES_DIR, RESOURCES_DIR } from '../util';
import { ASSERT, ATLASES_DIR } from '../util';
import fs from 'fs';
import path from 'path';

View File

@ -3,21 +3,18 @@ import { AppConstants } from './constants';
import { GeometryTemplates } from './geometry';
import { HashMap } from './hash_map';
import { OcclusionManager } from './occlusion';
import { TextureFiltering } from './texture';
import { Bounds, RGB } from './util';
import { Vector3 } from './vector';
export interface Voxel {
export type Voxel = {
position: Vector3;
colour: RGB;
collisions: number;
}
export interface VoxelMeshParams {
desiredHeight: number,
useMultisampleColouring: boolean,
textureFiltering: TextureFiltering,
enableAmbientOcclusion: boolean,
/** These are the parameters required to create a Voxel Mesh */
export type VoxelMeshParams = {
}
export class VoxelMesh {

View File

@ -2,13 +2,14 @@ import { UVTriangle, Triangle } from '../triangle';
import { RGB, UV } from '../util';
import { Vector3 } from '../vector';
import { Mesh } from '../mesh';
import { VoxelMesh, VoxelMeshParams } from '../voxel_mesh';
import { VoxelMesh } from '../voxel_mesh';
import { TextureFiltering } from '../texture';
import { StatusHandler } from '../status';
import { VoxeliseParams } from './voxelisers';
export abstract class IVoxeliser {
public voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh {
const voxelMesh = this._voxelise(mesh, voxelMeshParams);
public voxelise(mesh: Mesh, voxeliseParams: VoxeliseParams): VoxelMesh {
const voxelMesh = this._voxelise(mesh, voxeliseParams);
StatusHandler.Get.add('info', `Voxel mesh has ${voxelMesh.getVoxelCount().toLocaleString()} voxels`);
@ -18,7 +19,7 @@ export abstract class IVoxeliser {
return voxelMesh;
}
protected abstract _voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh;
protected abstract _voxelise(mesh: Mesh, voxeliseParams: VoxeliseParams): VoxelMesh;
protected _getVoxelColour(mesh: Mesh, triangle: UVTriangle, materialName: string, location: Vector3, filtering: TextureFiltering): (RGB | undefined) {
const area01 = new Triangle(triangle.v0, triangle.v1, location).getArea();

View File

@ -1,9 +1,10 @@
import { VoxelMeshParams, VoxelMesh } from '../voxel_mesh';
import { VoxelMesh } from '../voxel_mesh';
import { Mesh } from '../mesh';
import { Axes, axesToDirection, Ray } from '../ray';
import { ASSERT, LOG } from '../util';
import { Vector3 } from '../vector';
import { IVoxeliser } from './base-voxeliser';
import { VoxeliseParams } from './voxelisers';
const bvhtree = require('bvh-tree');
@ -12,10 +13,10 @@ const bvhtree = require('bvh-tree');
* on each of the principle angles and testing for intersections
*/
export class BVHRayVoxeliser extends IVoxeliser {
protected override _voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh {
protected override _voxelise(mesh: Mesh, voxeliseParams: VoxeliseParams): VoxelMesh {
const voxelMesh = new VoxelMesh();
const scale = (voxelMeshParams.desiredHeight - 1) / Mesh.desiredHeight;
const offset = (voxelMeshParams.desiredHeight % 2 === 0) ? new Vector3(0.0, 0.5, 0.0) : new Vector3(0.0, 0.0, 0.0);
const scale = (voxeliseParams.desiredHeight - 1) / Mesh.desiredHeight;
const offset = (voxeliseParams.desiredHeight % 2 === 0) ? new Vector3(0.0, 0.5, 0.0) : new Vector3(0.0, 0.0, 0.0);
const useMesh = mesh.copy(); // TODO: Voxelise without copying mesh, too expensive for dense meshes
useMesh.scaleMesh(scale);
@ -88,7 +89,7 @@ export class BVHRayVoxeliser extends IVoxeliser {
useMesh.getUVTriangle(intersection.triangleIndex),
useMesh.getMaterialByTriangle(intersection.triangleIndex),
position,
voxelMeshParams.textureFiltering,
voxeliseParams.textureFiltering,
);
if (voxelColour) {

View File

@ -1,4 +1,4 @@
import { VoxelMeshParams, VoxelMesh } from '../voxel_mesh';
import { VoxelMesh } from '../voxel_mesh';
import { AppConfig } from '../config';
import { Mesh } from '../mesh';
import { Axes, Ray, rayIntersectTriangle } from '../ray';
@ -6,6 +6,7 @@ import { Triangle, UVTriangle } from '../triangle';
import { Bounds, RGB, UV } from '../util';
import { Vector3 } from '../vector';
import { IVoxeliser } from './base-voxeliser';
import { VoxeliseParams } from './voxelisers';
/**
* This voxeliser works by projecting rays onto each triangle
@ -14,17 +15,17 @@ import { IVoxeliser } from './base-voxeliser';
export class NormalCorrectedRayVoxeliser extends IVoxeliser {
private _mesh?: Mesh;
private _voxelMesh?: VoxelMesh;
private _voxelMeshParams?: VoxelMeshParams;
private _voxeliseParams?: VoxeliseParams;
private _scale!: number;
private _size!: Vector3;
private _offset!: Vector3;
protected override _voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh {
protected override _voxelise(mesh: Mesh, voxeliseParams: VoxeliseParams): VoxelMesh {
this._mesh = mesh;
this._voxelMesh = new VoxelMesh();
this._voxelMeshParams = voxelMeshParams;
this._voxeliseParams = voxeliseParams;
this._scale = (voxelMeshParams.desiredHeight) / Mesh.desiredHeight;
this._scale = (voxeliseParams.desiredHeight) / Mesh.desiredHeight;
const useMesh = mesh.copy(); // TODO: Voxelise without copying mesh, too expensive for dense meshes
useMesh.scaleMesh(this._scale);
@ -75,7 +76,7 @@ export class NormalCorrectedRayVoxeliser extends IVoxeliser {
voxelPosition.round();
let voxelColour: RGB;
if (this._voxelMeshParams!.useMultisampleColouring) {
if (this._voxeliseParams!.useMultisampleColouring) {
const samples: RGB[] = [];
for (let i = 0; i < AppConfig.MULTISAMPLE_COUNT; ++i) {
const samplePosition = Vector3.add(voxelPosition, Vector3.random().add(-0.5));
@ -107,7 +108,7 @@ export class NormalCorrectedRayVoxeliser extends IVoxeliser {
triangle.uv0.v * w0 + triangle.uv1.v * w1 + triangle.uv2.v * w2,
);
return this._mesh!.sampleMaterial(materialName, uv, this._voxelMeshParams!.textureFiltering);
return this._mesh!.sampleMaterial(materialName, uv, this._voxeliseParams!.textureFiltering);
}
private _generateRays(v0: Vector3, v1: Vector3, v2: Vector3, offset: Vector3): Array<Ray> {

View File

@ -1,4 +1,4 @@
import { VoxelMeshParams, VoxelMesh } from '../voxel_mesh';
import { VoxelMesh } from '../voxel_mesh';
import { AppConfig } from '../config';
import { Mesh } from '../mesh';
import { Axes, Ray, rayIntersectTriangle } from '../ray';
@ -6,6 +6,7 @@ import { Triangle, UVTriangle } from '../triangle';
import { Bounds, RGB, UV } from '../util';
import { Vector3 } from '../vector';
import { IVoxeliser } from './base-voxeliser';
import { VoxeliseParams } from './voxelisers';
/**
* This voxeliser works by projecting rays onto each triangle
@ -14,17 +15,17 @@ import { IVoxeliser } from './base-voxeliser';
export class RayVoxeliser extends IVoxeliser {
private _mesh?: Mesh;
private _voxelMesh?: VoxelMesh;
private _voxelMeshParams?: VoxelMeshParams;
private _voxeliseParams?: VoxeliseParams;
private _scale!: number;
private _offset!: Vector3;
protected override _voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh {
protected override _voxelise(mesh: Mesh, voxeliseParams: VoxeliseParams): VoxelMesh {
this._mesh = mesh;
this._voxelMesh = new VoxelMesh();
this._voxelMeshParams = voxelMeshParams;
this._voxeliseParams = voxeliseParams;
this._scale = (voxelMeshParams.desiredHeight - 1) / Mesh.desiredHeight;
this._offset = (voxelMeshParams.desiredHeight % 2 === 0) ? new Vector3(0.0, 0.5, 0.0) : new Vector3(0.0, 0.0, 0.0);
this._scale = (voxeliseParams.desiredHeight - 1) / Mesh.desiredHeight;
this._offset = (voxeliseParams.desiredHeight % 2 === 0) ? new Vector3(0.0, 0.5, 0.0) : new Vector3(0.0, 0.0, 0.0);
const useMesh = mesh.copy(); // TODO: Voxelise without copying mesh, too expensive for dense meshes
useMesh.scaleMesh(this._scale);
@ -59,7 +60,7 @@ export class RayVoxeliser extends IVoxeliser {
}
let voxelColour: RGB;
if (this._voxelMeshParams!.useMultisampleColouring) {
if (this._voxeliseParams!.useMultisampleColouring) {
const samples: RGB[] = [];
for (let i = 0; i < AppConfig.MULTISAMPLE_COUNT; ++i) {
const samplePosition = Vector3.add(voxelPosition, Vector3.random().add(-0.5));
@ -91,7 +92,7 @@ export class RayVoxeliser extends IVoxeliser {
triangle.uv0.v * w0 + triangle.uv1.v * w1 + triangle.uv2.v * w2,
);
return this._mesh!.sampleMaterial(materialName, uv, this._voxelMeshParams!.textureFiltering);
return this._mesh!.sampleMaterial(materialName, uv, this._voxeliseParams!.textureFiltering);
}
private _generateRays(v0: Vector3, v1: Vector3, v2: Vector3): Array<Ray> {

View File

@ -3,9 +3,18 @@ import { BVHRayVoxeliser } from './bvh-ray-voxeliser';
import { NormalCorrectedRayVoxeliser } from './normal-corrected-ray-voxeliser';
import { RayVoxeliser } from './ray-voxeliser';
import { ASSERT } from '../util';
import { TextureFiltering } from '../texture';
export type TVoxelisers = 'bvh-ray' | 'ncrb' | 'ray-based';
/** These are the parameters required by voxelisers */
export type VoxeliseParams = {
desiredHeight: number,
useMultisampleColouring: boolean,
textureFiltering: TextureFiltering,
enableAmbientOcclusion: boolean,
}
export class VoxeliserFactory {
public static GetVoxeliser(voxeliser: TVoxelisers): IVoxeliser {
switch (voxeliser) {

View File

@ -4,7 +4,7 @@ export const headlessConfig = {
},
voxelise: {
voxeliser: 'raybased', // 'raybased' / 'ncrb'
voxelMeshParams: {
voxeliseParams: {
desiredHeight: 80, // 5-320 inclusive
useMultisampleColouring: false,
textureFiltering: 'linear', // 'linear' / 'nearest'

View File

@ -1,7 +1,7 @@
import { Mesh } from '../src/mesh';
import { ObjImporter } from '../src/importers/obj_importer';
import { IVoxeliser } from '../src/voxelisers/base-voxeliser';
import { VoxelMesh, VoxelMeshParams } from '../src/voxel_mesh';
import { VoxelMesh } from '../src/voxel_mesh';
import { BlockMesh, BlockMeshParams, FallableBehaviour } from '../src/block_mesh';
import { IExporter} from '../src/exporters/base_exporter';
import { Schematic } from '../src/exporters/schematic_exporter';
@ -12,6 +12,7 @@ import { TextureFiltering } from '../src/texture';
import { ColourSpace } from '../src/util';
import { log, LogStyle } from './logging';
import { headlessConfig } from './headless-config';
import { VoxeliseParams } from '../src/voxelisers/voxelisers';
void async function main() {
const mesh = _import({
@ -19,10 +20,10 @@ void async function main() {
});
const voxelMesh = _voxelise(mesh, {
voxeliser: headlessConfig.voxelise.voxeliser === 'raybased' ? new RayVoxeliser() : new NormalCorrectedRayVoxeliser(),
voxelMeshParams: {
desiredHeight: headlessConfig.voxelise.voxelMeshParams.desiredHeight,
useMultisampleColouring: headlessConfig.voxelise.voxelMeshParams.useMultisampleColouring,
textureFiltering: headlessConfig.voxelise.voxelMeshParams.textureFiltering === 'linear' ? TextureFiltering.Linear : TextureFiltering.Nearest,
voxeliseParams: {
desiredHeight: headlessConfig.voxelise.voxeliseParams.desiredHeight,
useMultisampleColouring: headlessConfig.voxelise.voxeliseParams.useMultisampleColouring,
textureFiltering: headlessConfig.voxelise.voxeliseParams.textureFiltering === 'linear' ? TextureFiltering.Linear : TextureFiltering.Nearest,
enableAmbientOcclusion: false,
},
});
@ -46,9 +47,9 @@ interface ImportParams {
absoluteFilePathLoad: string;
}
interface VoxeliseParams {
interface ActionVoxeliseParams {
voxeliser: IVoxeliser;
voxelMeshParams: VoxelMeshParams;
voxeliseParams: VoxeliseParams;
}
interface PaletteParams {
@ -71,10 +72,10 @@ function _import(params: ImportParams): Mesh {
}
// TODO: Log status messages
function _voxelise(mesh: Mesh, params: VoxeliseParams): VoxelMesh {
function _voxelise(mesh: Mesh, params: ActionVoxeliseParams): VoxelMesh {
log(LogStyle.Info, 'Voxelising...');
const voxeliser: IVoxeliser = params.voxeliser;
return voxeliser.voxelise(mesh, params.voxelMeshParams);
return voxeliser.voxelise(mesh, params.voxeliseParams);
}
// TODO: Log status messages