Replace RGBAColours with OtS_Colours

This commit is contained in:
Lucas Dower 2023-10-22 17:39:19 +01:00
parent 0f543d809d
commit 70d4df2d2d
11 changed files with 82 additions and 53 deletions

View File

@ -21,7 +21,7 @@ export namespace RGBAUtil {
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = v - c;
let output = RGBAUtil.copy(RGBAColours.BLACK);
let output = OtS_Colours.BLACK;
if (h < 60) {
output.r = c;
output.g = x;
@ -220,15 +220,36 @@ export namespace RGBAUtil {
export type TColourAccuracy = number;
}
export namespace RGBAColours {
export const RED: RGBA = { r: 1.0, g: 0.0, b: 0.0, a: 1.0 };
export const GREEN: RGBA = { r: 0.0, g: 1.0, b: 0.0, a: 1.0 };
export const BLUE: RGBA = { r: 0.0, g: 0.0, b: 1.0, a: 1.0 };
export class OtS_Colours {
public static get BLACK() {
return { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };
}
export const YELLOW: RGBA = { r: 1.0, g: 1.0, b: 0.0, a: 1.0 };
export const CYAN: RGBA = { r: 0.0, g: 1.0, b: 1.0, a: 1.0 };
export const MAGENTA: RGBA = { r: 1.0, g: 0.0, b: 1.0, a: 1.0 };
public static get WHITE() {
return { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
}
public static get RED() {
return { r: 1.0, g: 0.0, b: 0.0, a: 1.0 };
}
export const WHITE: RGBA = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
export const BLACK: RGBA = { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };
public static get GREEN() {
return { r: 0.0, g: 1.0, b: 0.0, a: 1.0 };
}
public static get BLUE() {
return { r: 0.0, g: 0.0, b: 1.0, a: 1.0 };
}
public static get YELLOW() {
return { r: 1.0, g: 1.0, b: 0.0, a: 1.0 };
}
public static get CYAN() {
return { r: 0.0, g: 1.0, b: 1.0, a: 1.0 };
}
public static get MAGENTA() {
return { r: 1.0, g: 0.0, b: 1.0, a: 1.0 };
}
}

View File

@ -1,12 +1,12 @@
import { parse } from '@loaders.gl/core';
import { GLTFLoader } from '@loaders.gl/gltf';
import { RGBAColours, RGBAUtil } from '../colour';
import { UV } from '../util';
import { Vector3 } from '../vector';
import { OtS_Mesh } from '../ots_mesh';
import { OtS_Texture } from '../ots_texture';
import { OtS_Importer } from './base_importer';
import { OtS_Colours } from '../colour';
export type OtS_GltfImporterError =
| { type: 'failed-to-parse' }
@ -48,7 +48,7 @@ export class OtS_Importer_Gltf extends OtS_Importer {
const meshMaterials: Map<string, { }> = new Map();
meshMaterials.set('NONE', {
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
canBeTextured: false,
});
let maxIndex = 0;
@ -122,7 +122,7 @@ export class OtS_Importer_Gltf extends OtS_Importer {
meshMaterials.set(materialName, {
name: materialName,
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
canBeTextured: true,
});
}

View File

@ -1,4 +1,4 @@
import { RGBAColours, RGBAUtil } from '../colour';
import { OtS_Colours, RGBAUtil } from '../colour';
import { OtS_Mesh } from '../ots_mesh';
import { OtS_Texture } from '../ots_texture';
import { Triangle } from '../triangle';
@ -226,7 +226,7 @@ export class OtS_Importer_Obj extends OtS_Importer {
mesh.addSection({
name: material,
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
positionData: Float32Array.from(positionData),
normalData: Float32Array.from(normalData),
indexData: Uint32Array.from(indexData),

View File

@ -1,9 +1,9 @@
import { OtS_VoxelMesh } from './ots_voxel_mesh';
import { OtS_BlockMesh } from './ots_block_mesh';
import { RGBA, RGBAColours, RGBAUtil } from './colour';
import { OtS_FaceVisibility, OtS_VoxelMesh_Neighbourhood } from './ots_voxel_mesh_neighbourhood';
import { ASSERT } from './util/error_util';
import { Vector3 } from './vector';
import { RGBA, OtS_Colours, RGBAUtil } from './colour';
export type OtS_BlockData_PerBlock<T> = { name: string, colour: T }[];
@ -45,7 +45,7 @@ export class OtS_BlockMesh_Converter {
this._config = {
mode: {
type: 'per-block', data: [
{ name: 'minecraft:stone', colour: RGBAUtil.copy(RGBAColours.WHITE) }
{ name: 'minecraft:stone', colour: OtS_Colours.WHITE }
]
}
};

View File

@ -2,10 +2,8 @@ import { OtS_ReplaceMode, OtS_VoxelMesh } from './ots_voxel_mesh';
import { TAxis } from './util/type_util';
import { Vector3 } from './vector';
import { Triangle } from './triangle';
import { LinearAllocator } from './linear_allocator';
import { Axes, Ray, rayIntersectTriangle } from './ray';
import { Bounds } from './bounds';
import { RGBA, RGBAColours, RGBAUtil } from './colour';
import { OtS_Colours, RGBA, RGBAUtil } from './colour';
import { OtS_Mesh, OtS_Triangle } from './ots_mesh';
import { UV } from './util';
@ -155,7 +153,7 @@ export class OtS_VoxelMesh_Converter {
};
if (isNaN(texcoord.u) || isNaN(texcoord.v)) {
RGBAUtil.copy(RGBAColours.MAGENTA);
return OtS_Colours.MAGENTA;
}
return triangle.texture.sample(texcoord.u, texcoord.v);

View File

@ -1,21 +1,20 @@
import { RGBAColours } from '../src/colour';
import { OtS_VoxelMesh } from '../src/ots_voxel_mesh';
import { RGBAUtil } from '../src/colour';
import { OtS_Colours, RGBAUtil } from '../src/colour';
import { OtS_BlockMesh_Converter } from '../src/ots_block_mesh_converter';
test('Per-block', () => {
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAUtil.copy(RGBAColours.RED));
voxelMesh.addVoxel(1, 0, 0, RGBAUtil.copy(RGBAColours.GREEN));
voxelMesh.addVoxel(2, 0, 0, RGBAUtil.copy(RGBAColours.BLUE));
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.RED);
voxelMesh.addVoxel(1, 0, 0, OtS_Colours.GREEN);
voxelMesh.addVoxel(2, 0, 0, OtS_Colours.BLUE);
const converter = new OtS_BlockMesh_Converter();
converter.setConfig({
mode: {
type: 'per-block', data: [
{ name: 'RED-BLOCK', colour: RGBAUtil.copy(RGBAColours.RED) },
{ name: 'GREEN-BLOCK', colour: RGBAUtil.copy(RGBAColours.GREEN) },
{ name: 'BLUE-BLOCK', colour: RGBAUtil.copy(RGBAColours.BLUE) }
{ name: 'RED-BLOCK', colour: OtS_Colours.RED },
{ name: 'GREEN-BLOCK', colour: OtS_Colours.GREEN },
{ name: 'BLUE-BLOCK', colour: OtS_Colours.BLUE }
]
},
});

View File

@ -1,5 +1,5 @@
import { RGBAColours, RGBAUtil } from "../src/colour";
import { OtS_Mesh } from "../src/ots_mesh";
import { OtS_Colours } from '../src/colour';
test('Mesh Triangles', () => {
const mesh = OtS_Mesh.create();
@ -7,7 +7,7 @@ test('Mesh Triangles', () => {
mesh.addSection({
name: 'Test Section 1',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
positionData: Float32Array.from([
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,
@ -33,7 +33,7 @@ test('Mesh Triangles', () => {
mesh.addSection({
name: 'Test Section 2',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
positionData: Float32Array.from([
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,

View File

@ -1,10 +1,10 @@
import { RGBAColours } from '../src/colour';
import { OtS_Colours } from '../src/colour';
import { OtS_VoxelMesh } from '../src/ots_voxel_mesh';
import { OtS_FaceVisibility, OtS_VoxelMesh_Neighbourhood } from '../src/ots_voxel_mesh_neighbourhood';
test('VoxelMesh Neighbourhood #1', () => {
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'cardinal');
@ -20,8 +20,8 @@ test('VoxelMesh Neighbourhood #2', () => {
test('VoxelMesh Neighbourhood #3', () => {
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 1, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(0, 1, 0, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'cardinal');
@ -35,10 +35,10 @@ test('VoxelMesh Neighbourhood #3', () => {
test('VoxelMesh Neighbourhood #4', () => {
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(1, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 1, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 1, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(1, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(0, 1, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 1, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'cardinal');
@ -62,8 +62,8 @@ test('VoxelMesh Neighbourhood #5', () => {
test('VoxelMesh Neighbourhood #6', () => {
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(1, 1, 1, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(1, 1, 1, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'non-cardinal');
@ -75,8 +75,8 @@ test('VoxelMesh Neighbourhood #6', () => {
test('VoxelMesh Neighbourhood #6', () => {
// Checking a non-cardinal neighbour when processing using 'cardinal' mode
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(1, 1, 1, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(1, 1, 1, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'cardinal');
@ -93,8 +93,8 @@ test('VoxelMesh Neighbourhood #6', () => {
test('VoxelMesh Neighbourhood #6', () => {
// Checking a cardinal neighbour when processing using 'non-cardinal' mode
const voxelMesh = new OtS_VoxelMesh();
voxelMesh.addVoxel(0, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(1, 0, 0, RGBAColours.WHITE, 'replace');
voxelMesh.addVoxel(0, 0, 0, OtS_Colours.WHITE, 'replace');
voxelMesh.addVoxel(1, 0, 0, OtS_Colours.WHITE, 'replace');
const neighbourhood = new OtS_VoxelMesh_Neighbourhood();
neighbourhood.process(voxelMesh, 'non-cardinal');

View File

@ -3,8 +3,6 @@ import { MaterialTypeComponent } from "./material_type";
import { SolidMaterialComponent } from './solid_material';
import { TexturedMaterialComponent } from "./textured_material";
import { UIUtil } from "../../util/ui_util";
import { RGBAColours, RGBAUtil } from "../../../../Core/src/colour";
import { OtS_Texture } from "../../../../Core/src/ots_texture";
import { OtS_MeshSectionMetadata } from "ots-core/src/ots_mesh";
export class MaterialComponent extends ConfigComponent<Promise<OtS_MeshSectionMetadata>, HTMLDivElement> {

View File

@ -1,20 +1,21 @@
import fs from 'fs';
import { RGBAColours, RGBAUtil } from 'ots-core/src/colour';
import { OtS_Mesh } from 'ots-core/src/ots_mesh';
import { OtS_Texture } from 'ots-core/src/ots_texture';
import { OtS_VoxelMesh_Converter } from '../Core/src/ots_voxel_mesh_converter';
import { OtS_BlockMesh_Converter } from '../Core/src/ots_block_mesh_converter';
import { ExporterFactory } from '../Core/src/exporters/exporters';
import { ASSERT } from 'ots-core/src/util/error_util';
import { OtS_Colours } from 'ots-core/src/colour';
(async () => {
console.time('Mesh');
const mesh = OtS_Mesh.create();
{
mesh.addSection({
name: 'Test Section 1',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
colour: OtS_Colours.WHITE,
positionData: Float32Array.from([
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,
@ -30,6 +31,7 @@ import { ASSERT } from 'ots-core/src/util/error_util';
]),
});
/*
mesh.addSection({
name: 'Test Section 2',
type: 'colour',
@ -52,7 +54,9 @@ import { ASSERT } from 'ots-core/src/util/error_util';
0, 1, 2
]),
});
*/
/*
mesh.addSection({
name: 'Test Section 3',
type: 'textured',
@ -76,9 +80,12 @@ import { ASSERT } from 'ots-core/src/util/error_util';
0, 1, 2
]),
});
*/
}
console.timeEnd('Mesh');
// 3. Construct a voxel mesh from the mesh
console.time('Voxel Mesh');
const voxelMeshConverter = new OtS_VoxelMesh_Converter();
voxelMeshConverter.setConfig({
constraintAxis: 'y',
@ -88,23 +95,28 @@ import { ASSERT } from 'ots-core/src/util/error_util';
});
const voxelMesh = voxelMeshConverter.process(mesh);
console.timeEnd('Voxel Mesh');
// 4. Construct a block mesh from the block
console.time('Block Mesh');
const blockMeshConverter = new OtS_BlockMesh_Converter();
blockMeshConverter.setConfig({
mode: {
type: 'per-block', data: [
{ name: 'minecraft:stone', colour: RGBAUtil.copy(RGBAColours.WHITE) },
]
{ name: 'minecraft:stone', colour: OtS_Colours.YELLOW }
],
},
});
const blockMesh = blockMeshConverter.process(voxelMesh);
console.timeEnd('Block Mesh');
// 5. Export the block mesh to a file
console.time('Export');
const exporter = ExporterFactory.GetExporter('uncompressed_json');
const structure = exporter.export(blockMesh);
ASSERT(structure.type === 'single');
fs.writeFileSync('output.json', structure.content);
console.timeEnd('Export');
})();

1
Sandbox/output.json Normal file

File diff suppressed because one or more lines are too long