diff --git a/src/app/components/previews/NoiseSettingsPreview.tsx b/src/app/components/previews/NoiseSettingsPreview.tsx index 84eb67dd..490a6519 100644 --- a/src/app/components/previews/NoiseSettingsPreview.tsx +++ b/src/app/components/previews/NoiseSettingsPreview.tsx @@ -1,10 +1,13 @@ import { useEffect, useMemo, useRef, useState } from 'preact/hooks' import { useLocale, useProject } from '../../contexts/index.js' import { useCanvas } from '../../hooks/index.js' -import { getNoiseBlock, noiseSettings } from '../../previews/index.js' +import type { ColormapType } from '../../previews/Colormap.js' +import { densityFunction, getNoiseBlock, noiseSettings } from '../../previews/index.js' import { CachedCollections, checkVersion } from '../../services/index.js' +import { Store } from '../../Store.js' import { randomSeed } from '../../Utils.js' import { Btn, BtnInput, BtnMenu } from '../index.js' +import { ColormapSelector } from './ColormapSelector.jsx' import type { PreviewProps } from './index.js' export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => { @@ -15,7 +18,9 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => const [biomeScale, setBiomeScale] = useState(0.2) const [biomeDepth, setBiomeDepth] = useState(0.1) const [autoScroll, setAutoScroll] = useState(false) - const [focused, setFocused] = useState(undefined) + const [focused, setFocused] = useState([]) + const [layer, setLayer] = useState('terrain') + const [colormap, setColormap] = useState(Store.getColormap() ?? 'viridis') const offset = useRef(0) const scrollInterval = useRef(undefined) const state = JSON.stringify([data, biomeScale, biomeDepth]) @@ -26,8 +31,13 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => return [size, size] }, async draw(img) { - const options = { biome, biomeDepth, biomeScale, offset: offset.current, width: img.width, seed, version, project } - await noiseSettings(data, img, options) + const options = { biome, biomeDepth, biomeScale, offset: offset.current, width: img.width, seed, version, project, colormap, minY: data?.noise?.min_y ?? 0, height: data?.noise?.height ?? 256, hardZero: true } + if (layer === 'final_density') { + const df = data?.noise_router?.final_density ?? 0 + await densityFunction(df, img, options) + } else { + await noiseSettings(data, img, options) + } }, async onDrag(dx) { offset.current += dx * size @@ -37,12 +47,12 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => const worldX = Math.floor(x * size - offset.current) const worldY = size - Math.max(1, Math.ceil(y * size)) + (data?.noise?.min_y ?? 0) const block = getNoiseBlock(worldX, worldY) - setFocused(block ? `Y=${worldY} (${block.getName().path})` : `Y=${worldY}`) + setFocused([block ? `Y=${worldY} (${block.getName().path})` : `Y=${worldY}`]) }, onLeave() { - setFocused(undefined) + setFocused([]) }, - }, [state, seed, project]) + }, [version, state, seed, project, shown, biome, biomeScale, biomeDepth, layer, colormap]) useEffect(() => { if (scrollInterval.current) { @@ -63,13 +73,14 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => } })() } - }, [version, state, seed, project, shown, biome, biomeScale, biomeDepth, autoScroll]) + }, [version, state, seed, project, shown, biome, biomeScale, biomeDepth, autoScroll, layer, colormap]) const allBiomes = useMemo(() => CachedCollections?.get('worldgen/biome') ?? [], [version]) return <>
- {focused && } + {focused.map(s => )} + {layer === 'final_density' && } {checkVersion(version, undefined, '1.17') ? <> setBiomeScale(Number(v))} /> @@ -78,6 +89,7 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => } setAutoScroll(!autoScroll)} /> + setLayer(layer === 'final_density' ? 'terrain' : 'final_density')} /> setSeed(randomSeed())} /> diff --git a/src/app/previews/NoiseSettings.ts b/src/app/previews/NoiseSettings.ts index 16b20465..027db613 100644 --- a/src/app/previews/NoiseSettings.ts +++ b/src/app/previews/NoiseSettings.ts @@ -20,6 +20,7 @@ export type NoiseSettingsOptions = { minY?: number, height?: number, colormap?: ColormapType, + hardZero?: boolean, } const colors: Record = { @@ -93,20 +94,21 @@ export async function densityFunction(state: any, img: ImageData, options: Noise const arr = Array(options.width * noise.height) let limit = 0.01 for (let x = 0; x < options.width; x += 1) { - for (let y = 0; y < noise.height - noise.minY; y += 1) { + for (let y = 0; y < noise.height; y += 1) { const i = x + y * options.width - const density = fn.compute(DensityFunction.context(x - options.offset, noise.height - y - 1, 0)) + const density = fn.compute(DensityFunction.context(x - options.offset, noise.height - y - 1 + noise.minY, 0)) limit = Math.max(limit, Math.min(1, Math.abs(density))) arr[i] = density } } const colormap = getColormap(options.colormap ?? 'viridis') + const colorPicker = options.hardZero ? (t: number) => colormap(t <= 0.5 ? t - 0.08 : t + 0.08) : colormap const min = -limit const max = limit const data = img.data for (let i = 0; i < options.width * noise.height; i += 1) { - const color = colormap(clampedMap(arr[i], min, max, 1, 0)) + const color = colorPicker(clampedMap(arr[i], min, max, 1, 0)) data[4 * i] = color[0] * 256 data[4 * i + 1] = color[1] * 256 data[4 * i + 2] = color[2] * 256 diff --git a/src/locales/en.json b/src/locales/en.json index 814a93ea..30d2ef86 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -141,6 +141,7 @@ "preview.width": "Width", "preview.height": "Height", "preview.min_y": "Min Y", + "preview.final_density": "Show final density", "project.new": "New project", "project.cancel": "Cancel", "project.create": "Create a new project",