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 1 commit
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
6 changes: 4 additions & 2 deletions src/render/BlockModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ export class BlockModel {
private guiLight?: BlockModelGuiLight | undefined,
) {}

public getDisplayMesh(display: Display, uvProvider: TextureAtlasProvider, tint?: Color | ((index: number) => Color)) {
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)
}
const transform = this.display?.[display]
const t = mat4.create()
mat4.identity(t)
Expand Down
15 changes: 12 additions & 3 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 { Identifier } from '../core/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,16 @@ 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)
var additionalMesh = undefined
if (SpecialRenderers.has(this.item.id.toString())){
additionalMesh = 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)
}
const mesh = model.getDisplayMesh('gui', this.resources, tint, additionalMesh)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the additionalMesh not be merged with the mesh returned from getDisplayMesh instead of passing it to the function?

const mesh = model.getDisplayMesh('gui', this.resources, tint)
mesh.merge(additionalMesh)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additionalMesh also need to get all the transformations that getDisplayMesh applies on the mesh, so it has to be merged before the transformations (or be transformed separately).

mesh.quads.forEach(q => {
const normal = q.normal()
q.forEach(v => v.normal = normal)
Expand Down