diff --git a/studio/src/renderer/src/scene/columnField.ts b/studio/src/renderer/src/scene/columnField.ts index aa80cb006..a0d60bdd6 100644 --- a/studio/src/renderer/src/scene/columnField.ts +++ b/studio/src/renderer/src/scene/columnField.ts @@ -1,6 +1,7 @@ import type { BlockLook } from './blockLooks' import type { CoverLook } from './coverLooks' import type { TerrainPictures } from './terrainAtlas' +import { isSeeThrough } from './raisedFaces' import type { RaisedBlock, TerrainSurface } from './terrainMesh' import type { ColumnArea } from './terrainTiles' @@ -86,13 +87,16 @@ export class ColumnField { return this.contains(column, row) && !Number.isNaN(this.groundTop[this.indexOf(column, row)]) } - /** Whether the block at {@code y} in the column is the column's ground or a block raised over it; false outside the field. */ - isSolidAt(column: number, row: number, y: number): boolean { + /** + * Whether the block at {@code y} in the column hides what touches it: the column's ground, or a block raised over it + * that nothing shows through. False outside the field. + */ + isOpaqueAt(column: number, row: number, y: number): boolean { if (!this.hasGround(column, row)) { return false } const index = this.indexOf(column, row) - return y < this.groundTop[index] || this.raised[index].some((block) => block.y === y) + return y < this.groundTop[index] || this.raised[index].some((block) => block.y === y && !isSeeThrough(block)) } /** Whether any column has ground. */ diff --git a/studio/src/renderer/src/scene/raisedFaces.ts b/studio/src/renderer/src/scene/raisedFaces.ts index 5e2ac58a6..0abb19fad 100644 --- a/studio/src/renderer/src/scene/raisedFaces.ts +++ b/studio/src/renderer/src/scene/raisedFaces.ts @@ -29,12 +29,18 @@ const BLOCK_FACES: readonly BlockFace[] = [ { columnStep: 1, yStep: 0, rowStep: 0, light: FACE_LIGHT.east, picture: (look) => look.side, standing: true, quad: (step) => [[step, 0, 0], [0, 0, step], [0, 1, 0]] } ] +/** Whether what lies behind a raised block shows through its pictures, as through leaves; never for one shown in its colour. */ +export function isSeeThrough(block: RaisedBlock): boolean { + return block.look !== null && (block.look.top.cell.hasGaps || block.look.side.cell.hasGaps) +} + /** - * The blocks raised above the ground, such as trees, as whole blocks: each face that touches neither the ground nor - * another raised block. A face at the field's edge is drawn, as the field next to it is not known here. Blocks under - * water sink toward its deep colour as the ground does. + * The blocks raised above the ground, such as trees, as whole blocks: each face that touches neither the ground nor an + * opaque raised block. Blocks that are seen through, such as leaves, go to {@code seeThrough}, to be drawn cut out, and + * hide nothing they touch, as Minecraft draws leaves with fancy graphics. A face at the field's edge is drawn, as the + * field next to it is not known here. Blocks under water sink toward its deep colour as the ground does. */ -export function addRaisedFaces(field: ColumnField, batch: QuadBatch): void { +export function addRaisedFaces(field: ColumnField, opaque: QuadBatch, seeThrough: QuadBatch): void { const step = field.blocksPerColumn for (let row = 0; row < field.rows; row++) { for (let column = 0; column < field.columns; column++) { @@ -42,8 +48,9 @@ export function addRaisedFaces(field: ColumnField, batch: QuadBatch): void { const water = field.waterTopAt(column, row) for (const block of field.raised[index]) { const corner: Vec3 = [field.originX + column * step, block.y, field.originZ + row * step] + const batch = isSeeThrough(block) ? seeThrough : opaque for (const face of BLOCK_FACES) { - if (field.isSolidAt(column + face.columnStep, row + face.rowStep, block.y + face.yStep)) { + if (field.isOpaqueAt(column + face.columnStep, row + face.rowStep, block.y + face.yStep)) { continue } addFace(batch, face, corner, step, block, water) diff --git a/studio/src/renderer/src/scene/terrainAtlas.ts b/studio/src/renderer/src/scene/terrainAtlas.ts index 8097a5221..b1aa5c74f 100644 --- a/studio/src/renderer/src/scene/terrainAtlas.ts +++ b/studio/src/renderer/src/scene/terrainAtlas.ts @@ -9,6 +9,8 @@ export interface UvCell { width: number height: number average: number + /** Whether the picture is open in places, as leaves are between their twigs, so what lies behind shows through. */ + hasGaps: boolean } /** The atlas as the terrain draws it. */ @@ -38,13 +40,14 @@ const OPAQUE_ENOUGH_ALPHA = 128 const GAP_SHADE = 0.6 /** The cell with this number, counted row by row from the top left. */ -export function cellAt(index: number, average: number): UvCell { +export function cellAt(index: number, average: number, hasGaps = false): UvCell { return { u: (index % CELLS_PER_SIDE) / CELLS_PER_SIDE, v: Math.floor(index / CELLS_PER_SIDE) / CELLS_PER_SIDE, width: 1 / CELLS_PER_SIDE, height: 1 / CELLS_PER_SIDE, - average + average, + hasGaps } } @@ -100,11 +103,12 @@ export class TerrainAtlas implements TerrainPictures, PictureSource { this.drawing.drawImage(bitmap, 0, 0, bitmap.width, Math.min(bitmap.width, bitmap.height), left, top, CELL_PIXELS, CELL_PIXELS) const picture = this.drawing.getImageData(left, top, CELL_PIXELS, CELL_PIXELS) const average = averageColor(picture) + const hasGaps = isOpenInPlaces(picture) this.cutoutDrawing.putImageData(cutoutOf(picture), left, top) this.drawing.putImageData(withGapsFilled(picture, average), left, top) this.texture.needsUpdate = true this.cutouts.needsUpdate = true - return { ...placed, average } + return { ...placed, average, hasGaps } } } @@ -122,6 +126,15 @@ function sheet(): [CanvasRenderingContext2D, CanvasTexture] { return [drawing, texture] } +function isOpenInPlaces(picture: ImageData): boolean { + for (let offset = 3; offset < picture.data.length; offset += 4) { + if (picture.data[offset] < OPAQUE_ENOUGH_ALPHA) { + return true + } + } + return false +} + // A canvas keeps no colour under a transparent pixel, so the picture cannot carry both its filled gaps and where they were. function cutoutOf(picture: ImageData): ImageData { const cutout = new ImageData(picture.width, picture.height) diff --git a/studio/src/renderer/src/scene/terrainMesh.ts b/studio/src/renderer/src/scene/terrainMesh.ts index 07cd5107e..8323aeb2e 100644 --- a/studio/src/renderer/src/scene/terrainMesh.ts +++ b/studio/src/renderer/src/scene/terrainMesh.ts @@ -1,9 +1,10 @@ -import { DoubleSide, FrontSide, Group, Mesh, MeshBasicMaterial } from 'three' +import { DoubleSide, FrontSide, Group, Mesh, MeshBasicMaterial, type BufferGeometry } from 'three' import type { BlockLook } from './blockLooks' import { groundFaces, waterFaces } from './columnFaces' import { ColumnField } from './columnField' import { coverGeometry } from './coverFaces' import type { CoverLook } from './coverLooks' +import { QuadBatch } from './quadBatch' import { addRaisedFaces } from './raisedFaces' import type { TerrainPictures } from './terrainAtlas' import type { ColumnArea } from './terrainTiles' @@ -57,7 +58,7 @@ export interface TerrainMesh { * blocks, with the ground under water sunk toward a deep colour and a light sheet of water over it. Sides on the edge reach down to {@code base}, so tiles of ground next to * each other leave no cracks. The columns in {@code holes} are left out, for finer ground to fill. Columns with a look * show their block's textures, one picture to a block face. Blocks raised above the ground, such as trees, stand as - * whole blocks. + * whole blocks; what is seen through, such as leaves, is cut out along its pictures. */ export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: readonly ColumnArea[] = []): TerrainMesh { const field = new ColumnField(surface, holes) @@ -66,14 +67,16 @@ export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: r return { object: group, field, dispose: () => {} } } const groundBatch = groundFaces(field, base) - addRaisedFaces(field, groundBatch) + const seeThroughBatch = new QuadBatch(true, field.pictures?.plain ?? null) + addRaisedFaces(field, groundBatch, seeThroughBatch) const ground = groundBatch.geometry() const water = waterFaces(field).geometry() const groundMaterial = new MeshBasicMaterial({ vertexColors: true, side: FrontSide, map: field.pictures?.texture ?? null }) const waterMaterial = new MeshBasicMaterial({ color: WATER_SURFACE_COLOR, transparent: true, opacity: WATER_SURFACE_OPACITY, depthWrite: false, side: DoubleSide }) - const cover = field.pictures === null ? null : coverMesh(field, field.pictures) + const cutOut = field.pictures === null ? [] : [cutOutMesh(coverGeometry(field), field.pictures), cutOutMesh(seeThroughGeometry(seeThroughBatch), field.pictures)] + const shownCutOut = cutOut.filter((mesh) => mesh !== null) // The water is drawn last, over what stands under it. - group.add(new Mesh(ground, groundMaterial), ...(cover === null ? [] : [cover.mesh]), new Mesh(water, waterMaterial)) + group.add(new Mesh(ground, groundMaterial), ...shownCutOut.map((mesh) => mesh.mesh), new Mesh(water, waterMaterial)) return { object: group, field, @@ -82,13 +85,17 @@ export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: r water.dispose() groundMaterial.dispose() waterMaterial.dispose() - cover?.dispose() + shownCutOut.forEach((mesh) => mesh.dispose()) } } } -function coverMesh(field: ColumnField, pictures: TerrainPictures): { mesh: Mesh; dispose(): void } | null { - const geometry = coverGeometry(field) +function seeThroughGeometry(batch: QuadBatch): BufferGeometry | null { + return batch.count === 0 ? null : batch.geometry() +} + +// Drawn where the pictures are not open, so the grass between its blades and the leaves between their twigs show what lies behind. +function cutOutMesh(geometry: BufferGeometry | null, pictures: TerrainPictures): { mesh: Mesh; dispose(): void } | null { if (geometry === null) { return null } diff --git a/studio/src/renderer/src/scene/texturedTerrain.test.ts b/studio/src/renderer/src/scene/texturedTerrain.test.ts index 714ab015f..a1a505579 100644 --- a/studio/src/renderer/src/scene/texturedTerrain.test.ts +++ b/studio/src/renderer/src/scene/texturedTerrain.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import type { BlockLook, FaceLook } from './blockLooks' import type { CoverLook } from './coverLooks' import { cellAt, type TerrainPictures } from './terrainAtlas' -import { buildTerrainMesh, type TerrainSurface } from './terrainMesh' +import { buildTerrainMesh, type RaisedBlock, type TerrainSurface } from './terrainMesh' const ORIGIN_X = 100 const ORIGIN_Z = 200 @@ -24,6 +24,10 @@ function face(index: number, tint = WHITE): FaceLook { return { cell: cellAt(index, 0x808080), tint, color: 0x606060 } } +function gappyFace(index: number): FaceLook { + return { cell: cellAt(index, 0x808080, true), tint: WHITE, color: 0x606060 } +} + const grass: BlockLook = { top: face(1, GRASS_TINT), side: face(2), below: face(3) } // Three by three columns of one-block ground, the middle one two blocks above the rest. @@ -170,6 +174,28 @@ describe('textured terrain', () => { expect(material.alphaTest).toBeGreaterThan(0) }) + it('cuts leaves out along their pictures and hides no face behind them, as with fancy graphics', () => { + const leaves: BlockLook = { top: gappyFace(5), side: gappyFace(5), below: gappyFace(5) } + const log: BlockLook = { top: face(6), side: face(7), below: face(7) } + // Two leaves stacked on the peak, a log beside the lower one. + const raisedAt = (column: number, row: number): RaisedBlock[] => { + if (column === PEAK_COLUMN && row === PEAK_COLUMN) { + return [{ y: PEAK_GROUND + 2, color: 0, look: leaves }, { y: PEAK_GROUND + 1, color: 0, look: leaves }] + } + return column === PEAK_COLUMN + 1 && row === PEAK_COLUMN ? [{ y: PEAK_GROUND + 1, color: 0, look: log }] : [] + } + const [ground, seeThrough, water] = buildTerrainMesh({ ...surfaceOf(() => grass), raisedAt }, FLOOR).object.children as Mesh[] + const leafFaces = placed(seeThrough.geometry) + const logBesideLeaf = placed(ground.geometry).filter((face) => face.normal.x === -1 && face.middle.x === ORIGIN_X + PEAK_COLUMN + 1 && face.middle.y === PEAK_GROUND + 1.5) + + expect(water).toBeDefined() + expect((seeThrough.material as MeshBasicMaterial).alphaMap).toBe(pictures.cutouts) + // The lower leaf shows all but its bottom on the ground and its side on the log; the upper one shows every face. + expect(leafFaces).toHaveLength(4 + 6) + expect(leafFaces.filter((face) => face.middle.y === PEAK_GROUND + 2)).toHaveLength(2) + expect(logBesideLeaf).toHaveLength(1) + }) + it('draws no cover mesh where nothing stands on the ground', () => { expect(buildTerrainMesh(surfaceOf(() => grass), FLOOR).object.children).toHaveLength(2) })