Skip to content

Commit

Permalink
#34 add support for waterlogged blocks (#35)
Browse files Browse the repository at this point in the history
* #34 add support for waterlogged

* Small stylistic changes

* Remove the need for SpecialRenderers.has

---------

Co-authored-by: Misode <[email protected]>
  • Loading branch information
casu-dev and misode authored Sep 5, 2024
1 parent 0cef5d6 commit fb783d6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions demo/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Promise.all([
structure.addBlock([1, 0, 0], 'minecraft:stone')
structure.addBlock([2, 0, 0], 'minecraft:grass_block', { snowy: 'false' })
structure.addBlock([1, 1, 0], 'minecraft:cake', { bites: '3' })
structure.addBlock([2, 1, 0], 'minecraft:acacia_fence', { waterlogged: 'true' })
structure.addBlock([0, 0, 0], 'minecraft:wall_torch', { facing: 'west' })

const structureCanvas = document.getElementById('structure-display') as HTMLCanvasElement
Expand Down
4 changes: 4 additions & 0 deletions src/core/BlockState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class BlockState {
return this.is(BlockState.WATER) || this.is(BlockState.LAVA)
}

public isWaterlogged() {
return this.is(BlockState.WATER) || this.is(BlockState.LAVA) || this.properties['waterlogged'] === 'true'
}

public equals(other: BlockState) {
if (!this.name.equals(other.name)) {
return false
Expand Down
11 changes: 6 additions & 5 deletions src/render/ChunkBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mat4, vec3 } from 'gl-matrix'
import type { PlacedBlock, Resources, StructureProvider } from '../index.js'
import { BlockPos, Direction, Vector } from '../index.js'
import { Mesh } from './Mesh.js'
import { SpecialRenderer, SpecialRenderers } from './SpecialRenderer.js'
import { SpecialRenderers } from './SpecialRenderer.js'

export class ChunkBuilder {
private chunks: {mesh: Mesh, transparentMesh: Mesh}[][][] = []
Expand Down Expand Up @@ -69,8 +69,9 @@ export class ChunkBuilder {
if (blockDefinition) {
mesh.merge(blockDefinition.getMesh(blockName, blockProps, this.resources, this.resources, cull))
}
if (SpecialRenderers.has(blockName.toString())) {
mesh.merge(SpecialRenderer[blockName.toString()](blockProps, this.resources, cull))
const specialMesh = SpecialRenderers.getMesh(blockName.toString(), blockProps, this.resources, cull)
if (!specialMesh.isEmpty()) {
mesh.merge(specialMesh)
}
if (!mesh.isEmpty()) {
this.finishChunkMesh(mesh, b.pos)
Expand Down Expand Up @@ -114,9 +115,9 @@ export class ChunkBuilder {
}

if (neighborFlags?.opaque) {
return !(dir === Direction.UP && block.state.isFluid())
return !(dir === Direction.UP && block.state.isWaterlogged())
} else {
return block.state.isFluid() && neighbor.isFluid()
return block.state.isWaterlogged() && neighbor.isWaterlogged()
}
}

Expand Down
6 changes: 3 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 { Cull, SpecialRenderer, SpecialRenderers, type Color } from '../index.js'
import { Cull, 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 @@ -49,8 +49,8 @@ export class ItemRenderer extends Renderer {
tint = getItemColor(this.item)
}
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())
const specialMesh = SpecialRenderers.getMesh(this.item.id.toString(), {}, this.resources, Cull.none())
if (!specialMesh.isEmpty()) {
// undo the scaling done by the special renderer
const t = mat4.create()
mat4.identity(t)
Expand Down
17 changes: 14 additions & 3 deletions src/render/SpecialRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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 { 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 +124,7 @@ function decoratedPotRenderer(uvProvider: TextureAtlasProvider){
]))
}

export const SpecialRenderer: {
const RENDERERS: {
[key: string]: (props: { [key: string]: string }, uvProvider: TextureAtlasProvider, cull: Cull) => Mesh,
} = {
'minecraft:water': (props, uvProvider, cull) =>
Expand All @@ -137,4 +137,15 @@ export const SpecialRenderer: {
decoratedPotRenderer(uvProvider),
}

export const SpecialRenderers = new Set(Object.keys(SpecialRenderer))
export namespace SpecialRenderers {
export function getMesh(id: string, props: { [key: string]: string }, uvProvider: TextureAtlasProvider, cull: Cull): Mesh {
const mesh = new Mesh()
if (id in RENDERERS) {
mesh.merge(RENDERERS[id](props, uvProvider, cull))
}
if (props['waterlogged'] === 'true') {
mesh.merge(liquidRenderer('water', 0, uvProvider, cull, 0))
}
return mesh
}
}

0 comments on commit fb783d6

Please sign in to comment.