forked from mirror/ObjToSchematic
Fixed .schem exporter, closes #65
This commit is contained in:
parent
27e09c36fb
commit
f08c027936
@ -2,13 +2,13 @@ import { NBT, TagType } from 'prismarine-nbt';
|
|||||||
|
|
||||||
import { BlockMesh } from '../block_mesh';
|
import { BlockMesh } from '../block_mesh';
|
||||||
import { AppConstants } from '../constants';
|
import { AppConstants } from '../constants';
|
||||||
|
import { AppUtil } from '../util';
|
||||||
import { LOG } from '../util/log_util';
|
import { LOG } from '../util/log_util';
|
||||||
|
import { MathUtil } from '../util/math_util';
|
||||||
import { saveNBT } from '../util/nbt_util';
|
import { saveNBT } from '../util/nbt_util';
|
||||||
import { Vector3 } from '../vector';
|
import { Vector3 } from '../vector';
|
||||||
import { IExporter } from './base_exporter';
|
import { IExporter } from './base_exporter';
|
||||||
|
|
||||||
const varintarray = require('varint-array');
|
|
||||||
|
|
||||||
export class SchemExporter extends IExporter {
|
export class SchemExporter extends IExporter {
|
||||||
public override getFormatFilter() {
|
public override getFormatFilter() {
|
||||||
return {
|
return {
|
||||||
@ -33,27 +33,41 @@ export class SchemExporter extends IExporter {
|
|||||||
|
|
||||||
// https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-3.md#paletteObject
|
// https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-3.md#paletteObject
|
||||||
// const blockMapping: BlockMapping = {};
|
// const blockMapping: BlockMapping = {};
|
||||||
const test: {[name: string]: { type: TagType, value: any }} = {
|
const blockMapping: {[name: string]: { type: TagType, value: any }} = {
|
||||||
'minecraft:air': { type: TagType.Int, value: 0 },
|
'minecraft:air': { type: TagType.Int, value: 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
let blockIndex = 1;
|
let blockIndex = 1;
|
||||||
for (const blockName of blockMesh.getBlockPalette()) {
|
for (const blockName of blockMesh.getBlockPalette()) {
|
||||||
const namespacedBlockName = 'minecraft:' + blockName; // FIXME: All block names should be namespaced on import
|
const namespacedBlockName = AppUtil.Text.namespaceBlock(blockName);
|
||||||
// ASSERT(!(namespacedBlockName in blockMapping));
|
|
||||||
// blockMapping[namespacedBlockName] = blockIndex;
|
blockMapping[namespacedBlockName] = { type: TagType.Int, value: blockIndex };
|
||||||
test[namespacedBlockName] = { type: TagType.Int, value: blockIndex };
|
|
||||||
++blockIndex;
|
++blockIndex;
|
||||||
}
|
}
|
||||||
LOG(test);
|
LOG(blockMapping);
|
||||||
|
|
||||||
// const paletteObject = SchemExporter._createBlockStatePalette(blockMapping);
|
// const paletteObject = SchemExporter._createBlockStatePalette(blockMapping);
|
||||||
const blockData = new Array<number>(sizeVector.x * sizeVector.y * sizeVector.z).fill(0);
|
const blockData = new Array<number>(sizeVector.x * sizeVector.y * sizeVector.z).fill(0);
|
||||||
for (const block of blockMesh.getBlocks()) {
|
for (const block of blockMesh.getBlocks()) {
|
||||||
const indexVector = Vector3.sub(block.voxel.position, bounds.min);
|
const indexVector = Vector3.sub(block.voxel.position, bounds.min);
|
||||||
const bufferIndex = SchemExporter._getBufferIndex(sizeVector, indexVector);
|
const bufferIndex = SchemExporter._getBufferIndex(sizeVector, indexVector);
|
||||||
const namespacedBlockName = 'minecraft:' + block.blockInfo.name;
|
const namespacedBlockName = AppUtil.Text.namespaceBlock(block.blockInfo.name);
|
||||||
blockData[bufferIndex] = test[namespacedBlockName].value;
|
blockData[bufferIndex] = blockMapping[namespacedBlockName].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const blockEncoding: number[] = [];
|
||||||
|
for (let i = 0; i < blockData.length; ++i) {
|
||||||
|
let id = blockData[i];
|
||||||
|
|
||||||
|
while ((id & -128) != 0) {
|
||||||
|
blockEncoding.push(id & 127 | 128);
|
||||||
|
id >>>= 7;
|
||||||
|
}
|
||||||
|
blockEncoding.push(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < blockEncoding.length; ++i) {
|
||||||
|
blockEncoding[i] = MathUtil.int8(blockEncoding[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const nbt: NBT = {
|
const nbt: NBT = {
|
||||||
@ -66,8 +80,8 @@ export class SchemExporter extends IExporter {
|
|||||||
Height: { type: TagType.Short, value: sizeVector.y },
|
Height: { type: TagType.Short, value: sizeVector.y },
|
||||||
Length: { type: TagType.Short, value: sizeVector.z },
|
Length: { type: TagType.Short, value: sizeVector.z },
|
||||||
PaletteMax: { type: TagType.Int, value: blockIndex },
|
PaletteMax: { type: TagType.Int, value: blockIndex },
|
||||||
Palette: { type: TagType.Compound, value: test },
|
Palette: { type: TagType.Compound, value: blockMapping },
|
||||||
BlockData: { type: TagType.ByteArray, value: varintarray.encode(blockData) },
|
BlockData: { type: TagType.ByteArray, value: blockEncoding },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
18
src/util/math_util.ts
Normal file
18
src/util/math_util.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export namespace MathUtil {
|
||||||
|
|
||||||
|
export function uint8(x: number) {
|
||||||
|
return x & 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function int8(x: number) {
|
||||||
|
return uint8(x + 0x80) - 0x80;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function uint32(x: number) {
|
||||||
|
return x >>> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function int32(x: number) {
|
||||||
|
return uint32(x + 0x80000000) - 0x80000000;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user