Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(weather): the wind raises the waves and bends the plants, and rain falls on the water

yhh0669329d26fb
diff --git a/sdk/src/ecs/component.ts b/sdk/src/ecs/component.ts
index f667d106d..09bd4a52a 100644
--- a/sdk/src/ecs/component.ts
+++ b/sdk/src/ecs/component.ts
@@ -1353,7 +1353,7 @@ export interface WeatherData {
sequence: string;
/** Seconds each weather of the sequence holds before the next turn begins. */
hold: number;
- /** What the weather drives: lights, fog, rain, grade, exposure, reflections, sound. */
+ /** What the weather drives: lights, fog, rain, grade, exposure, reflections, sound, wind (plants and waves). */
drive: number;
/** An entity with a looping AudioSource — the sound of the rain. The weather sets its volume (the authored one times
* how hard it rains) and raises or lowers its `playing`. 0 = none. */
@@ -1380,7 +1380,7 @@ export const Weather = defineComponent<WeatherData>('Weather', {
duration: 6,
sequence: '',
hold: 20,
- drive: 127,
+ drive: 255,
soundSource: 0,
}, {
entityFields: ['soundSource'],
@@ -1393,6 +1393,7 @@ export const Weather = defineComponent<WeatherData>('Weather', {
drive: { flags: [
{ label: 'Lights', value: 1 }, { label: 'Fog', value: 2 }, { label: 'Rain', value: 4 }, { label: 'Grade', value: 8 },
{ label: 'Exposure', value: 16 }, { label: 'Reflections', value: 32 }, { label: 'Sound', value: 64 },
+ { label: 'Wind', value: 128 },
] },
},
});
diff --git a/sdk/src/foliage/foliagePlugin.ts b/sdk/src/foliage/foliagePlugin.ts
index 37fc90cc4..8af42164c 100644
--- a/sdk/src/foliage/foliagePlugin.ts
+++ b/sdk/src/foliage/foliagePlugin.ts
@@ -21,6 +21,9 @@ import type { MeshData } from '../asset/meshFormat';
import { log } from '../util/logger';
import type { Entity } from '../types';
import { TerrainsRes } from '../terrain/terrainPlugin';
+import { Material } from '../render/material';
+import { currentWeather, currentWeatherDrive, WeatherDrive } from '../postprocess/weather';
+import { isEditor, isPlayMode } from '../ecs/env';
import {
buildBillboardMesh, buildGrassMesh, buildInstancedMesh, scatterCell,
type Ground, type Plant,
@@ -117,6 +120,8 @@ const CELLS_PER_FRAME = 3;
/** Run-time facts about the foliage the plugin has grown. */
export class FoliageField {
/** @internal */ readonly built_ = new Map<Entity, BuiltFoliage>();
+ /** @internal The u_windStrength each material had before the weather's wind scaled it, to put back. */
+ readonly authoredWind_ = new Map<number, number>();
/** How many cells across every field are still waiting to be built. */
get pending(): number {
@@ -147,6 +152,7 @@ export class FoliagePlugin implements Plugin {
if (!app.hasResource(Assets)) return;
const terrains = app.hasResource(TerrainsRes) ? app.getResource(TerrainsRes) : null;
this.sweep(app, field);
+ this.blow(app, field);
// What stands on the ground waits for it: a plant placed before its terrain is read would stand on nothing.
if (terrains?.loading) return;
const revision = terrains?.revision_ ?? 0;
@@ -173,6 +179,40 @@ export class FoliagePlugin implements Plugin {
}, { name: 'FoliageSystem' }));
}
+ /**
+ * The weather's wind on the plants: each Foliage material's u_windStrength is its authored strength times the weather's `wind`, and is
+ * put back when the weather is taken away or stops driving the wind. Only while the game runs, as the weather's lights are: in the
+ * editor a material asset is left as authored, so previewing a storm never saves one.
+ */
+ private blow(app: App, field: FoliageField): void {
+ const w = currentWeather();
+ const playing = !isEditor() || isPlayMode();
+ const wind = w && playing && (currentWeatherDrive() & WeatherDrive.Wind) ? w.wind : 1;
+ const materials = new Set<number>();
+ if (wind !== 1) {
+ for (const entity of app.world.getEntitiesWithComponents([Foliage])) {
+ const material = (app.world.get(entity, Foliage) as FoliageData).material;
+ if (material) materials.add(material);
+ }
+ }
+ for (const material of materials) {
+ let authored = field.authoredWind_.get(material);
+ if (authored === undefined) {
+ const value = Material.getUniform(material, 'u_windStrength');
+ if (typeof value !== 'number') continue;
+ authored = value;
+ field.authoredWind_.set(material, authored);
+ }
+ Material.setUniform(material, 'u_windStrength', authored * wind);
+ }
+ // What the weather no longer blows goes back to its own strength, once; the material is the author's again from then on.
+ for (const [material, authored] of field.authoredWind_) {
+ if (materials.has(material)) continue;
+ Material.setUniform(material, 'u_windStrength', authored);
+ field.authoredWind_.delete(material);
+ }
+ }
+
cleanup(app?: App): void {
if (!app?.hasResource(FoliageRes)) return;
const field = app.getResource(FoliageRes);
diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.ts
index d55526973..760bce4ce 100644
--- a/sdk/src/postprocess/sources/atmosphere.ts
+++ b/sdk/src/postprocess/sources/atmosphere.ts
@@ -7,7 +7,7 @@
import type { ShaderHandle } from '../../render/material';
import { Material } from '../../render/material';
import { registerEffectSources } from '../effectSources';
-import { SSAO_GLSL, SSAO_WGSL, PUDDLE_GLSL, PUDDLE_WGSL, BASIN_GLSL, BASIN_WGSL } from './shared';
+import { SSAO_GLSL, SSAO_WGSL, PUDDLE_GLSL, PUDDLE_WGSL, BASIN_GLSL, BASIN_WGSL, GBUFFER_GLSL, GBUFFER_WGSL } from './shared';
export const atmosphereEffects = {
/**
@@ -831,6 +831,7 @@ fn streaks(uv : vec2f, aspect : f32, cols : f32, speed : f32, seed : f32, T : f3
#pragma param u_coverMatrix2 color default(0,0,1,0)
#pragma param u_coverMatrix3 color default(0,0,0,1)
#pragma param u_rainCover texture default(white)
+#pragma gbuffer
#pragma fragment
precision highp float;
@@ -838,11 +839,13 @@ precision highp float;
in vec2 v_texCoord;
uniform sampler2D u_texture;
uniform highp sampler2D u_sceneDepth;
+uniform highp sampler2D u_gbuffer;
out vec4 fragColor;
${SSAO_GLSL}
${PUDDLE_GLSL}
${BASIN_GLSL}
+${GBUFFER_GLSL}
${GLSL}
/// 1 where something stands over a point, as the map of the scene seen from straight above says — an awning, a roof,
@@ -875,7 +878,10 @@ void main() {
#endif
vec3 col = src.rgb;
- if (d < 1.0 && u_wetness > 0.0) {
+ // Water is the surface here, not the ground the depth found under it: the water takes the rain in its own ripples, and a wet
+ // floor and its puddles drawn over it were the bed of the lake showing through.
+ Surface surface = readSurface(c);
+ if (d < 1.0 && u_wetness > 0.0 && !surface.water) {
vec3 P = worldFromDepth(uvOfTexel(c, size), d);
float dl = depthAt(c + ivec2(-1, 0), size);
float dr = depthAt(c + ivec2( 1, 0), size);
@@ -892,7 +898,9 @@ void main() {
// ground under an awning, and the awning, open to the sky, is wet in its turn.
float shelter = shelteredAt(P + vec3(0.0, 1.0, 0.0));
float wet = u_wetness * (1.0 - 0.9 * shelter);
- float puddle = puddleMask(P.xz, basinCoverage(c, size, P, u_puddles, u_puddleScale), u_puddleScale) * up * wet;
+ // Standing water on what is sealed (a street, a flagstone); ground as rough as soil or grass soaks it up, and is only darker.
+ float soaks = surface.known ? smoothstep(0.82, 0.95, surface.roughness) : 0.0;
+ float puddle = puddleMask(P.xz, basinCoverage(c, size, P, u_puddles, u_puddleScale), u_puddleScale) * up * wet * (1.0 - soaks);
col *= 1.0 - 0.32 * wet * up;
col *= 1.0 - 0.35 * puddle;
// Drops landing need drops falling: wet ground under no rain is still.
@@ -935,6 +943,7 @@ void main() {
${SSAO_WGSL}
${PUDDLE_WGSL}
${BASIN_WGSL}
+${GBUFFER_WGSL}
${WGSL}
fn shelteredAt(W : vec3f) -> f32 {
@@ -968,7 +977,8 @@ fn shelteredAt(W : vec3f) -> f32 {
#endif
var col = src.rgb;
- if (d < 1.0 && mc.u_wetness > 0.0) {
+ let surface = readSurface(c);
+ if (d < 1.0 && mc.u_wetness > 0.0 && !surface.water) {
let P = worldFromDepth(uvOfTexel(c, size), d);
let dl = depthAt(c + vec2i(-1, 0), size);
let dr = depthAt(c + vec2i( 1, 0), size);
@@ -983,7 +993,9 @@ fn shelteredAt(W : vec3f) -> f32 {
let up = smoothstep(0.86, 0.95, N.y);
let shelter = shelteredAt(P + vec3f(0.0, 1.0, 0.0));
let wet = mc.u_wetness * (1.0 - 0.9 * shelter);
- let puddle = puddleMask(P.xz, basinCoverage(c, size, P, mc.u_puddles, mc.u_puddleScale), mc.u_puddleScale) * up * wet;
+ var soaks = 0.0;
+ if (surface.known) { soaks = smoothstep(0.82, 0.95, surface.roughness); }
+ let puddle = puddleMask(P.xz, basinCoverage(c, size, P, mc.u_puddles, mc.u_puddleScale), mc.u_puddleScale) * up * wet * (1.0 - soaks);
col = col * (1.0 - 0.32 * wet * up);
col = col * (1.0 - 0.35 * puddle);
let falling = clamp(mc.u_intensity, 0.0, 1.0) * (1.0 - shelter);
diff --git a/sdk/src/postprocess/sources/shared.ts b/sdk/src/postprocess/sources/shared.ts
index f3b58eacd..122bb03c2 100644
--- a/sdk/src/postprocess/sources/shared.ts
+++ b/sdk/src/postprocess/sources/shared.ts
@@ -157,14 +157,15 @@ fn basinCoverage(c : vec2i, size : vec2i, P : vec3f, coverage : f32, scale : f32
* Reading the scene's surface record, in both languages: what each pixel IS, as the scene's own shaders
* wrote it beside the colour — octahedral normal in rg, roughness in b, metalness in a (a is 0 where nothing
* wrote, and 0.75 + 0.25 * metalness where something did, so that an edge multisampling averaged with an
- * unwritten pixel — at most 0.5 — is told from a surface). An effect that wants it says `#pragma gbuffer`,
+ * unwritten pixel — at most 0.5 — is told from a surface; water's 0.72 is one rain leaves dry). An effect that wants it says `#pragma gbuffer`,
*/
export const GBUFFER_GLSL = `
-struct Surface { vec3 normal; float roughness; float metallic; bool known; };
+struct Surface { vec3 normal; float roughness; float metallic; bool known; bool water; };
Surface readSurface(ivec2 t) {
vec4 g = texelFetch(u_gbuffer, t, 0);
Surface s;
s.known = g.a > 0.7;
+ s.water = g.a > 0.7 && g.a < 0.735;
vec2 f = g.rg * 2.0 - 1.0;
vec3 n = vec3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y));
float k = clamp(-n.z, 0.0, 1.0);
@@ -176,7 +177,7 @@ Surface readSurface(ivec2 t) {
}`;
export const GBUFFER_WGSL = `
-struct Surface { normal : vec3f, roughness : f32, metallic : f32, known : bool };
+struct Surface { normal : vec3f, roughness : f32, metallic : f32, known : bool, water : bool };
fn readSurface(t : vec2i) -> Surface {
let g = textureLoad(t6, t, 0);
var f = g.rg * 2.0 - 1.0;
@@ -185,6 +186,7 @@ fn readSurface(t : vec2i) -> Surface {
n = vec3f(n.x + select(k, -k, n.x >= 0.0), n.y + select(k, -k, n.y >= 0.0), n.z);
var s : Surface;
s.known = g.a > 0.7;
+ s.water = g.a > 0.7 && g.a < 0.735;
s.normal = normalize(n);
s.roughness = g.b;
s.metallic = clamp((g.a - 0.75) / 0.25, 0.0, 1.0);
diff --git a/sdk/src/postprocess/weather.ts b/sdk/src/postprocess/weather.ts
index 6a66708fd..63a17cb55 100644
--- a/sdk/src/postprocess/weather.ts
+++ b/sdk/src/postprocess/weather.ts
@@ -56,6 +56,10 @@ export interface WeatherState {
overcast: number;
/** How loud the rain on the soundtrack is, 0..2. */
sound: number;
+ /** × the wind: how far plants sway (a Foliage material's u_windStrength). */
+ wind: number;
+ /** × the waves' height on every body of water, in what is drawn and in what the surface answers. */
+ waves: number;
}
export type WeatherPreset = Partial<WeatherState>;
@@ -65,7 +69,7 @@ const NEUTRAL: WeatherState = {
fogDensity: 1, fogTint: [0.7, 0.72, 0.76], fogTintAmount: 0, fogGlow: 1, skyFog: 1,
saturation: 1, contrast: 1, exposure: 0,
rain: 0, wetness: 0, puddles: 0, ssr: 1,
- reflection: 1, reflectionSaturation: 1, overcast: 0, sound: 0,
+ reflection: 1, reflectionSaturation: 1, overcast: 0, sound: 0, wind: 1, waves: 1,
};
/** The presets every project has. Their ids are the dropdown's values, so they only ever grow. */
@@ -98,22 +102,22 @@ define(WEATHER_CLEAR, 'clear', {});
define(WEATHER_MIST, 'mist', {
sun: 0.45, sky: 0.9, shadowSoftness: 3, shadowStrength: 0.8, fogDensity: 8, fogTint: [0.86, 0.86, 0.84], fogTintAmount: 0.7, fogGlow: 1.8, skyFog: 1.7,
saturation: 0.86, contrast: 0.94, exposure: 0.2, wetness: 0.2, puddles: 0.1, ssr: 1.3, reflection: 0.85,
- reflectionSaturation: 0.9, overcast: 0.55,
+ reflectionSaturation: 0.9, overcast: 0.55, wind: 0.4, waves: 0.6,
});
define(WEATHER_OVERCAST, 'overcast', {
sun: 0.25, sky: 0.85, sunTint: [0.9, 0.93, 1], shadowSoftness: 8, shadowStrength: 0.5, fogDensity: 3, fogTint: [0.62, 0.66, 0.72], fogTintAmount: 0.5,
fogGlow: 0.3, skyFog: 1.4, saturation: 0.85, contrast: 0.96, exposure: 0.25, wetness: 0.1, ssr: 1.2,
- reflection: 0.7, reflectionSaturation: 0.7, overcast: 0.95,
+ reflection: 0.7, reflectionSaturation: 0.7, overcast: 0.95, wind: 1.5, waves: 1.3,
});
define(WEATHER_RAIN, 'rain', {
sun: 0.04, sky: 0.75, sunTint: [0.8, 0.86, 1], shadowSoftness: 10, shadowStrength: 0.3, fogDensity: 5.5, fogTint: [0.58, 0.63, 0.7], fogTintAmount: 0.8,
fogGlow: 0, skyFog: 1.7, saturation: 0.73, contrast: 0.93, exposure: 0.45, rain: 1.1, wetness: 1, puddles: 0.6,
- ssr: 2.6, reflection: 0.55, reflectionSaturation: 0.5, overcast: 0.9, sound: 1,
+ ssr: 2.6, reflection: 0.55, reflectionSaturation: 0.5, overcast: 0.9, sound: 1, wind: 1.9, waves: 1.5,
});
define(WEATHER_STORM, 'storm', {
sun: 0.02, sky: 0.5, sunTint: [0.7, 0.78, 1], shadowSoftness: 10, shadowStrength: 0.2, fogDensity: 8, fogTint: [0.4, 0.44, 0.5], fogTintAmount: 0.9,
fogGlow: 0, skyFog: 1.8, saturation: 0.6, contrast: 0.9, exposure: 0.8, rain: 2.2, wetness: 1, puddles: 1,
- ssr: 3, reflection: 0.4, reflectionSaturation: 0.35, overcast: 1, sound: 1.6,
+ ssr: 3, reflection: 0.4, reflectionSaturation: 0.35, overcast: 1, sound: 1.6, wind: 3.2, waves: 2.4,
});
/**
@@ -176,7 +180,9 @@ export const WeatherDrive = {
Exposure: 16,
Reflections: 32,
Sound: 64,
- All: 127,
+ /** The plants' sway and the waves' height. */
+ Wind: 128,
+ All: 255,
} as const;
/** The state the frame is under, or null when no Weather component is. Set by the weather system, read by the volumes. */
diff --git a/sdk/src/water/ripples.ts b/sdk/src/water/ripples.ts
index c2c5a5e55..82c1fffed 100644
--- a/sdk/src/water/ripples.ts
+++ b/sdk/src/water/ripples.ts
@@ -4,7 +4,7 @@
* @file ripples.ts
* @brief The disturbance a thing makes in water: a wave equation on a grid, and the foam it leaves.
*
- * @details A body of water that is asked to ripple keeps a square window of its surface, a few hundred world units across, as a grid of heights, and each
+ * @details A water zone that is asked to ripple keeps a square window of its surface, a few hundred world units across, as a grid of heights, and each
* step moves it by the wave equation (every cell is pulled toward the mean of its four neighbours, and carries the velocity it had), with a little
* damping so a ripple dies. A disturbance (a body entering, a body moving, a script's `disturb`) is a smooth bump put into the grid; it spreads
* as a ring at the wave speed. Beside the height the grid keeps a foam: fed where a disturbance was and where the water moves, and fading with time.
@@ -25,6 +25,8 @@ export class RippleField {
/** World size of one cell. */
readonly cell: number;
/** The window's minimum corner in the body's xz plane. */
+ /** Drops of rain owed to the window: the weather asks for a rate, a frame takes whole drops and carries the rest. */
+ rainOwed = 0;
originX = 0;
originZ = 0;
/** The most height a cell may hold, either way: disturbances that go on (a body moving) saturate here instead of piling up. */
diff --git a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
index fc6a67e4b..3c40736ea 100644
--- a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
+++ b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
@@ -1755,6 +1755,10 @@ exports[`builtin FieldMeta (RC9-1 equivalence gate) > matches the golden snapsho
"label": "Sound",
"value": 64,
},
+ {
+ "label": "Wind",
+ "value": 128,
+ },
],
},
"duration": {