Skip to content

Commit

Permalink
Refactor item renderer to transform display seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Apr 22, 2024
1 parent 361c541 commit aae0448
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
12 changes: 3 additions & 9 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,11 +84,7 @@ export class BlockModel {
private guiLight?: BlockModelGuiLight | undefined,
) {}

public getDisplayMesh(display: Display, uvProvider: TextureAtlasProvider, tint?: Color | ((index: number) => Color), additionalMesh?: Mesh) {
const mesh = this.getMesh(uvProvider, Cull.none(), tint)
if (additionalMesh){
mesh.merge(additionalMesh)
}
public getDisplayTransform(display: Display) {
const transform = this.display?.[display]
const t = mat4.create()
mat4.identity(t)
Expand All @@ -105,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
11 changes: 6 additions & 5 deletions src/render/ItemRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mat4 } from 'gl-matrix'
import { ItemStack } from '../core/ItemStack.js'
import { Identifier } from '../core/index.js'
import { ItemStack } from '../core/ItemStack.js'
import { Cull, SpecialRenderer, SpecialRenderers, type Color } from '../index.js'
import type { BlockModelProvider } from './BlockModel.js'
import { getItemColor } from './ItemColors.js'
Expand Down Expand Up @@ -48,16 +48,17 @@ export class ItemRenderer extends Renderer {
if (!tint && this.item.id.namespace === Identifier.DEFAULT_NAMESPACE) {
tint = getItemColor(this.item)
}
var additionalMesh = undefined
const mesh = model.getMesh(this.resources, Cull.none(), tint)
if (SpecialRenderers.has(this.item.id.toString())){
additionalMesh = SpecialRenderer[this.item.id.toString()]({}, this.resources, Cull.none())
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])
additionalMesh.transform(t)
specialMesh.transform(t)
mesh.merge(specialMesh)
}
const mesh = model.getDisplayMesh('gui', this.resources, tint, additionalMesh)
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

0 comments on commit aae0448

Please sign in to comment.