* Added indexed JSON exporter

* Fixed vector UI element not setting value correctly
This commit is contained in:
Lucas Dower 2023-04-09 17:08:21 +01:00
parent d7614fc597
commit 9e3dac7e34
No known key found for this signature in database
GPG Key ID: B3EE6B8499593605
5 changed files with 77 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import { IExporter } from './base_exporter';
import { IndexedJSONExporter } from './indexed_json_exporter ';
import { Litematic } from './litematic_exporter';
import { NBTExporter } from './nbt_exporter';
import { SchemExporter } from './schem_exporter';
@ -10,7 +11,8 @@ export type TExporters =
'litematic' |
'schem' |
'nbt' |
'uncompressed_json';
'uncompressed_json' |
'indexed_json';
export class ExporterFactory {
public static GetExporter(voxeliser: TExporters): IExporter {
@ -25,6 +27,8 @@ export class ExporterFactory {
return new NBTExporter();
case 'uncompressed_json':
return new UncompressedJSONExporter();
case 'indexed_json':
return new IndexedJSONExporter();
}
}
}

View File

@ -0,0 +1,39 @@
import { BlockMesh } from '../block_mesh';
import { IExporter } from './base_exporter';
export class IndexedJSONExporter extends IExporter {
public override getFormatFilter() {
return {
name: 'Indexed JSON',
extension: 'json',
};
}
public override export(blockMesh: BlockMesh): Buffer {
const blocks = blockMesh.getBlocks();
const blocksUsed = blockMesh.getBlockPalette();
const blockToIndex = new Map<string, number>();
const indexToBlock = new Map<number, string>();
for (let i = 0; i < blocksUsed.length; ++i) {
blockToIndex.set(blocksUsed[i], i);
indexToBlock.set(i, blocksUsed[i]);
}
const blockArray = new Array<Array<number>>();
// Serialise all block except for the last one.
for (let i = 0; i < blocks.length; ++i) {
const block = blocks[i];
const pos = block.voxel.position;
blockArray.push([pos.x, pos.y, pos.z, blockToIndex.get(block.blockInfo.name)!]);
}
const json = JSON.stringify({
blocks: Object.fromEntries(indexToBlock),
xyzi: blockArray,
});
return Buffer.from(json);
}
}

View File

@ -85,12 +85,31 @@ export class GltfLoader extends IImporter {
canBeTextured: true,
});
materialNameToUse = materialName;
materialMade = true;
} else {
const diffuseColour: (number[] | undefined) = pbr.baseColorFactor;
if (diffuseColour !== undefined) {
meshMaterials.set(materialName, {
type: MaterialType.solid,
colour: {
r: diffuseColour[0],
g: diffuseColour[1],
b: diffuseColour[2],
a: diffuseColour[3],
},
needsAttention: false,
canBeTextured: false,
});
}
materialNameToUse = materialName;
materialMade = true;
}
}
const emissiveColour: (number[] | undefined) = primitive.material.emissiveFactor;
const emissiveColour: (number[] | undefined) = primitive.material.pbr;
if (!materialMade && emissiveColour !== undefined) {
meshMaterials.set(materialName, {
type: MaterialType.solid,

View File

@ -76,6 +76,15 @@ export class VectorComponent extends ConfigComponent<Vector3, HTMLDivElement> {
this._registerAxis('y');
}
this._registerAxis('z');
const elementX = UIUtil.getElementById(this._getValueId('x')) as HTMLInputElement;
const elementY = UIUtil.getElementById(this._getValueId('y')) as HTMLInputElement;
const elementZ = UIUtil.getElementById(this._getValueId('z')) as HTMLInputElement;
elementX.addEventListener('change', () => {
this.getValue().x = parseInt(elementX.value);
this.getValue().y = parseInt(elementY.value);
this.getValue().z = parseInt(elementZ.value);
});
}
private _updateValue(e: MouseEvent) {

View File

@ -293,6 +293,10 @@ export class UI {
displayText: 'Structure blocks (.nbt)',
payload: 'nbt',
},
{
displayText: 'Indexed JSON (.json)',
payload: 'indexed_json',
},
{
displayText: 'Uncompressed JSON (.json)',
payload: 'uncompressed_json',
@ -565,7 +569,6 @@ export class UI {
private _forEachComponent(action: EAction, functor: (component: ConfigComponent<unknown, unknown>) => void) {
const group = this._getGroup(action);
console.log(group);
for (const elementName of group.componentOrder) {
const element = group.components[elementName];