diff --git a/demo/main.ts b/demo/main.ts index 6c88f2c..c8122c0 100644 --- a/demo/main.ts +++ b/demo/main.ts @@ -104,12 +104,11 @@ Promise.all([ const atlasCtx = atlasCanvas.getContext('2d')! atlasCtx.drawImage(atlas, 0, 0) const atlasData = atlasCtx.getImageData(0, 0, atlasSize, atlasSize) - const part = 16 / atlasData.width const idMap = {} Object.keys(uvMap).forEach(id => { - const u = uvMap[id][0] / atlasSize - const v = uvMap[id][1] / atlasSize - idMap['minecraft:' + id] = [u, v, u + part, v + part] + const [u, v, du, dv] = uvMap[id] + const dv2 = (du !== dv && id.startsWith('block/')) ? du : dv + idMap[Identifier.create(id).toString()] = [u / atlasSize, v / atlasSize, (u + du) / atlasSize, (v + dv2) / atlasSize] }) const textureAtlas = new TextureAtlas(atlasData, idMap) diff --git a/src/render/BlockModel.ts b/src/render/BlockModel.ts index 0edf541..fc635f6 100644 --- a/src/render/BlockModel.ts +++ b/src/render/BlockModel.ts @@ -4,7 +4,7 @@ import type { Direction } from '../core/index.js' import { Identifier } from '../core/index.js' import { Vector } from '../math/index.js' import type { Color } from '../util/index.js' -import { Cull } from './Cull.js' +import type { Cull } from './Cull.js' import { Mesh } from './Mesh.js' import { Quad } from './Quad.js' import type { TextureAtlasProvider, UV } from './TextureAtlas.js' @@ -84,9 +84,7 @@ export class BlockModel { private guiLight?: BlockModelGuiLight | undefined, ) {} - public getDisplayMesh(display: Display, uvProvider: TextureAtlasProvider, tint?: Color | ((index: number) => Color)) { - const mesh = this.getMesh(uvProvider, Cull.none(), tint) - + public getDisplayTransform(display: Display) { const transform = this.display?.[display] const t = mat4.create() mat4.identity(t) @@ -103,9 +101,7 @@ export class BlockModel { mat4.scale(t, t, transform.scale) } mat4.translate(t, t, [-8, -8, -8]) - mesh.transform(t) - - return mesh + return t } public getMesh(uvProvider: TextureAtlasProvider, cull: Cull, tint?: Color | ((index: number) => Color)) { diff --git a/src/render/ItemRenderer.ts b/src/render/ItemRenderer.ts index 7fc17b2..79cc27e 100644 --- a/src/render/ItemRenderer.ts +++ b/src/render/ItemRenderer.ts @@ -1,7 +1,7 @@ import { mat4 } from 'gl-matrix' import { Identifier } from '../core/index.js' import { ItemStack } from '../core/ItemStack.js' -import type { Color } from '../index.js' +import { Cull, SpecialRenderer, SpecialRenderers, type Color } from '../index.js' import type { BlockModelProvider } from './BlockModel.js' import { getItemColor } from './ItemColors.js' import type { Mesh } from './Mesh.js' @@ -48,7 +48,17 @@ export class ItemRenderer extends Renderer { if (!tint && this.item.id.namespace === Identifier.DEFAULT_NAMESPACE) { tint = getItemColor(this.item) } - const mesh = model.getDisplayMesh('gui', this.resources, tint) + const mesh = model.getMesh(this.resources, Cull.none(), tint) + if (SpecialRenderers.has(this.item.id.toString())){ + const specialMesh = SpecialRenderer[this.item.id.toString()]({}, this.resources, Cull.none()) + // undo the scaling done by the special renderer + const t = mat4.create() + mat4.identity(t) + mat4.scale(t, t, [16, 16, 16]) + specialMesh.transform(t) + mesh.merge(specialMesh) + } + mesh.transform(model.getDisplayTransform('gui')) mesh.quads.forEach(q => { const normal = q.normal() q.forEach(v => v.normal = normal) diff --git a/src/render/SpecialRenderer.ts b/src/render/SpecialRenderer.ts index 1b35ff1..8ff93f6 100644 --- a/src/render/SpecialRenderer.ts +++ b/src/render/SpecialRenderer.ts @@ -3,6 +3,7 @@ import { Direction, Identifier } from '../core/index.js' import { BlockDefinition } from './BlockDefinition.js' import { BlockModel } from './BlockModel.js' import type { Cull } from './Cull.js' +import type { Mesh } from './Mesh.js' import type { TextureAtlasProvider } from './TextureAtlas.js' function dummy(id: Identifier, uvProvider: TextureAtlasProvider, cull: Cull, model: BlockModel) { @@ -124,7 +125,7 @@ function decoratedPotRenderer(uvProvider: TextureAtlasProvider){ } export const SpecialRenderer: { - [key: string]: (props: { [key: string]: string }, uvProvider: TextureAtlasProvider, cull: Cull) => any, + [key: string]: (props: { [key: string]: string }, uvProvider: TextureAtlasProvider, cull: Cull) => Mesh, } = { 'minecraft:water': (props, uvProvider, cull) => liquidRenderer('water', parseInt(props.level), uvProvider, cull, 0),