Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use SpecialRenderer in ItemRenderer #33

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions demo/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 3 additions & 7 deletions src/render/BlockModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down
14 changes: 12 additions & 2 deletions src/render/ItemRenderer.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/render/SpecialRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down