Sink studio 3D ground under water toward a deep colour by depth

The 3D terrain only laid one flat blue sheet over the sea floor, so depth and every water line were hard to read. Ground under water now sinks toward a deep colour the further down it lies, with a lighter sheet over it.

Studio only: no pack, seed or Java pin moved. Not run in the app.

v3: 23182951f
This commit is contained in:
piexel committed 2026-11-10 12:04:13 +01:00
1 parent 69b0303148
commit df80f2a99d
6 files changed
+151 -29

No files matched your search

+47 -17
View File
@@ -1,5 +1,6 @@
import type { ColumnField } from './columnField'
import { QuadBatch, type Vec3 } from './quadBatch'
import { submergedTint, sunk, tintAt } from './waterTint'
// Light on each face, as Minecraft shades blocks: tops full, north and south sides lighter than east and west.
const TOP_LIGHT = 1
@@ -11,6 +12,8 @@ interface Span {
bottom: number
top: number
color: number
/** The top face of the water the span lies under, or null where it lies in the open. */
water: number | null
}
interface Side {
@@ -30,13 +33,14 @@ const SIDES: readonly Side[] = [
/**
* The ground as block faces: the top of every column, and the sides where a column stands above its neighbour, or
* above {@code base} at the field's edge and next to columns where nothing is known. Neighbouring columns showing the
* same face in the same colour are one face.
* same face in the same colour are one face. Ground under water is sunk toward a deep colour, the more the further down
* it lies.
*/
export function groundFaces(field: ColumnField, base: number): QuadBatch {
const batch = new QuadBatch(true)
addTops(field, batch, (column, row) => {
const top = field.hasGround(column, row) ? field.groundTop[field.indexOf(column, row)] : null
return top === null ? null : { bottom: top, top, color: field.color[field.indexOf(column, row)] }
return top === null ? null : { bottom: top, top, color: field.color[field.indexOf(column, row)], water: field.waterTopAt(column, row) }
})
addSides(field, batch, (column, row, side) => {
if (!field.hasGround(column, row)) {
@@ -45,7 +49,11 @@ export function groundFaces(field: ColumnField, base: number): QuadBatch {
const top = field.groundTop[field.indexOf(column, row)]
const [nextColumn, nextRow] = [column + side.columnStep, row + side.rowStep]
const bottom = field.hasGround(nextColumn, nextRow) ? field.groundTop[field.indexOf(nextColumn, nextRow)] : base
return bottom < top ? { bottom, top, color: field.color[field.indexOf(column, row)] } : null
if (bottom >= top) {
return null
}
const water = field.waterTopAt(column, row) ?? field.waterTopAt(nextColumn, nextRow)
return { bottom, top, color: field.color[field.indexOf(column, row)], water }
})
return batch
}
@@ -57,19 +65,12 @@ export function groundFaces(field: ColumnField, base: number): QuadBatch {
*/
export function waterFaces(field: ColumnField): QuadBatch {
const batch = new QuadBatch(false)
const waterAt = (column: number, row: number): number | null => {
if (!field.contains(column, row)) {
return null
}
const top = field.waterTop[field.indexOf(column, row)]
return Number.isNaN(top) ? null : top
}
addTops(field, batch, (column, row) => {
const top = waterAt(column, row)
return top === null ? null : { bottom: top, top, color: WATER_COLOR }
const top = field.waterTopAt(column, row)
return top === null ? null : { bottom: top, top, color: WATER_COLOR, water: null }
})
addSides(field, batch, (column, row, side) => {
const top = waterAt(column, row)
const top = field.waterTopAt(column, row)
if (top === null) {
return null
}
@@ -78,7 +79,7 @@ export function waterFaces(field: ColumnField): QuadBatch {
return null
}
const bottom = Math.max(next, field.groundTop[field.indexOf(column, row)])
return bottom < top ? { bottom, top, color: WATER_COLOR } : null
return bottom < top ? { bottom, top, color: WATER_COLOR, water: null } : null
})
return batch
}
@@ -87,7 +88,7 @@ function addTops(field: ColumnField, batch: QuadBatch, spanAt: (column: number,
for (let row = 0; row < field.rows; row++) {
mergeRuns(field.columns, (column) => spanAt(column, row), (first, count, span) => {
const step = field.blocksPerColumn
batch.add([field.originX + first * step, span.top, field.originZ + row * step], [count * step, 0, 0], [0, 0, step], shaded(span.color, TOP_LIGHT))
batch.add([field.originX + first * step, span.top, field.originZ + row * step], [count * step, 0, 0], [0, 0, step], topColor(span))
})
}
}
@@ -101,12 +102,41 @@ function addSides(field: ColumnField, batch: QuadBatch, spanAt: (column: number,
const columnOf = (step: number): number => (runsAlongRow ? step : lane)
const rowOf = (step: number): number => (runsAlongRow ? lane : step)
mergeRuns(length, (step) => spanAt(columnOf(step), rowOf(step), side), (first, count, span) => {
batch.add(...sideQuad(field, side, columnOf(first), rowOf(first), count, span), shaded(span.color, side.light))
const light = shaded(span.color, side.light)
for (const part of sideParts(span)) {
batch.add(...sideQuad(field, side, columnOf(first), rowOf(first), count, part), sunk(light, part.bottomTint), sunk(light, part.topTint))
}
})
}
}
}
function topColor(span: Span): Vec3 {
const light = shaded(span.color, TOP_LIGHT)
return span.water === null ? light : sunk(light, tintAt(span.water - span.top))
}
interface SidePart extends Span {
/** How much of the deep colour covers the bottom and the top of the part. */
bottomTint: number
topTint: number
}
// A side reaching above the water is cut at its surface: only the part below sinks, deeper toward its bottom.
function sideParts(span: Span): SidePart[] {
const { bottom, top, water } = span
if (water === null || water <= bottom) {
return [{ ...span, bottomTint: 0, topTint: 0 }]
}
if (water >= top) {
return [{ ...span, bottomTint: submergedTint(water - bottom), topTint: submergedTint(water - top) }]
}
return [
{ ...span, top: water, bottomTint: submergedTint(water - bottom), topTint: submergedTint(0) },
{ ...span, bottom: water, bottomTint: 0, topTint: 0 }
]
}
// The corner a side starts from is the one on its left as seen from outside, so it runs and rises into view.
function sideQuad(field: ColumnField, side: Side, column: number, row: number, count: number, span: Span): [Vec3, Vec3, Vec3] {
const step = field.blocksPerColumn
@@ -144,7 +174,7 @@ function mergeRuns(length: number, spanAt: (index: number) => Span | null, emit:
}
function sameSpan(span: Span, other: Span | null): boolean {
return other !== null && other.top === span.top && other.bottom === span.bottom && other.color === span.color
return other !== null && other.top === span.top && other.bottom === span.bottom && other.color === span.color && other.water === span.water
}
function shaded(color: number, light: number): Vec3 {
@@ -61,6 +61,15 @@ export class ColumnField {
return this.groundTop.some((top) => !Number.isNaN(top))
}
/** The top face of the water over the column, or null where it is dry, holds none or lies outside the field. */
waterTopAt(column: number, row: number): number | null {
if (!this.contains(column, row)) {
return null
}
const top = this.waterTop[this.indexOf(column, row)]
return Number.isNaN(top) ? null : top
}
/** The face the column shows to the sky: its water where it has some, else its ground; null where nothing is known. */
surfaceTop(column: number, row: number): number | null {
if (!this.hasGround(column, row)) {
+5 -4
View File
@@ -21,9 +21,9 @@ export class QuadBatch {
/**
* A quad from {@code origin} along {@code along} and {@code up}; it faces the way {@code up} crosses {@code along}.
* Colours are red, green and blue from 0 to 1.
* Colours are red, green and blue from 0 to 1; the corners at the far end of {@code up} take {@code topColor}.
*/
add(origin: Vec3, along: Vec3, up: Vec3, color: Vec3 | null): void {
add(origin: Vec3, along: Vec3, up: Vec3, color: Vec3 | null, topColor: Vec3 | null = color): void {
this.reserveOne()
const first = this.quads * VERTICES_PER_QUAD * COMPONENTS
for (let corner = 0; corner < VERTICES_PER_QUAD; corner++) {
@@ -31,8 +31,9 @@ export class QuadBatch {
for (let axis = 0; axis < COMPONENTS; axis++) {
const at = first + corner * COMPONENTS + axis
this.positions[at] = origin[axis] + along[axis] * alongSteps + up[axis] * upSteps
if (this.colors !== null && color !== null) {
this.colors[at] = color[axis]
const cornerColor = upSteps === 1 ? topColor : color
if (this.colors !== null && cornerColor !== null) {
this.colors[at] = cornerColor[axis]
}
}
}
@@ -1,6 +1,7 @@
import { Vector3, type BufferGeometry, type Mesh } from 'three'
import { describe, expect, it } from 'vitest'
import { buildTerrainMesh, type TerrainSurface } from './terrainMesh'
import { submergedTint, tintAt } from './waterTint'
const GROUND = 10
const POND_GROUND = 7
@@ -9,6 +10,9 @@ const BASE_DEPTH = 8
const BASE = POND_GROUND + 1 - BASE_DEPTH
const GREY = 0x808080
const RED = 0xff0000
const GREY_LIGHT = 0x80 / 255
const SIDE_LIGHT = 0.6
const DEEP_RED = 0x0f / 255
// Three by three columns: nothing known at the north west corner, a pond at the middle column.
function surfaceOf(colorAt: TerrainSurface['colorAt'] = () => GREY): TerrainSurface {
@@ -30,6 +34,7 @@ function surfaceOf(colorAt: TerrainSurface['colorAt'] = () => GREY): TerrainSurf
}
const surface = surfaceOf()
const dry: TerrainSurface = { ...surface, waterAt: () => null }
function meshes(terrain: TerrainSurface = surface): { ground: Mesh; water: Mesh } {
const [ground, water] = buildTerrainMesh(terrain, BASE).object.children as Mesh[]
@@ -55,6 +60,15 @@ function faces(geometry: BufferGeometry): { middle: Vector3; normal: Vector3 }[]
return found
}
function redOf(geometry: BufferGeometry, vertex: number): number {
return geometry.getAttribute('color').getX(vertex)
}
// The red of the grey ground after sinking by the tint, lit as given.
function sunkRed(tint: number, light = 1): number {
return GREY_LIGHT * light + (DEEP_RED - GREY_LIGHT * light) * tint
}
describe('terrain mesh', () => {
it('draws one top for each run of neighbouring columns at one height', () => {
const { ground } = meshes()
@@ -64,7 +78,7 @@ describe('terrain mesh', () => {
})
it('draws a side only where a column stands above its neighbour or beside nothing known', () => {
const { ground } = meshes()
const { ground } = meshes(dry)
expect(quads(ground) - faces(ground.geometry).filter((face) => face.normal.y === 1).length).toBe(10)
})
@@ -75,7 +89,7 @@ describe('terrain mesh', () => {
})
it('faces every side outwards from the column it belongs to', () => {
const { ground } = meshes()
const { ground } = meshes(dry)
const sides = faces(ground.geometry).filter((face) => face.normal.y === 0)
const pondSide = sides.find((side) => side.middle.x === 101 + 1 && side.normal.x === -1 && side.middle.z === 201.5)!
expect(pondSide.middle.y).toBe((POND_GROUND + 1 + GROUND + 1) / 2)
@@ -84,7 +98,7 @@ describe('terrain mesh', () => {
})
it('lights tops fully and sides less', () => {
const colors = meshes().ground.geometry.getAttribute('color')
const colors = meshes(dry).ground.geometry.getAttribute('color')
const lights = new Set<number>()
for (let vertex = 0; vertex < colors.count; vertex += 4) {
lights.add(Math.round((colors.getX(vertex) * 255 * 100) / 0x80) / 100)
@@ -129,6 +143,42 @@ describe('terrain mesh', () => {
expect(faces(ground.geometry).filter((face) => face.normal.y === 0 && face.middle.x === 102.5 && face.middle.z === 202)).toHaveLength(1)
})
it('sinks the ground under water toward the deep colour, the more the deeper it lies', () => {
const pondTopRed = (terrain: TerrainSurface): number => {
const ground = meshes(terrain).ground.geometry
const pond = faces(ground).findIndex((face) => face.normal.y === 1 && face.middle.x === 101.5 && face.middle.z === 201.5)
return redOf(ground, pond * 4)
}
const shallow = pondTopRed(surface)
const deep = pondTopRed({ ...surface, waterAt: () => POND_GROUND + 30 })
expect(pondTopRed(dry)).toBeCloseTo(GREY_LIGHT)
expect(shallow).toBeCloseTo(sunkRed(tintAt(WATER - POND_GROUND)))
expect(deep).toBeLessThan(shallow)
})
it('leaves ground level with the water dry', () => {
const level = { ...surface, waterAt: (column: number, row: number) => (column === 1 && row === 1 ? POND_GROUND : null) }
const ground = meshes(level).ground.geometry
const pond = faces(ground).findIndex((face) => face.normal.y === 1 && face.middle.x === 101.5 && face.middle.z === 201.5)
expect(redOf(ground, pond * 4)).toBeCloseTo(GREY_LIGHT)
expect(tintAt(0)).toBe(0)
})
it('sinks only the part of a side under the water, deeper toward its bottom', () => {
const ground = meshes().ground.geometry
const toPond = faces(ground)
.map((face, quad) => ({ ...face, quad }))
.filter((face) => face.normal.x === -1 && face.middle.x === 102 && face.middle.z === 201.5)
expect(toPond).toHaveLength(2)
const [wet, above] = toPond.sort((first, second) => first.middle.y - second.middle.y)
const [wetBottom, wetTop] = [redOf(ground, wet.quad * 4), redOf(ground, wet.quad * 4 + 1)]
expect(wet.middle.y).toBe((POND_GROUND + 1 + WATER + 1) / 2)
expect(wetBottom).toBeLessThan(wetTop)
expect(wetTop).toBeCloseTo(sunkRed(submergedTint(0), SIDE_LIGHT))
expect(redOf(ground, above.quad * 4)).toBeCloseTo(GREY_LIGHT * SIDE_LIGHT)
expect(redOf(ground, above.quad * 4 + 1)).toBeCloseTo(GREY_LIGHT * SIDE_LIGHT)
})
it('has no mesh where no column has ground', () => {
const nothing = buildTerrainMesh({ ...surface, groundAt: () => null }, BASE)
expect(nothing.object.children).toHaveLength(0)
+3 -5
View File
@@ -2,6 +2,7 @@ import { DoubleSide, FrontSide, Group, Mesh, MeshBasicMaterial } from 'three'
import { groundFaces, waterFaces } from './columnFaces'
import { ColumnField } from './columnField'
import type { ColumnArea } from './terrainTiles'
import { WATER_SURFACE_COLOR, WATER_SURFACE_OPACITY } from './waterTint'
/**
* Terrain to draw as columns, row by row from the north west: the ground, the water over it, and the ground's colour.
@@ -28,12 +29,9 @@ export interface TerrainMesh {
dispose(): void
}
const WATER_COLOR = 0x3f76e4
const WATER_OPACITY = 0.55
/**
* The surface as columns with their faces merged where neighbours show the same one, flat-shaded as Minecraft shades
* blocks, with see-through water over them. Sides on the edge reach down to {@code base}, so tiles of ground next to
* 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.
*/
export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: readonly ColumnArea[] = []): TerrainMesh {
@@ -45,7 +43,7 @@ export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: r
const ground = groundFaces(field, base).geometry()
const water = waterFaces(field).geometry()
const groundMaterial = new MeshBasicMaterial({ vertexColors: true, side: FrontSide })
const waterMaterial = new MeshBasicMaterial({ color: WATER_COLOR, transparent: true, opacity: WATER_OPACITY, depthWrite: false, side: DoubleSide })
const waterMaterial = new MeshBasicMaterial({ color: WATER_SURFACE_COLOR, transparent: true, opacity: WATER_SURFACE_OPACITY, depthWrite: false, side: DoubleSide })
group.add(new Mesh(ground, groundMaterial), new Mesh(water, waterMaterial))
return {
object: group,
@@ -0,0 +1,34 @@
import type { Vec3 } from './quadBatch'
export const WATER_SURFACE_COLOR = 0x5a8ef0
export const WATER_SURFACE_OPACITY = 0.3
const DEEP_COLOR: Vec3 = [0x0f / 255, 0x25 / 255, 0x52 / 255]
const SHALLOW_TINT = 0.35
const DEEPEST_TINT = 0.85
/** How far down the tint covers about two thirds of the way from shallow to deepest. */
const TINT_DEPTH_BLOCKS = 24
/**
* How much of the deep colour covers ground this many blocks under the water's surface; none for ground at or above
* it, so ground level with the surface stays dry.
*/
export function tintAt(depthBlocks: number): number {
return depthBlocks <= 0 ? 0 : submergedTint(depthBlocks)
}
/** The same for ground known to lie under the surface, even at its very edge. */
export function submergedTint(depthBlocks: number): number {
return SHALLOW_TINT + (DEEPEST_TINT - SHALLOW_TINT) * (1 - Math.exp(-depthBlocks / TINT_DEPTH_BLOCKS))
}
/** The colour sunk toward the deep colour by {@code tint}, from 0 for none to 1 for all of it. */
export function sunk(color: Vec3, tint: number): Vec3 {
if (tint === 0) {
return color
}
return [mix(color[0], DEEP_COLOR[0], tint), mix(color[1], DEEP_COLOR[1], tint), mix(color[2], DEEP_COLOR[2], tint)]
}
function mix(from: number, to: number, share: number): number {
return from + (to - from) * share
}