Add blockstate and model previews

This commit is contained in:
Misode
2023-01-26 16:41:09 +01:00
parent d9c8eb17d7
commit 0a72ddd632
7 changed files with 193 additions and 13 deletions

View File

@@ -167,12 +167,13 @@ export async function fetchResources(versionId: VersionId) {
const version = config.versions.find(v => v.id === versionId)!
await validateCache(version)
try {
const [models, uvMapping, atlas] = await Promise.all([
const [blockDefinitions, models, uvMapping, atlas] = await Promise.all([
fetchAllPresets(versionId, 'block_definition'),
fetchAllPresets(versionId, 'model'),
fetch(`${mcmeta(version, 'atlas')}/all/data.min.json`).then(r => r.json()),
loadImage(`${mcmeta(version, 'atlas')}/all/atlas.png`),
])
return { models, uvMapping, atlas }
return { blockDefinitions, models, uvMapping, atlas }
} catch (e) {
throw new Error(`Error occured while fetching resources: ${message(e)}`)
}

View File

@@ -1,5 +1,5 @@
import type { BlockModelProvider, ItemStack, TextureAtlasProvider, UV } from 'deepslate/render'
import { BlockModel, Identifier, ItemRenderer, TextureAtlas, upperPowerOfTwo } from 'deepslate/render'
import type { BlockDefinitionProvider, BlockFlagsProvider, BlockModelProvider, BlockPropertiesProvider, ItemStack, TextureAtlasProvider, UV } from 'deepslate/render'
import { BlockDefinition, BlockModel, Identifier, ItemRenderer, TextureAtlas, upperPowerOfTwo } from 'deepslate/render'
import { message } from '../Utils.js'
import { fetchLanguage, fetchResources } from './DataFetcher.js'
import type { VersionId } from './Schemas.js'
@@ -10,8 +10,8 @@ export async function getResources(version: VersionId) {
if (!Resources[version]) {
Resources[version] = (async () => {
try {
const { models, uvMapping, atlas} = await fetchResources(version)
Resources[version] = new ResourceManager(models, uvMapping, atlas)
const { blockDefinitions, models, uvMapping, atlas} = await fetchResources(version)
Resources[version] = new ResourceManager(blockDefinitions, models, uvMapping, atlas)
return Resources[version]
} catch (e) {
console.error('Error: ', e)
@@ -51,17 +51,26 @@ export async function renderItem(version: VersionId, item: ItemStack) {
return promise
}
export class ResourceManager implements BlockModelProvider, TextureAtlasProvider {
private blockModels: { [id: string]: BlockModel }
interface Resources extends BlockDefinitionProvider, BlockModelProvider, TextureAtlasProvider, BlockFlagsProvider, BlockPropertiesProvider {}
export class ResourceManager implements Resources {
private readonly blockDefinitions: { [id: string]: BlockDefinition }
private readonly blockModels: { [id: string]: BlockModel }
private textureAtlas: TextureAtlas
constructor(models: Map<string, unknown>, uvMapping: any, textureAtlas: HTMLImageElement) {
constructor(blockDefinitions: Map<string, unknown>, models: Map<string, unknown>, uvMapping: any, textureAtlas: HTMLImageElement) {
this.blockDefinitions = {}
this.blockModels = {}
this.textureAtlas = TextureAtlas.empty()
this.loadBlockDefinitions(blockDefinitions)
this.loadBlockModels(models)
this.loadBlockAtlas(textureAtlas, uvMapping)
}
public getBlockDefinition(id: Identifier) {
return this.blockDefinitions[id.toString()]
}
public getBlockModel(id: Identifier) {
return this.blockModels[id.toString()]
}
@@ -74,6 +83,18 @@ export class ResourceManager implements BlockModelProvider, TextureAtlasProvider
return this.textureAtlas.getTextureAtlas()
}
public getBlockFlags() {
return { opaque: false }
}
public getBlockProperties() {
return null
}
public getDefaultBlockProperties() {
return null
}
private loadBlockModels(models: Map<string, unknown>) {
[...models.entries()].forEach(([id, model]) => {
this.blockModels[Identifier.create(id).toString()] = BlockModel.fromJson(id, model)
@@ -81,6 +102,12 @@ export class ResourceManager implements BlockModelProvider, TextureAtlasProvider
Object.values(this.blockModels).forEach(m => m.flatten(this))
}
private loadBlockDefinitions(definitions: Map<string, unknown>) {
[...definitions.entries()].forEach(([id, definition]) => {
this.blockDefinitions[Identifier.create(id).toString()] = BlockDefinition.fromJson(id, definition)
})
}
private loadBlockAtlas(image: HTMLImageElement, textures: any) {
const atlasCanvas = document.createElement('canvas')
const w = upperPowerOfTwo(image.width)
@@ -101,6 +128,41 @@ export class ResourceManager implements BlockModelProvider, TextureAtlasProvider
}
}
export class ResourceWrapper implements Resources {
constructor(
private readonly wrapped: Resources,
private readonly overrides: Partial<Resources>,
) {}
public getBlockDefinition(id: Identifier) {
return this.overrides.getBlockDefinition?.(id) ?? this.wrapped.getBlockDefinition(id)
}
public getBlockModel(id: Identifier) {
return this.overrides.getBlockModel?.(id) ?? this.wrapped.getBlockModel(id)
}
public getTextureUV(texture: Identifier) {
return this.overrides.getTextureUV?.(texture) ?? this.wrapped.getTextureUV(texture)
}
public getTextureAtlas() {
return this.overrides.getTextureAtlas?.() ?? this.wrapped.getTextureAtlas()
}
public getBlockFlags(id: Identifier) {
return this.overrides.getBlockFlags?.(id) ?? this.wrapped.getBlockFlags(id)
}
public getBlockProperties(id: Identifier) {
return this.overrides.getBlockProperties?.(id) ?? this.wrapped.getBlockProperties(id)
}
public getDefaultBlockProperties(id: Identifier) {
return this.overrides.getDefaultBlockProperties?.(id) ?? this.wrapped.getDefaultBlockProperties(id)
}
}
const Languages: Record<string, Record<string, string> | Promise<Record<string, string>>> = {}
export async function getLanguage(version: VersionId) {