From 2ed8b82ca57f67e3da30fdcd57343f812204cd38 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Thu, 5 Dec 2024 17:43:12 +0300 Subject: [PATCH] add unknown blocks --- prismarine-viewer/examples/chunksStorage.ts | 33 +++++++ .../examples/webgpuRendererMain.ts | 93 ++++++++++--------- prismarine-viewer/viewer/lib/mesher/models.ts | 2 +- 3 files changed, 83 insertions(+), 45 deletions(-) diff --git a/prismarine-viewer/examples/chunksStorage.ts b/prismarine-viewer/examples/chunksStorage.ts index 9941a67ba..e35f4611c 100644 --- a/prismarine-viewer/examples/chunksStorage.ts +++ b/prismarine-viewer/examples/chunksStorage.ts @@ -35,6 +35,39 @@ export class ChunksStorage { } } + printSectionData ({ x, y, z }) { + x = Math.floor(x / 16) * 16 + y = Math.floor(y / 16) * 16 + z = Math.floor(z / 16) * 16 + const key = `${x},${y},${z}` + const chunkIndex = this.chunksMap.get(key) + if (chunkIndex === undefined) return + const chunk = this.chunks[chunkIndex] + let start = 0 + for (let i = 0; i < chunkIndex; i++) { + start += this.chunks[i].length + } + const end = start + chunk.length + return { + blocks: this.allBlocks.slice(start, end), + index: chunkIndex, + range: [start, end] + } + } + + printBlock ([x, y, z]: [number, number, number]) { + const section = this.printSectionData({ x, y, z }) + if (!section) return + const xRel = ((x % 16) + 16) % 16 + const zRel = ((z % 16) + 16) % 16 + for (const block of section.blocks) { + if (block && block[0] === xRel && block[1] === y && block[2] === zRel) { + return block + } + } + return null + } + getDataForBuffers () { this.lastFetchedSize = this.dataSize if (this.awaitingUpdateStart === undefined) return diff --git a/prismarine-viewer/examples/webgpuRendererMain.ts b/prismarine-viewer/examples/webgpuRendererMain.ts index 9b09f7d4a..1a033ad67 100644 --- a/prismarine-viewer/examples/webgpuRendererMain.ts +++ b/prismarine-viewer/examples/webgpuRendererMain.ts @@ -292,6 +292,54 @@ const getBlocksModelData = () => { const blocksProccessed = {} as Record let i = 0 const allBlocksStateIdToModelIdMap = {} as AllBlocksStateIdToModelIdMap + + const addBlockModel = (state: number, name: string, props: Record) => { + const models = provider.getAllResolvedModels0_1({ + name, + properties: props + }, isPreflat) + // skipping composite blocks + if (models.length !== 1 || !models[0]![0].elements) { + return + } + const elements = models[0]![0]?.elements + if (elements.length !== 1 && name !== 'grass_block') { + return + } + const elem = models[0]![0].elements[0] + if (elem.from[0] !== 0 || elem.from[1] !== 0 || elem.from[2] !== 0 || elem.to[0] !== 16 || elem.to[1] !== 16 || elem.to[2] !== 16) { + // not full block + return + } + const facesMapping = [ + ['front', 'south'], + ['bottom', 'down'], + ['top', 'up'], + ['right', 'east'], + ['left', 'west'], + ['back', 'north'], + ] + const blockData = { + textures: [0, 0, 0, 0, 0, 0], + rotation: [0, 0, 0, 0, 0, 0] + } + for (const [face, { texture, cullface, rotation = 0 }] of Object.entries(elem.faces)) { + const faceIndex = facesMapping.findIndex(x => x.includes(face)) + if (faceIndex === -1) { + throw new Error(`Unknown face ${face}`) + } + blockData.textures[faceIndex] = texture.tileIndex + blockData.rotation[faceIndex] = rotation / 90 + if (Math.floor(blockData.rotation[faceIndex]) !== blockData.rotation[faceIndex]) { + throw new Error(`Invalid rotation ${rotation} ${name}`) + } + } + const k = i++ + allBlocksStateIdToModelIdMap[state] = k + blocksDataModel[k] = blockData + blocksProccessed[name] = true + } + addBlockModel(-1, 'unknown', {}) for (const b of loadedData.blocksArray) { for (let state = b.minStateId; state <= b.maxStateId; state++) { const mapping = blocksMap[b.name] @@ -304,50 +352,7 @@ const getBlocksModelData = () => { })) { continue } - const models = provider.getAllResolvedModels0_1({ - name: block.name, - properties: block.getProperties() - }, isPreflat) - // skipping composite blocks - if (models.length !== 1 || !models[0]![0].elements) { - continue - } - const elements = models[0]![0]?.elements - if (elements.length !== 1 && block.name !== 'grass_block') { - continue - } - const elem = models[0]![0].elements[0] - if (elem.from[0] !== 0 || elem.from[1] !== 0 || elem.from[2] !== 0 || elem.to[0] !== 16 || elem.to[1] !== 16 || elem.to[2] !== 16) { - // not full block - continue - } - const facesMapping = [ - ['front', 'south'], - ['bottom', 'down'], - ['top', 'up'], - ['right', 'east'], - ['left', 'west'], - ['back', 'north'], - ] - const blockData = { - textures: [0, 0, 0, 0, 0, 0], - rotation: [0, 0, 0, 0, 0, 0] - } - for (const [face, { texture, cullface, rotation = 0 }] of Object.entries(elem.faces)) { - const faceIndex = facesMapping.findIndex(x => x.includes(face)) - if (faceIndex === -1) { - throw new Error(`Unknown face ${face}`) - } - blockData.textures[faceIndex] = texture.tileIndex - blockData.rotation[faceIndex] = rotation / 90 - if (Math.floor(blockData.rotation[faceIndex]) !== blockData.rotation[faceIndex]) { - throw new Error(`Invalid rotation ${rotation} ${b.name}`) - } - } - const k = i++ - allBlocksStateIdToModelIdMap[state] = k - blocksDataModel[k] = blockData - blocksProccessed[block.name] = true + addBlockModel(state, block.name, block.getProperties()) } } return { diff --git a/prismarine-viewer/viewer/lib/mesher/models.ts b/prismarine-viewer/viewer/lib/mesher/models.ts index 549457a9b..c4382da97 100644 --- a/prismarine-viewer/viewer/lib/mesher/models.ts +++ b/prismarine-viewer/viewer/lib/mesher/models.ts @@ -406,7 +406,7 @@ function renderElement (world: World, cursor: Vec3, element: BlockElement, doAO: if (needTiles) { const tiles = attr.tiles as Tiles - const model = world.webgpuModelsMapping[block.stateId!] ?? world.webgpuModelsMapping[0] + const model = world.webgpuModelsMapping[block.stateId!] ?? world.webgpuModelsMapping[-1] if (model !== undefined) { tiles[`${cursor.x},${cursor.y},${cursor.z}`] ??= { block: block.name,