From 874cb4fe161ce7b2e683fe47fe6ff5032651a5c5 Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 6 Nov 2026 01:36:59 +0100 Subject: [PATCH] Add the 3D world view The terrain around where the map last looked, 1,024 blocks square, in 3D: one vertex per map pixel from the biome tiles with heights, coloured by biome, region or height, lit by slope. Walls round the rim and a see-through sea at fluid height make the square read as a solid block. The viewport now takes content with its own bounds and frames again when told to, so the scene views and the 3D world share it. The near clip follows the orbit distance, so surfaces a block apart no longer flicker when the camera is far away. --- src/renderer/src/scene/sceneMesh.ts | 23 ++- src/renderer/src/scene/terrainMesh.ts | 199 ++++++++++++++++++++++ src/renderer/src/studio/MapStudio.tsx | 23 ++- src/renderer/src/studio/SceneStudio.tsx | 5 +- src/renderer/src/studio/SceneViewport.tsx | 92 ++++++---- src/renderer/src/studio/ViewList.tsx | 3 +- src/renderer/src/studio/WorldStudio3D.tsx | 109 ++++++++++++ 7 files changed, 406 insertions(+), 48 deletions(-) create mode 100644 src/renderer/src/scene/terrainMesh.ts create mode 100644 src/renderer/src/studio/WorldStudio3D.tsx diff --git a/src/renderer/src/scene/sceneMesh.ts b/src/renderer/src/scene/sceneMesh.ts index 17a95dfc9..07fd89746 100644 --- a/src/renderer/src/scene/sceneMesh.ts +++ b/src/renderer/src/scene/sceneMesh.ts @@ -1,16 +1,20 @@ -import { BufferAttribute, BufferGeometry } from 'three' +import { Box3, BufferAttribute, BufferGeometry, Mesh, MeshBasicMaterial } from 'three' +import type { ViewportContent } from '../studio/SceneViewport' import { cubeModel, parseState, type BlockModels, type BlockModel, type Direction, type Quad } from './blockModels' import { blockTint } from './blockTints' import { INTS_PER_BLOCK, type BlockScene } from './sceneDecoding' import { TextureAtlas, type ImageSource } from './textureAtlas' export interface SceneMesh { - geometry: BufferGeometry - atlas: TextureAtlas + content: ViewportContent /** Block states the client has no blockstate file for, drawn as magenta and black cubes. */ unknownStates: string[] + /** Frees the mesh's geometry, material and atlas. */ + dispose(): void } +const CUTOUT_ALPHA = 0.1 + /** The light Minecraft gives each side of a block without shadows: the top brightest, the bottom darkest. */ const FACE_LIGHT: Record = { up: 1, down: 0.5, north: 0.8, south: 0.8, west: 0.6, east: 0.6 } const MISSING_TEXTURE = 'iris:missing' @@ -47,7 +51,18 @@ export async function buildSceneMesh(scene: BlockScene, models: BlockModels, ima const solid = model.fullCube && model.quads.every((quad) => atlas.cell(quad.texture).solid) return { model, name: state.name, properties: state.properties, solid } }) - return { geometry: geometryOf(scene, palette, atlas), atlas, unknownStates } + const geometry = geometryOf(scene, palette, atlas) + const material = new MeshBasicMaterial({ map: atlas.texture, vertexColors: true, alphaTest: CUTOUT_ALPHA }) + const bounds = new Box3().setFromBufferAttribute(geometry.getAttribute('position') as BufferAttribute) + return { + content: { object: new Mesh(geometry, material), bounds }, + unknownStates, + dispose: () => { + geometry.dispose() + material.dispose() + atlas.dispose() + } + } } // The client draws chests, signs, water and the like without a block model; their break texture stands in for them. diff --git a/src/renderer/src/scene/terrainMesh.ts b/src/renderer/src/scene/terrainMesh.ts new file mode 100644 index 000000000..175b3cbaa --- /dev/null +++ b/src/renderer/src/scene/terrainMesh.ts @@ -0,0 +1,199 @@ +import { Box3, BufferAttribute, BufferGeometry, DoubleSide, Group, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from 'three' +import { NO_LABEL, type MapTile } from '../bridge/types' +import { heightColor } from '../map/heightColors' +import type { ViewportContent } from '../studio/SceneViewport' + +export type TerrainColoring = 'biomes' | 'regions' | 'height' + +/** A square of map tiles with heights, row by row from the north west. */ +export interface TerrainArea { + tiles: MapTile[][] + /** The world spot of the first tile's first pixel. */ + originX: number + originZ: number +} + +export interface TerrainMesh { + content: ViewportContent + dispose(): void +} + +const WATER_COLOR = 0x3f76e4 +const WATER_OPACITY = 0.55 +const UNLABELLED_COLOR = 0x5a5f6b +// Light from high in the south west, so slopes read as relief; the darkest side keeps some light. +const LIGHT = new Vector3(-0.6, 1, 0.4).normalize() +const AMBIENT_LIGHT = 0.5 +const WALL_DEPTH_BLOCKS = 8 +const WALL_TOP_LIGHT = 0.55 +const WALL_BASE_LIGHT = 0.25 +const SEA_INSET_BLOCKS = 0.05 + +/** The terrain of the area, one vertex per map pixel, with the dimension's sea as a sheet of water at fluid height. */ +export function buildTerrainMesh(area: TerrainArea, coloring: TerrainColoring, fluidHeight: number): TerrainMesh { + const grid = TerrainGrid.of(area) + const terrain = terrainGeometry(grid, coloring, fluidHeight) + const base = Math.min(grid.lowestHeight(), fluidHeight) - WALL_DEPTH_BLOCKS + const landWalls = rimWall(grid, terrain, (ground) => ground, () => base) + const seaWalls = rimWall(grid, terrain, () => fluidHeight, (ground) => Math.min(ground, fluidHeight)) + const terrainMaterial = new MeshBasicMaterial({ vertexColors: true, side: DoubleSide }) + const sea = seaSurface(grid, fluidHeight) + const waterMaterial = new MeshBasicMaterial({ color: WATER_COLOR, transparent: true, opacity: WATER_OPACITY, depthWrite: false, side: DoubleSide }) + const group = new Group() + group.add(new Mesh(terrain, terrainMaterial), new Mesh(landWalls, terrainMaterial), new Mesh(sea, waterMaterial), new Mesh(seaWalls, waterMaterial)) + const geometries = [terrain, landWalls, sea, seaWalls] + return { + content: { object: group, bounds: new Box3().setFromObject(group) }, + dispose: () => { + geometries.forEach((geometry) => geometry.dispose()) + terrainMaterial.dispose() + waterMaterial.dispose() + } + } +} + +// Kept a little inside the walls, so its edge does not flicker against them. +function seaSurface(grid: TerrainGrid, fluidHeight: number): BufferGeometry { + const sea = new PlaneGeometry(grid.spanX - 2 * SEA_INSET_BLOCKS, grid.spanZ - 2 * SEA_INSET_BLOCKS).rotateX(-Math.PI / 2) + return sea.translate(grid.worldX(0) + grid.spanX / 2, fluidHeight, grid.worldZ(0) + grid.spanZ / 2) +} + +/** + * A wall round the terrain's rim, from {@code topAt} down to {@code bottomAt} of the ground height at each rim vertex; + * in the terrain's colour, darkening downwards, for a material that uses vertex colours. + */ +function rimWall(grid: TerrainGrid, terrain: BufferGeometry, topAt: (ground: number) => number, bottomAt: (ground: number) => number): BufferGeometry { + const rim = grid.rimVertices() + const surface = terrain.getAttribute('position') + const shade = terrain.getAttribute('color') + const positions = new Float32Array(rim.length * 2 * 3) + const colors = new Float32Array(rim.length * 2 * 3) + rim.forEach((vertex, index) => { + const [x, ground, z] = [surface.getX(vertex), surface.getY(vertex), surface.getZ(vertex)] + positions.set([x, topAt(ground), z, x, bottomAt(ground), z], index * 6) + const [red, green, blue] = [shade.getX(vertex), shade.getY(vertex), shade.getZ(vertex)] + colors.set([red * WALL_TOP_LIGHT, green * WALL_TOP_LIGHT, blue * WALL_TOP_LIGHT], index * 6) + colors.set([red * WALL_BASE_LIGHT, green * WALL_BASE_LIGHT, blue * WALL_BASE_LIGHT], index * 6 + 3) + }) + const indices: number[] = [] + for (let index = 0; index < rim.length; index++) { + const next = (index + 1) % rim.length + const [upper, lower, nextUpper, nextLower] = [index * 2, index * 2 + 1, next * 2, next * 2 + 1] + indices.push(upper, lower, nextUpper, nextUpper, lower, nextLower) + } + const geometry = new BufferGeometry() + geometry.setAttribute('position', new BufferAttribute(positions, 3)) + geometry.setAttribute('color', new BufferAttribute(colors, 3)) + geometry.setIndex(indices) + return geometry +} + +function terrainGeometry(grid: TerrainGrid, coloring: TerrainColoring, fluidHeight: number): BufferGeometry { + const { columns, rows } = grid + const positions = new Float32Array(columns * rows * 3) + const colors = new Float32Array(columns * rows * 3) + for (let row = 0; row < rows; row++) { + for (let column = 0; column < columns; column++) { + const vertex = row * columns + column + const height = grid.height(column, row) + positions.set([grid.worldX(column), height, grid.worldZ(row)], vertex * 3) + const color = coloring === 'height' ? heightColor(height, fluidHeight) : grid.labelColor(column, row, coloring) + const light = AMBIENT_LIGHT + (1 - AMBIENT_LIGHT) * Math.max(0, grid.normal(column, row).dot(LIGHT)) + colors.set([(((color >> 16) & 0xff) / 255) * light, (((color >> 8) & 0xff) / 255) * light, ((color & 0xff) / 255) * light], vertex * 3) + } + } + const indices = new Uint32Array((columns - 1) * (rows - 1) * 6) + let next = 0 + for (let row = 0; row < rows - 1; row++) { + for (let column = 0; column < columns - 1; column++) { + const northWest = row * columns + column + const southWest = northWest + columns + indices.set([northWest, southWest, northWest + 1, northWest + 1, southWest, southWest + 1], next) + next += 6 + } + } + const geometry = new BufferGeometry() + geometry.setAttribute('position', new BufferAttribute(positions, 3)) + geometry.setAttribute('color', new BufferAttribute(colors, 3)) + geometry.setIndex(new BufferAttribute(indices, 1)) + return geometry +} + +/** The area's tiles as one grid of pixels, without the ring each tile carries for its neighbours. */ +class TerrainGrid { + readonly columns: number + readonly rows: number + /** How far the vertices reach, west to east and north to south, in blocks. */ + readonly spanX: number + readonly spanZ: number + private readonly size: number + private readonly blocksPerPixel: number + + private constructor(private readonly area: TerrainArea) { + const first = area.tiles[0][0] + this.size = first.size + this.blocksPerPixel = first.blocksPerPixel + this.columns = area.tiles[0].length * this.size + this.rows = area.tiles.length * this.size + this.spanX = (this.columns - 1) * this.blocksPerPixel + this.spanZ = (this.rows - 1) * this.blocksPerPixel + } + + static of(area: TerrainArea): TerrainGrid { + return new TerrainGrid(area) + } + + worldX(column: number): number { + return this.area.originX + column * this.blocksPerPixel + } + + worldZ(row: number): number { + return this.area.originZ + row * this.blocksPerPixel + } + + height(column: number, row: number): number { + const { tile, cell } = this.cellAt(column, row) + return tile.heights![cell] + } + + labelColor(column: number, row: number, coloring: 'biomes' | 'regions'): number { + const { tile, cell } = this.cellAt(column, row) + const index = coloring === 'regions' ? tile.regions[cell] : tile.subjects[cell] + return index === NO_LABEL ? UNLABELLED_COLOR : tile.labels[index].color + } + + lowestHeight(): number { + let lowest = Infinity + for (let row = 0; row < this.rows; row++) { + for (let column = 0; column < this.columns; column++) { + lowest = Math.min(lowest, this.height(column, row)) + } + } + return lowest + } + + /** The vertices on the edge of the grid, once each, going round clockwise from the north west corner. */ + rimVertices(): number[] { + const { columns, rows } = this + const rim: number[] = [] + for (let column = 0; column < columns - 1; column++) rim.push(column) + for (let row = 0; row < rows - 1; row++) rim.push(row * columns + columns - 1) + for (let column = columns - 1; column > 0; column--) rim.push((rows - 1) * columns + column) + for (let row = rows - 1; row > 0; row--) rim.push(row * columns) + return rim + } + + normal(column: number, row: number): Vector3 { + const west = this.height(Math.max(0, column - 1), row) + const east = this.height(Math.min(this.columns - 1, column + 1), row) + const north = this.height(column, Math.max(0, row - 1)) + const south = this.height(column, Math.min(this.rows - 1, row + 1)) + return new Vector3(west - east, 2 * this.blocksPerPixel, north - south).normalize() + } + + private cellAt(column: number, row: number): { tile: MapTile; cell: number } { + const tile = this.area.tiles[Math.floor(row / this.size)][Math.floor(column / this.size)] + const gridSize = this.size + 2 + return { tile, cell: ((row % this.size) + 1) * gridSize + (column % this.size) + 1 } + } +} diff --git a/src/renderer/src/studio/MapStudio.tsx b/src/renderer/src/studio/MapStudio.tsx index a979b380e..a3a2e3498 100644 --- a/src/renderer/src/studio/MapStudio.tsx +++ b/src/renderer/src/studio/MapStudio.tsx @@ -12,6 +12,7 @@ import { MapCanvas, type CanvasPoint } from './MapCanvas' import { PickInspector } from './PickInspector' import { RegionPreviewList } from './RegionPreviewList' import { SceneStudio } from './SceneStudio' +import { WorldStudio3D } from './WorldStudio3D' import { actionForKey, viewForKey, type ShortcutAction } from './shortcuts' import { StatusBar } from './StatusBar' import { StudioToolbar } from './StudioToolbar' @@ -59,8 +60,9 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps const [packRevision, setPackRevision] = useState(0) const [sceneSummary, setSceneSummary] = useState(null) const sceneViews = useSceneViews(client, packRevision) - const views = useMemo(() => [...world.views, ...sceneViews.map(asViewInfo)], [world.views, sceneViews]) + const views = useMemo(() => [...withWorld3D(world.views), ...sceneViews.map(asViewInfo)], [world.views, sceneViews]) const sceneView = sceneViews.find((candidate) => candidate.id === viewId) ?? null + const showsMap = sceneView === null && viewId !== WORLD_3D.id const view = views.find((candidate) => candidate.id === viewId) ?? views[0] const layer = useMemo(() => ({ view: viewId, preview: viewId === 'regions' ? preview : null }), [viewId, preview]) const regions = usePreviewableRegions(client, packRevision) @@ -200,7 +202,7 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps world={world} toggles={toggles} hasPlayers={world.hasPlayers} - mapTools={sceneView === null} + mapTools={showsMap} theme={theme} onToggle={toggle} onRefresh={() => runShortcut('refresh')} @@ -214,6 +216,8 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps {sceneView !== null ? ( + ) : viewId === WORLD_3D.id ? ( + ) : ( <>
@@ -269,13 +273,26 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps )}
- + ) } const SCENE_GROUP = { group: 'BLOCKS', groupLabel: 'Blocks' } +/** The app draws the 3D world from the server's biome tiles, so the server does not list it among its views. */ +const WORLD_3D: ViewInfo = { + id: 'world3d', + label: '3D world', + description: 'The terrain in 3D, coloured by biome, region or height', + group: 'WORLD', + groupLabel: 'World' +} + +function withWorld3D(serverViews: ViewInfo[]): ViewInfo[] { + const afterHeight = serverViews.findIndex((candidate) => candidate.id === 'height') + 1 + return [...serverViews.slice(0, afterHeight), WORLD_3D, ...serverViews.slice(afterHeight)] +} function asViewInfo(view: SceneViewInfo): ViewInfo { return { ...view, ...SCENE_GROUP } diff --git a/src/renderer/src/studio/SceneStudio.tsx b/src/renderer/src/studio/SceneStudio.tsx index 6cfeefaa3..ffecd61dc 100644 --- a/src/renderer/src/studio/SceneStudio.tsx +++ b/src/renderer/src/studio/SceneStudio.tsx @@ -47,7 +47,7 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce return ( <>
- + {entry !== null && entry.variants > 1 && (