ObjToSchematic/tools/headless.ts

89 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-03-23 05:15:09 +08:00
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 { BlockMesh, BlockMeshParams } from '../src/block_mesh';
2022-04-15 06:35:23 +08:00
import { IExporter} from '../src/exporters/base_exporter';
import { Schematic } from '../src/exporters/schematic_exporter';
import { Litematic } from '../src/exporters/litematic_exporter';
2022-03-23 05:35:13 +08:00
import { RayVoxeliser } from '../src/voxelisers/ray-voxeliser';
2022-03-23 05:15:09 +08:00
import { NormalCorrectedRayVoxeliser } from '../src/voxelisers/normal-corrected-ray-voxeliser';
import { TextureFiltering } from '../src/texture';
import { ColourSpace } from '../src/util';
import { log, LogStyle } from './logging';
import { headlessConfig } from './headless-config';
void async function main() {
const mesh = _import({
absoluteFilePathLoad: headlessConfig.import.absoluteFilePathLoad,
});
const voxelMesh = _voxelise(mesh, {
2022-03-23 05:35:13 +08:00
voxeliser: headlessConfig.voxelise.voxeliser === 'raybased' ? new RayVoxeliser() : new NormalCorrectedRayVoxeliser(),
2022-03-23 05:15:09 +08:00
voxelMeshParams: {
desiredHeight: headlessConfig.voxelise.voxelMeshParams.desiredHeight,
useMultisampleColouring: headlessConfig.voxelise.voxelMeshParams.useMultisampleColouring,
textureFiltering: headlessConfig.voxelise.voxelMeshParams.textureFiltering === 'linear' ? TextureFiltering.Linear : TextureFiltering.Nearest,
},
});
const blockMesh = _palette(voxelMesh, {
blockMeshParams: {
textureAtlas: headlessConfig.palette.blockMeshParams.textureAtlas,
blockPalette: headlessConfig.palette.blockMeshParams.blockPalette,
ditheringEnabled: headlessConfig.palette.blockMeshParams.ditheringEnabled,
colourSpace: headlessConfig.palette.blockMeshParams.colourSpace === 'rgb' ? ColourSpace.RGB : ColourSpace.LAB,
},
});
_export(blockMesh, {
absoluteFilePathSave: headlessConfig.export.absoluteFilePathSave,
exporter: headlessConfig.export.exporter === 'schematic' ? new Schematic() : new Litematic(),
});
log(LogStyle.Success, 'Finished!');
}();
interface ImportParams {
absoluteFilePathLoad: string;
}
interface VoxeliseParams {
voxeliser: IVoxeliser;
voxelMeshParams: VoxelMeshParams;
}
interface PaletteParams {
blockMeshParams: BlockMeshParams;
}
interface ExportParams {
absoluteFilePathSave: string;
2022-04-01 02:10:37 +08:00
exporter: IExporter;
2022-03-23 05:15:09 +08:00
}
2022-04-14 03:10:53 +08:00
// TODO: Log status messages
2022-03-23 05:15:09 +08:00
function _import(params: ImportParams): Mesh {
log(LogStyle.Info, 'Importing...');
const importer = new ObjImporter();
importer.parseFile(params.absoluteFilePathLoad);
const mesh = importer.toMesh();
mesh.processMesh();
return mesh;
}
2022-04-14 03:10:53 +08:00
// TODO: Log status messages
2022-03-23 05:15:09 +08:00
function _voxelise(mesh: Mesh, params: VoxeliseParams): VoxelMesh {
log(LogStyle.Info, 'Voxelising...');
const voxeliser: IVoxeliser = params.voxeliser;
return voxeliser.voxelise(mesh, params.voxelMeshParams);
}
2022-04-14 03:10:53 +08:00
// TODO: Log status messages
2022-03-23 05:15:09 +08:00
function _palette(voxelMesh: VoxelMesh, params: PaletteParams): BlockMesh {
log(LogStyle.Info, 'Assigning blocks...');
return BlockMesh.createFromVoxelMesh(voxelMesh, params.blockMeshParams);
}
2022-04-14 03:10:53 +08:00
// TODO: Log status messages
2022-03-23 05:15:09 +08:00
function _export(blockMesh: BlockMesh, params: ExportParams) {
log(LogStyle.Info, 'Exporting...');
params.exporter.export(blockMesh, params.absoluteFilePathSave);
}