Skip to content

Commit

Permalink
💥 Remove unused id field of BlockModel and BlockDefinition
Browse files Browse the repository at this point in the history
This also simplifies special renderers a bit by removing the dummy function
  • Loading branch information
misode committed Oct 2, 2024
1 parent 4a3a21d commit 0c373d8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 118 deletions.
4 changes: 2 additions & 2 deletions demo/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ Promise.all([

const blockDefinitions: Record<string, BlockDefinition> = {}
Object.keys(blockstates).forEach(id => {
blockDefinitions['minecraft:' + id] = BlockDefinition.fromJson(id, blockstates[id])
blockDefinitions['minecraft:' + id] = BlockDefinition.fromJson(blockstates[id])
})

const blockModels: Record<string, BlockModel> = {}
Object.keys(models).forEach(id => {
blockModels['minecraft:' + id] = BlockModel.fromJson(id, models[id])
blockModels['minecraft:' + id] = BlockModel.fromJson(models[id])
})
Object.values(blockModels).forEach((m: any) => m.flatten({ getBlockModel: id => blockModels[id] }))

Expand Down
15 changes: 6 additions & 9 deletions src/render/BlockDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ export interface BlockDefinitionProvider {

export class BlockDefinition {
constructor(
private readonly id: Identifier,
private readonly variants: { [key: string]: ModelVariantEntry } | undefined,
private readonly multipart: ModelMultiPart[] | undefined,
) {
this.variants = variants
}
) {}

public getModelVariants(props: { [key: string]: string }): ModelVariant[] {
if (this.variants) {
Expand All @@ -54,7 +51,7 @@ export class BlockDefinition {
return []
}

public getMesh(name: Identifier, props: { [key: string]: string }, textureUVProvider: TextureAtlasProvider, blockModelProvider: BlockModelProvider, cull: Cull) {
public getMesh(name: Identifier | undefined, props: { [key: string]: string }, atlas: TextureAtlasProvider, blockModelProvider: BlockModelProvider, cull: Cull) {
const variants = this.getModelVariants(props)
const mesh = new Mesh()

Expand All @@ -64,8 +61,8 @@ export class BlockDefinition {
if (!blockModel) {
throw new Error(`Cannot find block model ${variant.model}`)
}
const tint = BlockColors[name.path]?.(props)
const variantMesh = blockModel.getMesh(textureUVProvider, newCull, tint)
const tint = name ? BlockColors[name.path]?.(props) : undefined
const variantMesh = blockModel.getMesh(atlas, newCull, tint)

if (variant.x || variant.y) {
const t = mat4.create()
Expand Down Expand Up @@ -101,7 +98,7 @@ export class BlockDefinition {
})
}

public static fromJson(id: string, data: any) {
return new BlockDefinition(Identifier.parse(id), data.variants, data.multipart)
public static fromJson(data: any) {
return new BlockDefinition(data.variants, data.multipart)
}
}
13 changes: 6 additions & 7 deletions src/render/BlockModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export class BlockModel {
private uvEpsilon = 1/16

constructor(
private readonly id: Identifier,
private parent: Identifier | undefined,
private textures: { [key: string]: string } | undefined,
private elements: BlockModelElement[] | undefined,
Expand All @@ -104,7 +103,7 @@ export class BlockModel {
return t
}

public getMesh(uvProvider: TextureAtlasProvider, cull: Cull, tint?: Color | ((index: number) => Color)) {
public getMesh(atlas: TextureAtlasProvider, cull: Cull, tint?: Color | ((index: number) => Color)) {
const mesh = new Mesh()
const getTint = (index?: number): Color => {
if (tint === undefined) return [1, 1, 1]
Expand All @@ -113,12 +112,12 @@ export class BlockModel {
return tint
}
for (const e of this.elements ?? []) {
mesh.merge(this.getElementMesh(e, uvProvider, cull, getTint))
mesh.merge(this.getElementMesh(e, atlas, cull, getTint))
}
return mesh
}

public getElementMesh(e: BlockModelElement, uvProvider: TextureAtlasProvider, cull: Cull, getTint: (index?: number) => Color) {
public getElementMesh(e: BlockModelElement, atlas: TextureAtlasProvider, cull: Cull, getTint: (index?: number) => Color) {
const mesh = new Mesh()
const [x0, y0, z0] = e.from
const [x1, y1, z1] = e.to
Expand All @@ -133,7 +132,7 @@ export class BlockModel {
const tint = getTint(face.tintindex)
quad.setColor(tint)

const [u0, v0, u1, v1] = uvProvider.getTextureUV(this.getTexture(face.texture))
const [u0, v0, u1, v1] = atlas.getTextureUV(this.getTexture(face.texture))
const du = (u1 - u0) / 16
const dv = (v1 - v0) / 16
const duu = du * this.uvEpsilon
Expand Down Expand Up @@ -276,8 +275,8 @@ export class BlockModel {
return accessor.getBlockModel(this.parent)
}

public static fromJson(id: string, data: any) {
public static fromJson(data: any) {
const parent = data.parent === undefined ? undefined : Identifier.parse(data.parent)
return new BlockModel(Identifier.parse(id), parent, data.textures, data.elements, data.display)
return new BlockModel(parent, data.textures, data.elements, data.display)
}
}
Loading

0 comments on commit 0c373d8

Please sign in to comment.