Optimize renderer
This commit is contained in:
+36
-9
@@ -1,5 +1,10 @@
|
||||
import { Alea, create_noise_2d, create_noise_3d, NoiseFunction2D } from "@paulaboks/rng";
|
||||
import { CHUNK_SIZE, Dimension } from "./components/dimension.ts";
|
||||
import { Alea, create_noise_2d, create_noise_3d, NoiseFunction2D, NoiseFunction3D } from "@paulaboks/rng";
|
||||
import { CHUNK_SIZE } from "$/common/constants.ts";
|
||||
|
||||
// generation runs in a worker now, so it only needs somewhere to put blocks
|
||||
export interface BlockSink {
|
||||
add_block(block: { x: number; y: number; z: number; id: string }): void;
|
||||
}
|
||||
|
||||
type Biome =
|
||||
| "desert"
|
||||
@@ -129,7 +134,7 @@ function can_place_tree(tree_map: boolean[][], local_x: number, local_z: number)
|
||||
return true;
|
||||
}
|
||||
|
||||
function place_tree(dimension: Dimension, rng: Alea, x: number, y: number, z: number, biome: Biome) {
|
||||
function place_tree(dimension: BlockSink, rng: Alea, x: number, y: number, z: number, biome: Biome) {
|
||||
const height = Math.floor(rng.next() * 3) + (biome === "jungle" ? 8 : 4);
|
||||
const trunk_block = "bworld:log";
|
||||
const leaves_block = "bworld:leaves";
|
||||
@@ -171,12 +176,34 @@ function should_place_tree(feature_noise: NoiseFunction2D, biome: Biome, x: numb
|
||||
return n > (TREE_THRESHOLD[biome] ?? 0.8);
|
||||
}
|
||||
|
||||
export function generate_chunk(dimension: Dimension, cx: number, cz: number, seed = "seed") {
|
||||
const height_noise = create_noise_2d(new Alea(seed + "_height"));
|
||||
const temp_noise = create_noise_2d(new Alea(seed + "_temp"));
|
||||
const moisture_noise = create_noise_2d(new Alea(seed + "_moisture"));
|
||||
const feature_noise = create_noise_2d(new Alea(seed + "_feature"));
|
||||
const ore_noises = ORES.map((ore) => create_noise_3d(new Alea(seed + "_" + ore.id)));
|
||||
interface SeedNoises {
|
||||
height_noise: NoiseFunction2D;
|
||||
temp_noise: NoiseFunction2D;
|
||||
moisture_noise: NoiseFunction2D;
|
||||
feature_noise: NoiseFunction2D;
|
||||
ore_noises: NoiseFunction3D[];
|
||||
}
|
||||
|
||||
// building the permutation tables is expensive, only do it once per seed
|
||||
const noise_cache = new Map<string, SeedNoises>();
|
||||
|
||||
function get_noises(seed: string): SeedNoises {
|
||||
let noises = noise_cache.get(seed);
|
||||
if (!noises) {
|
||||
noises = {
|
||||
height_noise: create_noise_2d(new Alea(seed + "_height")),
|
||||
temp_noise: create_noise_2d(new Alea(seed + "_temp")),
|
||||
moisture_noise: create_noise_2d(new Alea(seed + "_moisture")),
|
||||
feature_noise: create_noise_2d(new Alea(seed + "_feature")),
|
||||
ore_noises: ORES.map((ore) => create_noise_3d(new Alea(seed + "_" + ore.id))),
|
||||
};
|
||||
noise_cache.set(seed, noises);
|
||||
}
|
||||
return noises;
|
||||
}
|
||||
|
||||
export function generate_chunk(dimension: BlockSink, cx: number, cz: number, seed = "seed") {
|
||||
const { height_noise, temp_noise, moisture_noise, feature_noise, ore_noises } = get_noises(seed);
|
||||
// seeded per chunk so every client generates the exact same terrain
|
||||
const rng = new Alea(`${seed}_chunk_${cx}_${cz}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user