From 1b612d66eec987e973b5549070a585a6afdd159d Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Thu, 23 Nov 2023 14:36:35 +0000 Subject: [PATCH] Move `benchmark.ts` from `Core` to `Sandbox` --- Core/scripts/benchmark.ts | 64 ---------------------------- Core/src/ots_block_mesh_converter.ts | 12 ++++-- Sandbox/package.json | 3 +- Sandbox/scripts/benchmark.ts | 57 +++++++++++++++++++++++++ Sandbox/tsconfig.json | 2 +- run_benchmark_core.sh | 3 ++ 6 files changed, 71 insertions(+), 70 deletions(-) delete mode 100644 Core/scripts/benchmark.ts create mode 100644 Sandbox/scripts/benchmark.ts create mode 100755 run_benchmark_core.sh diff --git a/Core/scripts/benchmark.ts b/Core/scripts/benchmark.ts deleted file mode 100644 index 03a3798..0000000 --- a/Core/scripts/benchmark.ts +++ /dev/null @@ -1,64 +0,0 @@ -import fs from 'fs'; -import path from 'path'; - -import { ObjImporter } from "../src/importers/obj_importer"; -import { OtS_VoxelMesh_Converter } from '../src/ots_voxel_mesh_converter'; -import { BlockMesh } from '../src/block_mesh'; -import { PALETTE_ALL_RELEASE } from '../../Editor/res/palettes/all'; -import { createReadableStream, createOtSTexture } from './util'; -import { TexturedMaterial } from 'Core/src/materials'; -import { ASSERT } from 'Core/src/util/error_util'; - -(async () => { - const pathModel = path.join(__dirname, '../res/samples/skull.obj'); - const readableStream = createReadableStream(pathModel); - - console.time('Mesh'); - const loader = new ObjImporter(); - const mesh = await loader.import(readableStream); - console.timeEnd('Mesh'); - - const pathTexture = path.join(__dirname, '../res/samples/skull.jpg'); - const texture = createOtSTexture(pathTexture); - ASSERT(texture !== undefined, `Could not parse ${pathTexture}`); - - // Update the 'skull' material - const success = mesh.setMaterial({ - type: 'textured', - name: 'skull', - texture: texture, - }); - ASSERT(success, 'Could not update skull material'); - - console.time('VoxelMesh'); - const converter = new OtS_VoxelMesh_Converter(); - converter.setConfig({ - constraintAxis: 'y', - size: 380, - multisampling: false, - replaceMode: 'keep', - }); - - const voxelMesh = converter.process(mesh); - console.timeEnd('VoxelMesh'); - - - console.time('BlockMesh'); - const blockMesh = BlockMesh.createFromVoxelMesh(voxelMesh, { - atlasJSON: JSON.parse(fs.readFileSync(path.join(__dirname, '../res/atlases/vanilla.atlas'), 'utf8')), - blockPalette: new Set(PALETTE_ALL_RELEASE), - calculateLighting: false, - contextualAveraging: true, - dithering: 'off', - ditheringMagnitude: 0, - errorWeight: 0.02, - resolution: 16, - fallable: 'do-nothing', - lightThreshold: 0, - }); - console.timeEnd('BlockMesh'); - - //console.log(mesh.getTriangleCount().toLocaleString(), 'triangles'); - //console.log(mesh.getMaterials()); - //console.log(voxelMesh.getVoxelCount().toLocaleString(), 'voxels'); -})(); \ No newline at end of file diff --git a/Core/src/ots_block_mesh_converter.ts b/Core/src/ots_block_mesh_converter.ts index ee0eb03..c4fe4ca 100644 --- a/Core/src/ots_block_mesh_converter.ts +++ b/Core/src/ots_block_mesh_converter.ts @@ -35,10 +35,10 @@ export type OtS_FallableBehaviour = 'replace-falling' | 'replace-fallable' | 'pl export type OtS_BlockMesh_ConverterConfig = { mode: OtS_BlockMesh_DataMode, - dithering?: { mode: 'random' | 'ordered', magnitude: number }, - fallable?: OtS_FallableBehaviour, - smoothness?: OtS_BlockMesh_DataMode & { weight: number }, - resolution?: number, // [1, 255] + dithering: { mode: 'random' | 'ordered', magnitude: number } | null, + fallable: OtS_FallableBehaviour | null, + smoothness: (OtS_BlockMesh_DataMode & { weight: number }) | null, + resolution: number, // [1, 255] } export class OtS_BlockMesh_Converter { @@ -47,6 +47,10 @@ export class OtS_BlockMesh_Converter { public constructor() { this._config = { mode: { type: 'per-block', data: BLOCK_DATA_DEFAULT.PER_BLOCK }, + resolution: 255, + smoothness: null, + dithering: null, + fallable: 'replace-falling', }; } diff --git a/Sandbox/package.json b/Sandbox/package.json index 84f4bb0..017ef55 100644 --- a/Sandbox/package.json +++ b/Sandbox/package.json @@ -6,7 +6,8 @@ "main": "index.js", "scripts": { "build": "tsc", - "test": "echo \"No tests\" && exit 0" + "test": "echo \"No tests\" && exit 0", + "benchmark": "ts-node ./scripts/benchmark.ts" }, "keywords": [], "author": "", diff --git a/Sandbox/scripts/benchmark.ts b/Sandbox/scripts/benchmark.ts new file mode 100644 index 0000000..3b68de0 --- /dev/null +++ b/Sandbox/scripts/benchmark.ts @@ -0,0 +1,57 @@ +import path from 'node:path'; + +import { OtS_Importer_Obj } from 'ots-core/src/importers/obj_importer'; +import { createOtSTexture, createReadableStream } from '../src/util'; +import { ASSERT } from 'ots-core/src/util/util'; +import { OtS_VoxelMesh_Converter } from 'ots-core/src/ots_voxel_mesh_converter'; +import { OtS_BlockMesh_Converter } from 'ots-core/src/ots_block_mesh_converter'; +import { BLOCK_DATA_DEFAULT } from 'ots-core/src/ots_block_data_default'; + +(async () => { + const pathModel = path.join(__dirname, '../../Editor/res/samples/skull.obj'); + const readableStream = createReadableStream(pathModel); + + console.time('Mesh'); + const loader = new OtS_Importer_Obj(); + const mesh = await loader.import(readableStream); + console.timeEnd('Mesh'); + + const pathTexture = path.join(__dirname, '../res/samples/skull.jpg'); + const texture = createOtSTexture(pathTexture); + ASSERT(texture !== undefined, `Could not parse ${pathTexture}`); + + // Update the 'skull' material + /* + const success = mesh.mo({ + type: 'textured', + name: 'skull', + texture: texture, + }); + ASSERT(success, 'Could not update skull material'); + */ + + console.time('VoxelMesh'); + const voxelMeshConverter = new OtS_VoxelMesh_Converter(); + voxelMeshConverter.setConfig({ + constraintAxis: 'y', + size: 380, + replaceMode: 'keep', + }); + + const voxelMesh = voxelMeshConverter.process(mesh); + console.timeEnd('VoxelMesh'); + + + console.time('BlockMesh'); + const blockMeshConverter = new OtS_BlockMesh_Converter(); + blockMeshConverter.setConfig({ + mode: { type: 'per-block', data: BLOCK_DATA_DEFAULT.PER_BLOCK }, + dithering: null, + smoothness: null, + fallable: 'replace-falling', + resolution: 128, + }); + + const blockMesh = blockMeshConverter.process(voxelMesh); + console.timeEnd('BlockMesh'); +})(); \ No newline at end of file diff --git a/Sandbox/tsconfig.json b/Sandbox/tsconfig.json index 12b2ca8..ff7362f 100644 --- a/Sandbox/tsconfig.json +++ b/Sandbox/tsconfig.json @@ -12,5 +12,5 @@ "outDir": "lib" }, - "include": ["src/**/*", "index.ts"] + "include": ["src/**/*", "scripts/**/*", "index.ts"] } \ No newline at end of file diff --git a/run_benchmark_core.sh b/run_benchmark_core.sh new file mode 100755 index 0000000..fd9c486 --- /dev/null +++ b/run_benchmark_core.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd Sandbox +npm run benchmark \ No newline at end of file