mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Show caves as a form with area sizes, and in the 3D world
The Caves view drops its node graph. Each cave file is one form beside the scene, with a section for the cave, its tunnel, its cavern and the pillars. A section the file lacks says "(unset)" and typing a value writes it. EditableSceneView.showsNodes() lets a view ask for this. Scenes can now be built at a size the studio picks. SceneView.sizes() lists them, and scene() and parts() take a SceneRequest with the key, variant and size. Caves offer 64 to 512 blocks across. The preview sends only the carved blocks on the outside of the space, so 512 stays light. The 3D world gets a Caves toggle. It generates the chunks of a 128, 256 or 512 square around the middle of the view and draws every air block under the terrain top, coloured by height band, with a legend.
This commit is contained in:
1 parent
e862a80615
commit
263f5baa3c
28 files changed
+958
-222
No files matched your search
@@ -40,6 +40,13 @@ export class BridgeError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/** Which scene of a scene view: an entry's variant, at one of the view's sizes or null when it has none. */
|
||||
export interface ScenePlace {
|
||||
key: string
|
||||
variant: number
|
||||
size: number | null
|
||||
}
|
||||
|
||||
export interface TileRequest {
|
||||
view: string
|
||||
/** The region the region view previews everywhere, or null for the world as it is. */
|
||||
@@ -213,17 +220,28 @@ export class BridgeClient {
|
||||
return this.json(`scene-bake?${new URLSearchParams({ view: viewId, key })}`, { signal })
|
||||
}
|
||||
|
||||
async scene(viewId: string, key: string, variant: number, signal?: AbortSignal): Promise<BlockScene> {
|
||||
const response = await this.fetch(`scene?${new URLSearchParams({ view: viewId, key, variant: String(variant) })}`, { signal })
|
||||
async scene(viewId: string, place: ScenePlace, signal?: AbortSignal): Promise<BlockScene> {
|
||||
const response = await this.fetch(`scene?${sceneQuery(viewId, place)}`, { signal })
|
||||
return decodeScene(await response.arrayBuffer())
|
||||
}
|
||||
|
||||
/** The part that placed each block of {@link scene}, in the same order, or NO_PART; baked again on the server to find out. */
|
||||
async sceneParts(viewId: string, key: string, variant: number, signal?: AbortSignal): Promise<Int32Array> {
|
||||
const response = await this.fetch(`scene-parts?${new URLSearchParams({ view: viewId, key, variant: String(variant) })}`, { signal })
|
||||
async sceneParts(viewId: string, place: ScenePlace, signal?: AbortSignal): Promise<Int32Array> {
|
||||
const response = await this.fetch(`scene-parts?${sceneQuery(viewId, place)}`, { signal })
|
||||
return new Int32Array(await response.arrayBuffer())
|
||||
}
|
||||
|
||||
/**
|
||||
* The caves of the world's generated chunks over a square {@code size} blocks across around a spot: every air block
|
||||
* under the terrain, drawn as blocks coloured by height. Every chunk of the square is generated, which takes seconds.
|
||||
*/
|
||||
async caves(centerX: number, centerZ: number, size: number, signal: AbortSignal): Promise<BlockScene> {
|
||||
const query = new URLSearchParams({ x: String(Math.round(centerX)), z: String(Math.round(centerZ)), size: String(size) })
|
||||
this.cancelOnAbort(query, signal)
|
||||
const response = await this.fetch(`caves?${query}`, { signal })
|
||||
return decodeScene(await response.arrayBuffer())
|
||||
}
|
||||
|
||||
/** The pattern views the pack's modules show, such as its palettes. */
|
||||
patternViews(): Promise<ModuleViewInfo[]> {
|
||||
return this.json('patterns')
|
||||
@@ -334,6 +352,14 @@ export class BridgeClient {
|
||||
}
|
||||
}
|
||||
|
||||
function sceneQuery(viewId: string, place: ScenePlace): URLSearchParams {
|
||||
const query = new URLSearchParams({ view: viewId, key: place.key, variant: String(place.variant) })
|
||||
if (place.size !== null) {
|
||||
query.set('size', String(place.size))
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
function layerQuery(layer: MapLayerQuery, rest: Record<string, string>): URLSearchParams {
|
||||
const query = new URLSearchParams({ view: layer.view, ...rest })
|
||||
if (layer.preview !== null) {
|
||||
|
||||
@@ -40,8 +40,18 @@ export interface SceneEntry {
|
||||
export interface SceneViewInfo extends ModuleViewInfo {
|
||||
editable: boolean
|
||||
creatable: boolean
|
||||
/** Whether an editable view's files show as nodes under the scene; else as one node's settings beside it. */
|
||||
nodes: boolean
|
||||
baked: boolean
|
||||
parts: boolean
|
||||
/** The sizes the scenes can be built at, or null when each entry has its own. */
|
||||
sizes: SceneSizes | null
|
||||
}
|
||||
|
||||
/** The sizes a scene view builds its scenes at, as blocks across, smallest first, and the one shown first (IrisApi SceneSizes). */
|
||||
export interface SceneSizes {
|
||||
blocksAcross: number[]
|
||||
opening: number
|
||||
}
|
||||
|
||||
/** One timed bake of a scene entry (IrisApi BakeRun): a time per step, added up over every variant. */
|
||||
|
||||
@@ -21,7 +21,7 @@ import { EntryBrowser } from './EntryBrowser'
|
||||
import { useEntryFiles } from './EntryFileDialogs'
|
||||
import { NodeInspector } from './NodeInspector'
|
||||
import { NodePane } from './NodePane'
|
||||
import { Panel, SidebarPanel, Stepper, ViewIsland } from './Sidebar'
|
||||
import { Panel, SidebarPanel, SizeChoice, Stepper, ViewIsland } from './Sidebar'
|
||||
import { SceneViewport, type PickedSurface } from './SceneViewport'
|
||||
|
||||
const NUMBER = new Intl.NumberFormat('en-US')
|
||||
@@ -54,6 +54,8 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce
|
||||
const entryKeys = useMemo(() => entries.map((candidate) => candidate.key), [entries])
|
||||
const files = useEntryFiles(client, view.id, entryKeys, (key) => setChosen({ key, variant: 0 }), tell)
|
||||
const [farLeavesOnly, setFarLeavesOnly] = useState(false)
|
||||
const [chosenSize, setChosenSize] = useState<{ viewId: string; size: number } | null>(null)
|
||||
const size = chosenSize?.viewId === view.id ? chosenSize.size : (view.sizes?.opening ?? null)
|
||||
const chooseVariant = (next: number): void => {
|
||||
if (entry !== null) {
|
||||
setChosen({ key: entry.key, variant: (next + entry.variants) % entry.variants })
|
||||
@@ -63,14 +65,16 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce
|
||||
const bakeTiming = useBakeTiming(client, view.id, entry?.key ?? null, view.baked, packRevision, tell)
|
||||
|
||||
const editor = useNodeEditor(client, view.id, view.editable ? (entry?.key ?? null) : null, packRevision, tell)
|
||||
const editsAsForm = view.editable && !view.nodes
|
||||
const [selectedPath, setSelectedPath] = useState<JsonPath | null>(null)
|
||||
const selected = editor.state?.roots != null && selectedPath !== null ? nodeWithPath(editor.state.roots, selectedPath) : null
|
||||
const roots = editor.state?.roots ?? null
|
||||
const selected = roots === null ? null : editsAsForm ? (roots[0] ?? null) : selectedPath !== null ? nodeWithPath(roots, selectedPath) : null
|
||||
const values = useMemo(() => (editor.state === null ? undefined : parsedText(editor.state.text)), [editor.state])
|
||||
const suggestions = useMemo(() => new SuggestionStore((of) => client.suggestions(of)), [client, packRevision])
|
||||
|
||||
const [pickedPart, setPickedPart] = useState(NO_PART)
|
||||
const highlightedPart = view.parts ? (selected?.partId ?? pickedPart) : NO_PART
|
||||
const { scene, shown, sceneParts, building } = useSceneMesh(client, view, entry, variant, packRevision, models, images, farLeavesOnly, highlightedPart, tell)
|
||||
const { scene, shown, sceneParts, building } = useSceneMesh(client, view, entry, variant, size, packRevision, models, images, farLeavesOnly, highlightedPart, tell)
|
||||
const shownScene = shown?.scene ?? null
|
||||
const blockIndex = useMemo(() => (shownScene === null ? null : new BlockIndex(shownScene)), [shownScene])
|
||||
const shownParts = partsOf(sceneParts, shownScene)
|
||||
@@ -121,12 +125,15 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce
|
||||
title={view.label}
|
||||
entry={entry}
|
||||
variant={variant}
|
||||
sizes={view.sizes?.blocksAcross ?? null}
|
||||
size={size}
|
||||
shown={shown}
|
||||
status={building && entry !== null ? (waiting ?? `Building ${entry.name}`) : null}
|
||||
onVariant={chooseVariant}
|
||||
onSize={(next) => setChosenSize({ viewId: view.id, size: next })}
|
||||
onPick={view.parts ? pickPart : undefined}
|
||||
/>
|
||||
{view.editable && entry !== null && (
|
||||
{view.editable && !editsAsForm && entry !== null && (
|
||||
<NodePane
|
||||
editor={editor}
|
||||
entryKey={entry.key}
|
||||
@@ -155,30 +162,36 @@ interface SceneAreaProps {
|
||||
title: string
|
||||
entry: SceneEntry | null
|
||||
variant: number
|
||||
/** The sizes the scenes can be built at, or null when each entry has its own. */
|
||||
sizes: readonly number[] | null
|
||||
size: number | null
|
||||
shown: ShownScene | null
|
||||
/** What is being built, or null when nothing is. */
|
||||
status: string | null
|
||||
onVariant(variant: number): void
|
||||
onSize(size: number): void
|
||||
/** Called when a block is clicked; absent when the scene has no parts to pick. */
|
||||
onPick?(picked: PickedSurface): void
|
||||
}
|
||||
|
||||
function SceneArea({ title, entry, variant, shown, status, onVariant, onPick }: SceneAreaProps): React.JSX.Element {
|
||||
function SceneArea({ title, entry, variant, sizes, size, shown, status, onVariant, onSize, onPick }: SceneAreaProps): React.JSX.Element {
|
||||
return (
|
||||
<ViewIsland
|
||||
className="flex-[11]"
|
||||
title={entry === null ? title : `${title} · ${entry.name}`}
|
||||
tools={
|
||||
entry !== null &&
|
||||
entry.variants > 1 && (
|
||||
<Stepper
|
||||
label={`Variant ${variant + 1} of ${entry.variants}`}
|
||||
backHint="Previous variant"
|
||||
onHint="Next variant"
|
||||
onBack={() => onVariant(variant - 1)}
|
||||
onOn={() => onVariant(variant + 1)}
|
||||
/>
|
||||
)
|
||||
<>
|
||||
{sizes !== null && size !== null && <SizeChoice sizes={sizes} size={size} onSize={onSize} />}
|
||||
{entry !== null && entry.variants > 1 && (
|
||||
<Stepper
|
||||
label={`Variant ${variant + 1} of ${entry.variants}`}
|
||||
backHint="Previous variant"
|
||||
onHint="Next variant"
|
||||
onBack={() => onVariant(variant - 1)}
|
||||
onOn={() => onVariant(variant + 1)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
>
|
||||
<SceneViewport content={shown?.mesh.content ?? null} framing={shown?.scene} onPick={onPick} />
|
||||
@@ -247,6 +260,7 @@ function useSceneMesh(
|
||||
view: SceneViewInfo,
|
||||
entry: SceneEntry | null,
|
||||
variant: number,
|
||||
size: number | null,
|
||||
packRevision: number,
|
||||
models: BlockModels,
|
||||
images: BridgeAssets,
|
||||
@@ -268,9 +282,10 @@ function useSceneMesh(
|
||||
return
|
||||
}
|
||||
const stop = new AbortController()
|
||||
const place = { key, variant, size }
|
||||
setBuilding(true)
|
||||
client
|
||||
.scene(viewId, key, variant, stop.signal)
|
||||
.scene(viewId, place, stop.signal)
|
||||
.then((found) => {
|
||||
if (stop.signal.aborted) {
|
||||
return
|
||||
@@ -278,7 +293,7 @@ function useSceneMesh(
|
||||
setScene(found)
|
||||
if (hasParts) {
|
||||
client
|
||||
.sceneParts(viewId, key, variant, stop.signal)
|
||||
.sceneParts(viewId, place, stop.signal)
|
||||
.then((parts) => !stop.signal.aborted && setSceneParts({ scene: found, parts }))
|
||||
.catch((failure: Error) => !stop.signal.aborted && tell(failure.message))
|
||||
}
|
||||
@@ -291,7 +306,7 @@ function useSceneMesh(
|
||||
setBuilding(false)
|
||||
})
|
||||
return () => stop.abort()
|
||||
}, [client, viewId, hasParts, key, variant, packRevision, tell])
|
||||
}, [client, viewId, hasParts, key, variant, size, packRevision, tell])
|
||||
|
||||
const faint = useMemo(() => {
|
||||
const parts = partsOf(sceneParts, scene)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createContext, useContext } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { IdeIcon, type IdeIconName } from '@/components/IdeIcon'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
/** Where a view puts its own lists: under the views in the left island, as in v3e. */
|
||||
@@ -44,6 +45,26 @@ export function Stepper({ label, backHint, onHint, onBack, onOn }: StepperProps)
|
||||
)
|
||||
}
|
||||
|
||||
interface SizeChoiceProps {
|
||||
/** Each size, as blocks across, smallest first. */
|
||||
sizes: readonly number[]
|
||||
size: number
|
||||
onSize(size: number): void
|
||||
}
|
||||
|
||||
/** Chooses how many blocks across an area is, from an island's header. */
|
||||
export function SizeChoice({ sizes, size, onSize }: SizeChoiceProps): React.JSX.Element {
|
||||
return (
|
||||
<ToggleGroup type="single" size="sm" className="gap-0.5" value={String(size)} onValueChange={(value) => value !== '' && onSize(Number(value))}>
|
||||
{sizes.map((choice) => (
|
||||
<ToggleGroupItem key={choice} value={String(choice)} className={cn(HEADER_CHOICE, 'tabular-nums')} title={`${choice} × ${choice} blocks`}>
|
||||
{choice}
|
||||
</ToggleGroupItem>
|
||||
))}
|
||||
</ToggleGroup>
|
||||
)
|
||||
}
|
||||
|
||||
interface ViewIslandProps {
|
||||
title: string
|
||||
/** The view's own buttons, at the right of its header. */
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Toggle } from '@/components/ui/toggle'
|
||||
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
|
||||
import type { BridgeClient, MapLayerQuery, WorldRect } from '../bridge/BridgeClient'
|
||||
import type { MapTile, WorldInfo } from '../bridge/types'
|
||||
@@ -16,8 +17,9 @@ import { PickInspector } from './PickInspector'
|
||||
import { SceneViewport } from './SceneViewport'
|
||||
import { usePickInspection } from './usePickInspection'
|
||||
import { useSpotActions } from './useSpotActions'
|
||||
import { HEADER_CHOICE, Panel, ViewIsland } from './Sidebar'
|
||||
import { HEADER_CHOICE, Panel, SizeChoice, ViewIsland } from './Sidebar'
|
||||
import { useGroundInView, useLongPending, useTerrainStream } from './useTerrainStream'
|
||||
import { caveBandShares, useWorldCaves, type CaveArea } from './useWorldCaves'
|
||||
|
||||
const NUMBER = new Intl.NumberFormat('en-US')
|
||||
// The map's coarsest tiles are 4096 blocks a pixel, which is level 12.
|
||||
@@ -27,6 +29,9 @@ const BLOCKS_BELOW_FLUID_IN_FOCUS = 32
|
||||
// Every level carries blocks, out to the horizon: a coarse tile generates a chunk for each of its columns, a minute or
|
||||
// so of the server's time, and the ground already sampled stays in sight meanwhile.
|
||||
const MOST_BLOCKS_PER_COLUMN = 2 ** COARSEST_LEVEL
|
||||
// Every chunk of the square is generated: 256 blocks across is 256 chunks, seconds of the server's time.
|
||||
const CAVE_SIZES = [128, 256, 512]
|
||||
const OPENING_CAVE_SIZE = 256
|
||||
|
||||
// Tiles with generated blocks take seconds to minutes each. The same tiles without them stand in, but only on ground
|
||||
// where no tile with blocks of any level is known yet.
|
||||
@@ -88,42 +93,76 @@ export function WorldStudio3D({ title, client, world, mapArea, packRevision, onS
|
||||
const area = useAreaOf(ground)
|
||||
const inspection = usePickInspection(client, pickLayer, area, inspectsPicks, packRevision)
|
||||
const spotActions = useSpotActions(client, tell, true)
|
||||
const [showsCaves, setShowsCaves] = useState(false)
|
||||
const [caveArea, setCaveArea] = useState<CaveArea | null>(null)
|
||||
const caves = useWorldCaves(client, caveArea, packRevision, tell)
|
||||
const caveShares = useMemo(() => (caves.scene === null ? [] : caveBandShares(caves.scene)), [caves.scene])
|
||||
// The caves are generated around the middle of the ground in view when they are shown, or at another size there.
|
||||
const showCavesAt = (size: number): void => {
|
||||
const middle = area === null ? centre : { x: Math.round((area.minX + area.maxX) / 2), z: Math.round((area.minZ + area.maxZ) / 2) }
|
||||
setCaveArea((known) => (known !== null && known.centerX === middle.x && known.centerZ === middle.z && known.size === size ? known : { centerX: middle.x, centerZ: middle.z, size }))
|
||||
setShowsCaves(true)
|
||||
}
|
||||
|
||||
const notice = showsCaves ? caves.status : sampling ? 'Sampling the terrain' : null
|
||||
|
||||
useEffect(() => {
|
||||
onSummary(`Around ${NUMBER.format(centre.x)}, ${NUMBER.format(centre.z)}`)
|
||||
const around = showsCaves && caveArea !== null ? { x: caveArea.centerX, z: caveArea.centerZ } : centre
|
||||
onSummary(`${showsCaves ? 'Caves around' : 'Around'} ${NUMBER.format(around.x)}, ${NUMBER.format(around.z)}`)
|
||||
return () => onSummary(null)
|
||||
}, [centre, onSummary])
|
||||
}, [centre, showsCaves, caveArea, onSummary])
|
||||
|
||||
return (
|
||||
<>
|
||||
<ViewIsland
|
||||
title={title}
|
||||
tools={
|
||||
<ToggleGroup type="single" size="sm" className="gap-0.5" value={coloring} onValueChange={(value) => value !== '' && setColoring(value as MapTerrainColoring)}>
|
||||
<ToggleGroupItem value="biomes" className={HEADER_CHOICE}>
|
||||
Biomes
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="regions" className={HEADER_CHOICE}>
|
||||
Regions
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="height" className={HEADER_CHOICE}>
|
||||
Height
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="blocks" className={HEADER_CHOICE} title="The blocks the world generates">
|
||||
Blocks
|
||||
</ToggleGroupItem>
|
||||
</ToggleGroup>
|
||||
<>
|
||||
{showsCaves ? (
|
||||
<SizeChoice sizes={CAVE_SIZES} size={caveArea?.size ?? OPENING_CAVE_SIZE} onSize={showCavesAt} />
|
||||
) : (
|
||||
<ToggleGroup type="single" size="sm" className="gap-0.5" value={coloring} onValueChange={(value) => value !== '' && setColoring(value as MapTerrainColoring)}>
|
||||
<ToggleGroupItem value="biomes" className={HEADER_CHOICE}>
|
||||
Biomes
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="regions" className={HEADER_CHOICE}>
|
||||
Regions
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="height" className={HEADER_CHOICE}>
|
||||
Height
|
||||
</ToggleGroupItem>
|
||||
<ToggleGroupItem value="blocks" className={HEADER_CHOICE} title="The blocks the world generates">
|
||||
Blocks
|
||||
</ToggleGroupItem>
|
||||
</ToggleGroup>
|
||||
)}
|
||||
<Toggle
|
||||
size="sm"
|
||||
className={HEADER_CHOICE}
|
||||
pressed={showsCaves}
|
||||
title="The caves of the generated chunks around the middle of the view, coloured by height"
|
||||
onPressedChange={(pressed) => (pressed ? showCavesAt(caveArea?.size ?? OPENING_CAVE_SIZE) : setShowsCaves(false))}
|
||||
>
|
||||
Caves
|
||||
</Toggle>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<SceneViewport content={content} framing={content} spotActions={spotActions} />
|
||||
{sampling && (
|
||||
{showsCaves ? (
|
||||
<SceneViewport content={caves.mesh?.content ?? null} framing={caves.scene} />
|
||||
) : (
|
||||
<SceneViewport content={content} framing={content} spotActions={spotActions} />
|
||||
)}
|
||||
{notice !== null && (
|
||||
<div className="pointer-events-none absolute top-3 left-1/2 -translate-x-1/2 rounded-md border border-overlay-border bg-card px-3 py-1.5 text-[12px] text-secondary-text">
|
||||
Sampling the terrain
|
||||
{notice}
|
||||
</div>
|
||||
)}
|
||||
</ViewIsland>
|
||||
<Panel className="w-[250px]">
|
||||
{inspectsPicks ? (
|
||||
{showsCaves ? (
|
||||
<Legend title="Caves by height" shares={caveShares} emptyText={caves.status === null ? 'No caves here' : 'Generating the caves…'} className="flex-1" />
|
||||
) : inspectsPicks ? (
|
||||
<PickInspector title={coloring === 'regions' ? 'Regions' : 'Biomes'} inspection={inspection} />
|
||||
) : (
|
||||
<Legend title="Heights in view" shares={heightShares} emptyText="Looking at the ground…" className="flex-1" />
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import type { BridgeClient } from '../bridge/BridgeClient'
|
||||
import type { Share } from '../map/census'
|
||||
import { BlockModels } from '../scene/blockModels'
|
||||
import { BridgeAssets } from '../scene/BridgeAssets'
|
||||
import { INTS_PER_BLOCK, type BlockScene } from '../scene/sceneDecoding'
|
||||
import { buildSceneMesh, type SceneMesh } from '../scene/sceneMesh'
|
||||
|
||||
/** Where the caves of the world are shown: a square of blocks around a spot. */
|
||||
export interface CaveArea {
|
||||
centerX: number
|
||||
centerZ: number
|
||||
size: number
|
||||
}
|
||||
|
||||
export interface WorldCaves {
|
||||
scene: BlockScene | null
|
||||
mesh: SceneMesh | null
|
||||
/** What the server is doing while the caves are on their way, or null once they are in. */
|
||||
status: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* The colour of each band of heights the caves are drawn in, from the highest down, as the server colours them
|
||||
* (Iris core CaveSpace).
|
||||
*/
|
||||
const CAVE_BANDS: readonly { block: string; name: string; color: number }[] = [
|
||||
{ block: 'minecraft:yellow_concrete', name: 'Y 64 and up', color: 0xf1af15 },
|
||||
{ block: 'minecraft:orange_concrete', name: 'Y 32 to 63', color: 0xe06101 },
|
||||
{ block: 'minecraft:red_concrete', name: 'Y 0 to 31', color: 0x8e2121 },
|
||||
{ block: 'minecraft:magenta_concrete', name: 'Y −32 to −1', color: 0xa9309f },
|
||||
{ block: 'minecraft:purple_concrete', name: 'Y −64 to −33', color: 0x64209c },
|
||||
{ block: 'minecraft:blue_concrete', name: 'Below Y −64', color: 0x2c2e8f }
|
||||
]
|
||||
|
||||
/** How much of the caves' outside lies in each band of heights, highest first; bands without caves are left out. */
|
||||
export function caveBandShares(scene: BlockScene): Share[] {
|
||||
const counts = new Map<number, number>()
|
||||
for (let offset = 3; offset < scene.blocks.length; offset += INTS_PER_BLOCK) {
|
||||
const paletteIndex = scene.blocks[offset]
|
||||
counts.set(paletteIndex, (counts.get(paletteIndex) ?? 0) + 1)
|
||||
}
|
||||
const blocks = scene.blocks.length / INTS_PER_BLOCK
|
||||
return CAVE_BANDS.flatMap((band) => {
|
||||
const count = counts.get(scene.palette.indexOf(band.block)) ?? 0
|
||||
return count === 0 ? [] : [{ subject: { kind: 'HEIGHT' as const, key: band.block, name: band.name }, color: band.color, fraction: count / blocks }]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* The caves of the world's generated chunks in {@code area}, asked for again when it or the pack changes; null asks
|
||||
* for none. A newer area abandons an older one, on the server too, and a replaced mesh is freed.
|
||||
*/
|
||||
export function useWorldCaves(client: BridgeClient, area: CaveArea | null, packRevision: number, tell: (message: string) => void): WorldCaves {
|
||||
const [waiting, setWaiting] = useState<string | null>(null)
|
||||
const images = useMemo(() => new BridgeAssets(client, setWaiting), [client])
|
||||
const models = useMemo(() => new BlockModels(images), [images])
|
||||
const [shown, setShown] = useState<{ scene: BlockScene; mesh: SceneMesh } | null>(null)
|
||||
const [building, setBuilding] = useState(false)
|
||||
const centerX = area?.centerX
|
||||
const centerZ = area?.centerZ
|
||||
const size = area?.size
|
||||
|
||||
useEffect(() => {
|
||||
if (centerX === undefined || centerZ === undefined || size === undefined) {
|
||||
return
|
||||
}
|
||||
const stop = new AbortController()
|
||||
setBuilding(true)
|
||||
client
|
||||
.caves(centerX, centerZ, size, stop.signal)
|
||||
.then((scene) => buildSceneMesh(scene, models, images).then((mesh) => ({ scene, mesh })))
|
||||
.then((built) => {
|
||||
if (stop.signal.aborted) {
|
||||
built.mesh.dispose()
|
||||
return
|
||||
}
|
||||
setShown(built)
|
||||
})
|
||||
.catch((failure: Error) => !stop.signal.aborted && tell(failure.message))
|
||||
.finally(() => !stop.signal.aborted && setBuilding(false))
|
||||
return () => stop.abort()
|
||||
}, [client, centerX, centerZ, size, packRevision, models, images, tell])
|
||||
|
||||
useEffect(() => () => shown?.mesh.dispose(), [shown])
|
||||
|
||||
return {
|
||||
scene: shown?.scene ?? null,
|
||||
mesh: shown?.mesh ?? null,
|
||||
status: building ? (waiting ?? `Generating the caves of ${size} × ${size} blocks`) : null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user