@jgengine/core 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/game/defineGame.d.ts +3 -0
- package/dist/game/playableGame.d.ts +10 -1
- package/dist/meta/changelog.d.ts +1 -1
- package/dist/meta/changelog.js +19 -1
- package/dist/physics/physicsWorld.d.ts +136 -0
- package/dist/physics/physicsWorld.js +489 -0
- package/dist/runtime/gameContext.d.ts +2 -0
- package/dist/runtime/gameContext.js +3 -0
- package/dist/scene/assetCatalog.d.ts +13 -0
- package/dist/scene/entityStore.d.ts +6 -0
- package/dist/scene/entityStore.js +15 -1
- package/dist/time/simClock.d.ts +62 -0
- package/dist/time/simClock.js +132 -0
- package/dist/world/buildingIndex.d.ts +16 -0
- package/dist/world/buildingIndex.js +63 -0
- package/dist/world/buildings.d.ts +98 -0
- package/dist/world/buildings.js +320 -0
- package/dist/world/features.d.ts +105 -1
- package/dist/world/features.js +95 -0
- package/dist/world/regions.d.ts +66 -0
- package/dist/world/regions.js +158 -0
- package/dist/world/scatter.d.ts +24 -0
- package/dist/world/scatter.js +126 -0
- package/dist/world/scatterItems.d.ts +38 -0
- package/dist/world/scatterItems.js +85 -0
- package/dist/world/terrain.d.ts +54 -0
- package/dist/world/terrain.js +181 -0
- package/dist/world/water.d.ts +27 -0
- package/dist/world/water.js +125 -0
- package/dist/world/wind.d.ts +17 -0
- package/dist/world/wind.js +40 -0
- package/package.json +1 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
function hash(ix, iz, salt) {
|
|
2
|
+
let h = salt | 0;
|
|
3
|
+
h = Math.imul(h ^ (ix | 0), 0x27d4eb2d);
|
|
4
|
+
h = Math.imul(h ^ (iz | 0), 0x85ebca6b);
|
|
5
|
+
h ^= h >>> 15;
|
|
6
|
+
h = Math.imul(h, 0x2c1b3c6d);
|
|
7
|
+
h ^= h >>> 12;
|
|
8
|
+
return (h >>> 0) / 4294967295;
|
|
9
|
+
}
|
|
10
|
+
const DEFAULT_MAX_SLOPE = 0.55;
|
|
11
|
+
/**
|
|
12
|
+
* Deterministically place opaque items across `area`, grounded on a region field.
|
|
13
|
+
* For each grid cell it asks `layersFor` which items may appear in that region and
|
|
14
|
+
* rolls one against their densities. The engine never interprets `item` — a game
|
|
15
|
+
* maps it to a mesh or entity. Content scatter (region-driven density) as opposed to
|
|
16
|
+
* `scatter` in `./scatter`, which is renderer-free geometric point distribution.
|
|
17
|
+
*/
|
|
18
|
+
export function scatterItems(field, area, layersFor, options = {}) {
|
|
19
|
+
const cell = options.cell ?? 5;
|
|
20
|
+
const max = options.max ?? 1400;
|
|
21
|
+
const salt = field.seed ^ (options.saltKey ?? 0x5f3a);
|
|
22
|
+
const out = [];
|
|
23
|
+
for (let gx = Math.floor(area.minX / cell); gx <= Math.ceil(area.maxX / cell); gx += 1) {
|
|
24
|
+
for (let gz = Math.floor(area.minZ / cell); gz <= Math.ceil(area.maxZ / cell); gz += 1) {
|
|
25
|
+
const roll = hash(gx, gz, salt);
|
|
26
|
+
const x = (gx + hash(gx, gz, salt ^ 0x11)) * cell;
|
|
27
|
+
const z = (gz + hash(gx, gz, salt ^ 0x22)) * cell;
|
|
28
|
+
const y = field.sampleHeight(x, z);
|
|
29
|
+
const sample = field.sampleRegion(x, z);
|
|
30
|
+
const layers = layersFor(sample);
|
|
31
|
+
if (layers.length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
let total = 0;
|
|
34
|
+
for (const layer of layers)
|
|
35
|
+
total += layer.density;
|
|
36
|
+
if (roll > total)
|
|
37
|
+
continue;
|
|
38
|
+
let acc = 0;
|
|
39
|
+
let chosen = layers[0];
|
|
40
|
+
for (const layer of layers) {
|
|
41
|
+
acc += layer.density;
|
|
42
|
+
if (roll <= acc) {
|
|
43
|
+
chosen = layer;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if ((chosen.aboveWaterOnly ?? true) && y < field.seaLevel + 0.1)
|
|
48
|
+
continue;
|
|
49
|
+
if (1 - field.sampleNormal(x, z)[1] > (chosen.maxSlope ?? DEFAULT_MAX_SLOPE))
|
|
50
|
+
continue;
|
|
51
|
+
const scaleRoll = hash(gx, gz, salt ^ 0x99);
|
|
52
|
+
const min = chosen.minScale ?? 0.8;
|
|
53
|
+
const maxScale = chosen.maxScale ?? 1.2;
|
|
54
|
+
out.push({
|
|
55
|
+
id: `${gx}:${gz}`,
|
|
56
|
+
item: chosen.item,
|
|
57
|
+
x,
|
|
58
|
+
z,
|
|
59
|
+
y,
|
|
60
|
+
scale: min + scaleRoll * (maxScale - min),
|
|
61
|
+
rotation: hash(gx, gz, salt ^ 0xabcd) * Math.PI * 2,
|
|
62
|
+
regionId: sample.region.id,
|
|
63
|
+
});
|
|
64
|
+
if (out.length >= max)
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/** Weighted pick from opaque entries; `roll` in [0, 1). Returns null when empty. */
|
|
71
|
+
export function pickWeighted(entries, roll) {
|
|
72
|
+
if (entries.length === 0)
|
|
73
|
+
return null;
|
|
74
|
+
let total = 0;
|
|
75
|
+
for (const entry of entries)
|
|
76
|
+
total += entry.weight;
|
|
77
|
+
const pick = roll * total;
|
|
78
|
+
let acc = 0;
|
|
79
|
+
for (const entry of entries) {
|
|
80
|
+
acc += entry.weight;
|
|
81
|
+
if (pick <= acc)
|
|
82
|
+
return entry.value;
|
|
83
|
+
}
|
|
84
|
+
return entries[entries.length - 1].value;
|
|
85
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { TerrainEnvironmentDescriptor, WorldBounds } from "./features.js";
|
|
2
|
+
export type TerrainNormal = readonly [number, number, number];
|
|
3
|
+
export interface TerrainField {
|
|
4
|
+
sampleHeight(x: number, z: number): number;
|
|
5
|
+
sampleNormal(x: number, z: number): TerrainNormal;
|
|
6
|
+
readonly bounds?: WorldBounds;
|
|
7
|
+
readonly waterLevel?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function valueNoise(x: number, z: number, seed: number): number;
|
|
10
|
+
export interface FractalNoiseConfig {
|
|
11
|
+
seed: number;
|
|
12
|
+
frequency: number;
|
|
13
|
+
octaves: number;
|
|
14
|
+
lacunarity: number;
|
|
15
|
+
persistence: number;
|
|
16
|
+
ridged: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function fractalNoise(x: number, z: number, config: FractalNoiseConfig): number;
|
|
19
|
+
export declare function seedFrom(value: string | number | undefined, fallback: number): number;
|
|
20
|
+
export declare function withNormal(sampleHeight: (x: number, z: number) => number): TerrainField["sampleNormal"];
|
|
21
|
+
export declare const FLAT_FIELD: TerrainField;
|
|
22
|
+
export declare function flatField(): TerrainField;
|
|
23
|
+
export interface NoiseFieldConfig {
|
|
24
|
+
seed?: string | number;
|
|
25
|
+
amplitude?: number;
|
|
26
|
+
frequency?: number;
|
|
27
|
+
octaves?: number;
|
|
28
|
+
lacunarity?: number;
|
|
29
|
+
persistence?: number;
|
|
30
|
+
baseHeight?: number;
|
|
31
|
+
ridged?: boolean;
|
|
32
|
+
bounds?: WorldBounds;
|
|
33
|
+
waterLevel?: number;
|
|
34
|
+
}
|
|
35
|
+
export declare function noiseField(config?: NoiseFieldConfig): TerrainField;
|
|
36
|
+
export interface RollingFieldConfig {
|
|
37
|
+
seed?: string | number;
|
|
38
|
+
amplitude?: number;
|
|
39
|
+
frequency?: number;
|
|
40
|
+
bounds?: WorldBounds;
|
|
41
|
+
}
|
|
42
|
+
export declare function rollingField(config?: RollingFieldConfig): TerrainField;
|
|
43
|
+
export interface ArenaFieldConfig {
|
|
44
|
+
seed?: string | number;
|
|
45
|
+
bounds?: WorldBounds;
|
|
46
|
+
}
|
|
47
|
+
export declare const ARENA_WATER_LEVEL = -0.9;
|
|
48
|
+
export declare function arenaField(config?: ArenaFieldConfig): TerrainField;
|
|
49
|
+
export declare function resolveTerrainField(descriptor?: TerrainEnvironmentDescriptor): TerrainField;
|
|
50
|
+
export declare const DEFAULT_MAX_WALK_SLOPE = 0.6;
|
|
51
|
+
export declare function resolveGroundStep(field: TerrainField, x: number, z: number, stepX: number, stepZ: number, maxSlope?: number): {
|
|
52
|
+
stepX: number;
|
|
53
|
+
stepZ: number;
|
|
54
|
+
};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
function clamp01(value) {
|
|
2
|
+
return value < 0 ? 0 : value > 1 ? 1 : value;
|
|
3
|
+
}
|
|
4
|
+
function lerp(a, b, t) {
|
|
5
|
+
return a + (b - a) * t;
|
|
6
|
+
}
|
|
7
|
+
function hash2(ix, iz, seed) {
|
|
8
|
+
let h = seed | 0;
|
|
9
|
+
h = Math.imul(h ^ (ix | 0), 0x27d4eb2d);
|
|
10
|
+
h = Math.imul(h ^ (iz | 0), 0x85ebca6b);
|
|
11
|
+
h ^= h >>> 15;
|
|
12
|
+
h = Math.imul(h, 0x2c1b3c6d);
|
|
13
|
+
h ^= h >>> 12;
|
|
14
|
+
return (h >>> 0) / 4294967295;
|
|
15
|
+
}
|
|
16
|
+
export function valueNoise(x, z, seed) {
|
|
17
|
+
const x0 = Math.floor(x);
|
|
18
|
+
const z0 = Math.floor(z);
|
|
19
|
+
const fx = x - x0;
|
|
20
|
+
const fz = z - z0;
|
|
21
|
+
const v00 = hash2(x0, z0, seed);
|
|
22
|
+
const v10 = hash2(x0 + 1, z0, seed);
|
|
23
|
+
const v01 = hash2(x0, z0 + 1, seed);
|
|
24
|
+
const v11 = hash2(x0 + 1, z0 + 1, seed);
|
|
25
|
+
const sx = fx * fx * (3 - 2 * fx);
|
|
26
|
+
const sz = fz * fz * (3 - 2 * fz);
|
|
27
|
+
const top = lerp(v00, v10, sx);
|
|
28
|
+
const bottom = lerp(v01, v11, sx);
|
|
29
|
+
return lerp(top, bottom, sz) * 2 - 1;
|
|
30
|
+
}
|
|
31
|
+
export function fractalNoise(x, z, config) {
|
|
32
|
+
let amplitude = 1;
|
|
33
|
+
let frequency = config.frequency;
|
|
34
|
+
let sum = 0;
|
|
35
|
+
let norm = 0;
|
|
36
|
+
for (let octave = 0; octave < config.octaves; octave += 1) {
|
|
37
|
+
let sample = valueNoise(x * frequency, z * frequency, config.seed + octave * 1013);
|
|
38
|
+
if (config.ridged)
|
|
39
|
+
sample = 1 - 2 * Math.abs(sample);
|
|
40
|
+
sum += sample * amplitude;
|
|
41
|
+
norm += amplitude;
|
|
42
|
+
amplitude *= config.persistence;
|
|
43
|
+
frequency *= config.lacunarity;
|
|
44
|
+
}
|
|
45
|
+
return norm === 0 ? 0 : sum / norm;
|
|
46
|
+
}
|
|
47
|
+
export function seedFrom(value, fallback) {
|
|
48
|
+
if (typeof value === "number")
|
|
49
|
+
return value | 0;
|
|
50
|
+
if (typeof value === "string") {
|
|
51
|
+
let hash = 2166136261;
|
|
52
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
53
|
+
hash ^= value.charCodeAt(index);
|
|
54
|
+
hash = Math.imul(hash, 16777619);
|
|
55
|
+
}
|
|
56
|
+
return hash | 0;
|
|
57
|
+
}
|
|
58
|
+
return fallback;
|
|
59
|
+
}
|
|
60
|
+
export function withNormal(sampleHeight) {
|
|
61
|
+
const epsilon = 0.75;
|
|
62
|
+
return (x, z) => {
|
|
63
|
+
const hx = sampleHeight(x + epsilon, z) - sampleHeight(x - epsilon, z);
|
|
64
|
+
const hz = sampleHeight(x, z + epsilon) - sampleHeight(x, z - epsilon);
|
|
65
|
+
const nx = -hx;
|
|
66
|
+
const nz = -hz;
|
|
67
|
+
const ny = 2 * epsilon;
|
|
68
|
+
const length = Math.hypot(nx, ny, nz) || 1;
|
|
69
|
+
return [nx / length, ny / length, nz / length];
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function fieldFromHeight(sampleHeight, extra) {
|
|
73
|
+
return {
|
|
74
|
+
sampleHeight,
|
|
75
|
+
sampleNormal: withNormal(sampleHeight),
|
|
76
|
+
...(extra?.bounds === undefined ? {} : { bounds: extra.bounds }),
|
|
77
|
+
...(extra?.waterLevel === undefined ? {} : { waterLevel: extra.waterLevel }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export const FLAT_FIELD = {
|
|
81
|
+
sampleHeight: () => 0,
|
|
82
|
+
sampleNormal: () => [0, 1, 0],
|
|
83
|
+
};
|
|
84
|
+
export function flatField() {
|
|
85
|
+
return FLAT_FIELD;
|
|
86
|
+
}
|
|
87
|
+
export function noiseField(config = {}) {
|
|
88
|
+
const fractal = {
|
|
89
|
+
seed: seedFrom(config.seed, 1337),
|
|
90
|
+
frequency: config.frequency ?? 0.03,
|
|
91
|
+
octaves: config.octaves ?? 4,
|
|
92
|
+
lacunarity: config.lacunarity ?? 2,
|
|
93
|
+
persistence: config.persistence ?? 0.5,
|
|
94
|
+
ridged: config.ridged ?? false,
|
|
95
|
+
};
|
|
96
|
+
const amplitude = config.amplitude ?? 1;
|
|
97
|
+
const baseHeight = config.baseHeight ?? 0;
|
|
98
|
+
return fieldFromHeight((x, z) => baseHeight + amplitude * fractalNoise(x, z, fractal), {
|
|
99
|
+
bounds: config.bounds,
|
|
100
|
+
waterLevel: config.waterLevel,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export function rollingField(config = {}) {
|
|
104
|
+
return noiseField({
|
|
105
|
+
seed: config.seed ?? "rolling",
|
|
106
|
+
amplitude: config.amplitude ?? 0.55,
|
|
107
|
+
frequency: config.frequency ?? 0.03,
|
|
108
|
+
octaves: 3,
|
|
109
|
+
persistence: 0.55,
|
|
110
|
+
bounds: config.bounds,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const ARENA_PLATEAU_HEIGHT = 6;
|
|
114
|
+
const ARENA_HILL_HEIGHT = 4.2;
|
|
115
|
+
const ARENA_BASIN_DEPTH = 2.6;
|
|
116
|
+
export const ARENA_WATER_LEVEL = -0.9;
|
|
117
|
+
function smoothstep(edge0, edge1, value) {
|
|
118
|
+
if (edge0 === edge1)
|
|
119
|
+
return value < edge0 ? 0 : 1;
|
|
120
|
+
const t = clamp01((value - edge0) / (edge1 - edge0));
|
|
121
|
+
return t * t * (3 - 2 * t);
|
|
122
|
+
}
|
|
123
|
+
export function arenaField(config = {}) {
|
|
124
|
+
const seed = seedFrom(config.seed ?? "arena", 7);
|
|
125
|
+
const rolling = {
|
|
126
|
+
seed,
|
|
127
|
+
frequency: 0.03,
|
|
128
|
+
octaves: 3,
|
|
129
|
+
lacunarity: 2,
|
|
130
|
+
persistence: 0.5,
|
|
131
|
+
ridged: false,
|
|
132
|
+
};
|
|
133
|
+
const sampleHeight = (x, z) => {
|
|
134
|
+
const distToSpawn = Math.hypot(x, z);
|
|
135
|
+
const spawn = 1 - smoothstep(14, 24, distToSpawn);
|
|
136
|
+
const undulation = 0.55 * fractalNoise(x, z, rolling);
|
|
137
|
+
const cliff = smoothstep(20, 27, z);
|
|
138
|
+
const ramp = smoothstep(16, 46, z);
|
|
139
|
+
const inRamp = 1 - clamp01(Math.abs(x) / 6);
|
|
140
|
+
const plateauExtent = 1 - smoothstep(30, 42, Math.abs(x));
|
|
141
|
+
const mesa = ARENA_PLATEAU_HEIGHT * plateauExtent * lerp(cliff, ramp, inRamp);
|
|
142
|
+
const hillDist = Math.hypot(x - 34, z + 8) / 18;
|
|
143
|
+
const hillFalloff = smoothstep(0, 1, 1 - hillDist);
|
|
144
|
+
const hill = ARENA_HILL_HEIGHT * hillFalloff;
|
|
145
|
+
const basinDist = Math.hypot(x + 40, z + 20) / 20;
|
|
146
|
+
const basin = -ARENA_BASIN_DEPTH * smoothstep(0, 1, 1 - basinDist);
|
|
147
|
+
return (undulation + mesa + hill + basin) * (1 - spawn);
|
|
148
|
+
};
|
|
149
|
+
return fieldFromHeight(sampleHeight, { bounds: config.bounds, waterLevel: ARENA_WATER_LEVEL });
|
|
150
|
+
}
|
|
151
|
+
export function resolveTerrainField(descriptor) {
|
|
152
|
+
if (descriptor === undefined)
|
|
153
|
+
return flatField();
|
|
154
|
+
return noiseField({
|
|
155
|
+
seed: descriptor.seed,
|
|
156
|
+
amplitude: descriptor.height,
|
|
157
|
+
frequency: descriptor.frequency,
|
|
158
|
+
octaves: descriptor.octaves,
|
|
159
|
+
ridged: descriptor.ridged,
|
|
160
|
+
baseHeight: descriptor.baseHeight,
|
|
161
|
+
waterLevel: descriptor.waterLevel,
|
|
162
|
+
bounds: descriptor.bounds,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
export const DEFAULT_MAX_WALK_SLOPE = 0.6;
|
|
166
|
+
export function resolveGroundStep(field, x, z, stepX, stepZ, maxSlope = DEFAULT_MAX_WALK_SLOPE) {
|
|
167
|
+
const baseHeight = field.sampleHeight(x, z);
|
|
168
|
+
const tooSteep = (dx, dz) => {
|
|
169
|
+
const distance = Math.hypot(dx, dz);
|
|
170
|
+
if (distance < 1e-6)
|
|
171
|
+
return false;
|
|
172
|
+
const rise = field.sampleHeight(x + dx, z + dz) - baseHeight;
|
|
173
|
+
return rise / distance > maxSlope;
|
|
174
|
+
};
|
|
175
|
+
if (!tooSteep(stepX, stepZ))
|
|
176
|
+
return { stepX, stepZ };
|
|
177
|
+
return {
|
|
178
|
+
stepX: tooSteep(stepX, 0) ? 0 : stepX,
|
|
179
|
+
stepZ: tooSteep(0, stepZ) ? 0 : stepZ,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { OceanEnvironmentDescriptor } from "./features.js";
|
|
2
|
+
export type WaterNormal = readonly [number, number, number];
|
|
3
|
+
export interface GerstnerWave {
|
|
4
|
+
direction: readonly [number, number];
|
|
5
|
+
wavelength: number;
|
|
6
|
+
amplitude: number;
|
|
7
|
+
steepness: number;
|
|
8
|
+
speed: number;
|
|
9
|
+
}
|
|
10
|
+
export interface WaterSurfaceConfig {
|
|
11
|
+
level?: number;
|
|
12
|
+
waveHeight?: number;
|
|
13
|
+
waveScale?: number;
|
|
14
|
+
waveSpeed?: number;
|
|
15
|
+
waves?: number;
|
|
16
|
+
choppiness?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface WaterSurface {
|
|
19
|
+
readonly level: number;
|
|
20
|
+
readonly waves: readonly GerstnerWave[];
|
|
21
|
+
height(x: number, z: number, time: number): number;
|
|
22
|
+
normal(x: number, z: number, time: number): WaterNormal;
|
|
23
|
+
displace(x: number, z: number, time: number): readonly [number, number, number];
|
|
24
|
+
}
|
|
25
|
+
export declare function synthesizeWaves(config?: WaterSurfaceConfig): GerstnerWave[];
|
|
26
|
+
export declare function waterSurface(config?: WaterSurfaceConfig): WaterSurface;
|
|
27
|
+
export declare function waterSurfaceFromDescriptor(descriptor: OceanEnvironmentDescriptor, waves?: number): WaterSurface;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
const GRAVITY = 9.81;
|
|
2
|
+
const TWO_PI = Math.PI * 2;
|
|
3
|
+
const AMPLITUDE_FALLOFF = 0.65;
|
|
4
|
+
const WAVELENGTH_FALLOFF = 0.65;
|
|
5
|
+
const DIRECTION_SPREAD = 0.62;
|
|
6
|
+
function resolveConfig(config) {
|
|
7
|
+
return {
|
|
8
|
+
level: config.level ?? 0,
|
|
9
|
+
waveHeight: config.waveHeight ?? 1.2,
|
|
10
|
+
waveScale: config.waveScale ?? 18,
|
|
11
|
+
waveSpeed: config.waveSpeed ?? 0.55,
|
|
12
|
+
waves: config.waves ?? 4,
|
|
13
|
+
choppiness: config.choppiness ?? 1,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function synthesizeWaves(config = {}) {
|
|
17
|
+
const resolved = resolveConfig(config);
|
|
18
|
+
const count = Math.max(0, Math.floor(resolved.waves));
|
|
19
|
+
if (count === 0 || resolved.waveHeight <= 0) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
let amplitudeSum = 0;
|
|
23
|
+
for (let i = 0; i < count; i += 1) {
|
|
24
|
+
amplitudeSum += AMPLITUDE_FALLOFF ** i;
|
|
25
|
+
}
|
|
26
|
+
const amplitudeUnit = resolved.waveHeight / amplitudeSum;
|
|
27
|
+
const waves = [];
|
|
28
|
+
for (let i = 0; i < count; i += 1) {
|
|
29
|
+
const amplitude = amplitudeUnit * AMPLITUDE_FALLOFF ** i;
|
|
30
|
+
const wavelength = resolved.waveScale * WAVELENGTH_FALLOFF ** i;
|
|
31
|
+
const k = TWO_PI / wavelength;
|
|
32
|
+
const angle = DIRECTION_SPREAD * (i % 2 === 0 ? 1 : -1) * Math.ceil(i / 2);
|
|
33
|
+
const direction = [Math.cos(angle), Math.sin(angle)];
|
|
34
|
+
const omega = Math.sqrt(GRAVITY * k) * resolved.waveSpeed;
|
|
35
|
+
const speed = omega / k;
|
|
36
|
+
const steepnessLimit = 1 / (k * amplitude * count);
|
|
37
|
+
const steepness = Math.min(1, Math.max(0, 0.4 * steepnessLimit));
|
|
38
|
+
waves.push({ direction, wavelength, amplitude, steepness, speed });
|
|
39
|
+
}
|
|
40
|
+
return waves;
|
|
41
|
+
}
|
|
42
|
+
export function waterSurface(config = {}) {
|
|
43
|
+
const resolved = resolveConfig(config);
|
|
44
|
+
const level = resolved.level;
|
|
45
|
+
const choppiness = resolved.choppiness;
|
|
46
|
+
const waves = synthesizeWaves(config);
|
|
47
|
+
const height = (x, z, time) => {
|
|
48
|
+
let y = level;
|
|
49
|
+
for (const wave of waves) {
|
|
50
|
+
const k = TWO_PI / wave.wavelength;
|
|
51
|
+
const omega = wave.speed * k;
|
|
52
|
+
const phase = k * (wave.direction[0] * x + wave.direction[1] * z) - omega * time;
|
|
53
|
+
y += wave.amplitude * Math.sin(phase);
|
|
54
|
+
}
|
|
55
|
+
return y;
|
|
56
|
+
};
|
|
57
|
+
const displace = (x, z, time) => {
|
|
58
|
+
let dx = x;
|
|
59
|
+
let dz = z;
|
|
60
|
+
let dy = level;
|
|
61
|
+
for (const wave of waves) {
|
|
62
|
+
const k = TWO_PI / wave.wavelength;
|
|
63
|
+
const omega = wave.speed * k;
|
|
64
|
+
const phase = k * (wave.direction[0] * x + wave.direction[1] * z) - omega * time;
|
|
65
|
+
const cosPhase = Math.cos(phase);
|
|
66
|
+
const q = wave.steepness;
|
|
67
|
+
dx += q * wave.amplitude * choppiness * wave.direction[0] * cosPhase;
|
|
68
|
+
dz += q * wave.amplitude * choppiness * wave.direction[1] * cosPhase;
|
|
69
|
+
dy += wave.amplitude * Math.sin(phase);
|
|
70
|
+
}
|
|
71
|
+
return [dx, dy, dz];
|
|
72
|
+
};
|
|
73
|
+
const normal = (x, z, time) => {
|
|
74
|
+
let tx = 1;
|
|
75
|
+
let ty = 0;
|
|
76
|
+
let tz = 0;
|
|
77
|
+
let bx = 0;
|
|
78
|
+
let by = 0;
|
|
79
|
+
let bz = 1;
|
|
80
|
+
for (const wave of waves) {
|
|
81
|
+
const k = TWO_PI / wave.wavelength;
|
|
82
|
+
const omega = wave.speed * k;
|
|
83
|
+
const phase = k * (wave.direction[0] * x + wave.direction[1] * z) - omega * time;
|
|
84
|
+
const cosPhase = Math.cos(phase);
|
|
85
|
+
const sinPhase = Math.sin(phase);
|
|
86
|
+
const wa = k * wave.amplitude;
|
|
87
|
+
const q = wave.steepness;
|
|
88
|
+
const dxWave = wave.direction[0];
|
|
89
|
+
const dzWave = wave.direction[1];
|
|
90
|
+
tx += -q * dxWave * dxWave * wa * choppiness * sinPhase;
|
|
91
|
+
ty += dxWave * wa * cosPhase;
|
|
92
|
+
tz += -q * dxWave * dzWave * wa * choppiness * sinPhase;
|
|
93
|
+
bx += -q * dxWave * dzWave * wa * choppiness * sinPhase;
|
|
94
|
+
by += dzWave * wa * cosPhase;
|
|
95
|
+
bz += -q * dzWave * dzWave * wa * choppiness * sinPhase;
|
|
96
|
+
}
|
|
97
|
+
let nx = by * tz - bz * ty;
|
|
98
|
+
let ny = bz * tx - bx * tz;
|
|
99
|
+
let nz = bx * ty - by * tx;
|
|
100
|
+
const lengthSq = nx * nx + ny * ny + nz * nz;
|
|
101
|
+
if (lengthSq === 0) {
|
|
102
|
+
return [0, 1, 0];
|
|
103
|
+
}
|
|
104
|
+
const inv = 1 / Math.sqrt(lengthSq);
|
|
105
|
+
nx *= inv;
|
|
106
|
+
ny *= inv;
|
|
107
|
+
nz *= inv;
|
|
108
|
+
if (ny < 0) {
|
|
109
|
+
nx = -nx;
|
|
110
|
+
ny = -ny;
|
|
111
|
+
nz = -nz;
|
|
112
|
+
}
|
|
113
|
+
return [nx, ny, nz];
|
|
114
|
+
};
|
|
115
|
+
return { level, waves, height, normal, displace };
|
|
116
|
+
}
|
|
117
|
+
export function waterSurfaceFromDescriptor(descriptor, waves) {
|
|
118
|
+
return waterSurface({
|
|
119
|
+
level: descriptor.level,
|
|
120
|
+
waveHeight: descriptor.waveHeight,
|
|
121
|
+
waveScale: descriptor.waveScale,
|
|
122
|
+
waveSpeed: descriptor.waveSpeed,
|
|
123
|
+
...(waves === undefined ? {} : { waves }),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type WindVector = readonly [number, number];
|
|
2
|
+
export interface WindFieldConfig {
|
|
3
|
+
direction?: WindVector;
|
|
4
|
+
speed?: number;
|
|
5
|
+
gust?: number;
|
|
6
|
+
gustFrequency?: number;
|
|
7
|
+
turbulence?: number;
|
|
8
|
+
seed?: string | number;
|
|
9
|
+
}
|
|
10
|
+
export interface WindField {
|
|
11
|
+
readonly direction: WindVector;
|
|
12
|
+
readonly speed: number;
|
|
13
|
+
at(time: number): WindVector;
|
|
14
|
+
atPoint(x: number, z: number, time: number): WindVector;
|
|
15
|
+
strengthAt(x: number, z: number, time: number): number;
|
|
16
|
+
}
|
|
17
|
+
export declare function windField(config?: WindFieldConfig): WindField;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { fractalNoise, seedFrom, valueNoise } from "./terrain.js";
|
|
2
|
+
const WIND_SEED_FALLBACK = 0x77696e64;
|
|
3
|
+
function normalizeDirection(direction) {
|
|
4
|
+
const length = Math.hypot(direction[0], direction[1]);
|
|
5
|
+
if (length === 0)
|
|
6
|
+
return [1, 0];
|
|
7
|
+
return [direction[0] / length, direction[1] / length];
|
|
8
|
+
}
|
|
9
|
+
export function windField(config = {}) {
|
|
10
|
+
const direction = normalizeDirection(config.direction ?? [1, 0]);
|
|
11
|
+
const speed = config.speed ?? 1;
|
|
12
|
+
const gust = config.gust ?? 0;
|
|
13
|
+
const gustFrequency = config.gustFrequency ?? 0.15;
|
|
14
|
+
const turbulence = config.turbulence ?? 0;
|
|
15
|
+
const seed = seedFrom(config.seed, WIND_SEED_FALLBACK);
|
|
16
|
+
const at = (time) => {
|
|
17
|
+
const gustAmt = gust *
|
|
18
|
+
fractalNoise(time * gustFrequency, 0, {
|
|
19
|
+
seed,
|
|
20
|
+
frequency: 1,
|
|
21
|
+
octaves: 3,
|
|
22
|
+
lacunarity: 2,
|
|
23
|
+
persistence: 0.5,
|
|
24
|
+
ridged: false,
|
|
25
|
+
});
|
|
26
|
+
const magnitude = speed + gustAmt;
|
|
27
|
+
return [direction[0] * magnitude, direction[1] * magnitude];
|
|
28
|
+
};
|
|
29
|
+
const atPoint = (x, z, time) => {
|
|
30
|
+
const base = at(time);
|
|
31
|
+
const tx = valueNoise(x * 0.05 + time * 0.1, z * 0.05, seed);
|
|
32
|
+
const tz = valueNoise(z * 0.05, x * 0.05 - time * 0.1, seed + 101);
|
|
33
|
+
return [base[0] + turbulence * tx, base[1] + turbulence * tz];
|
|
34
|
+
};
|
|
35
|
+
const strengthAt = (x, z, time) => {
|
|
36
|
+
const [wx, wz] = atPoint(x, z, time);
|
|
37
|
+
return Math.hypot(wx, wz);
|
|
38
|
+
};
|
|
39
|
+
return { direction, speed, at, atPoint, strengthAt };
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jgengine/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Genre-agnostic TypeScript game engine SDK: game runtime, state store, entity scene, combat, movement, inventory, economy, quests, multiplayer primitives.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"type": "module",
|