create-threenative 0.2.7 → 0.2.8
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/README.md +3 -1
- package/agent-docs/references/capability-reference.md +177 -7
- package/capabilities.json +247 -4
- package/dist/index.js +6 -1
- package/dist/threenative.js +3 -2
- package/package.json +4 -4
- package/templates/action-rpg/package.json +1 -1
- package/templates/minimal/package.json +1 -1
- package/templates/platformer/package.json +1 -1
- package/templates/puzzle/package.json +1 -1
- package/templates/racing/package.json +1 -1
- package/templates/rain/.codex/config.toml +19 -0
- package/templates/rain/.cursor/mcp.json +32 -0
- package/templates/rain/.gemini/settings.json +32 -0
- package/templates/rain/.mcp.json +32 -0
- package/templates/rain/.vscode/mcp.json +36 -0
- package/templates/rain/.zed/settings.json +36 -0
- package/templates/rain/AGENTS.md +96 -0
- package/templates/rain/CLAUDE.md +98 -0
- package/templates/rain/assets/rain-loop.wav +0 -0
- package/templates/rain/assets/thunder.wav +0 -0
- package/templates/rain/assets/wind-loop.wav +0 -0
- package/templates/rain/index.html +18 -0
- package/templates/rain/kit.json +7 -0
- package/templates/rain/native-playtests/boot.playtest.json +13 -0
- package/templates/rain/native-playtests/lightning.playtest.json +173 -0
- package/templates/rain/opencode.json +41 -0
- package/templates/rain/package.json +56 -0
- package/templates/rain/playtests/auto-lightning-clear.playtest.json +66 -0
- package/templates/rain/playtests/auto-lightning.playtest.json +26 -0
- package/templates/rain/playtests/automation.playtest.json +133 -0
- package/templates/rain/playtests/cinematic.playtest.json +59 -0
- package/templates/rain/playtests/flash.playtest.json +27 -0
- package/templates/rain/playtests/fly.playtest.json +54 -0
- package/templates/rain/playtests/lightning.playtest.json +220 -0
- package/templates/rain/playtests/loading.playtest.json +57 -0
- package/templates/rain/playtests/paused.playtest.json +59 -0
- package/templates/rain/playtests/performance.playtest.json +22 -0
- package/templates/rain/playtests/reduced-motion.playtest.json +87 -0
- package/templates/rain/playtests/simulation.playtest.json +20 -0
- package/templates/rain/playtests/storm.playtest.json +150 -0
- package/templates/rain/playtests/survives.playtest.json +58 -0
- package/templates/rain/playtests/tiers.playtest.json +179 -0
- package/templates/rain/playtests/weather.playtest.json +77 -0
- package/templates/rain/public/favicon.svg +5 -0
- package/templates/rain/src/audio/storm.ts +314 -0
- package/templates/rain/src/game.ts +114 -0
- package/templates/rain/src/main.ts +15 -0
- package/templates/rain/src/render/bloom-shader.ts +65 -0
- package/templates/rain/src/render/camera.ts +160 -0
- package/templates/rain/src/render/clouds-shader.ts +242 -0
- package/templates/rain/src/render/clouds.ts +142 -0
- package/templates/rain/src/render/lighting.ts +39 -0
- package/templates/rain/src/render/lightning.ts +360 -0
- package/templates/rain/src/render/loading.ts +327 -0
- package/templates/rain/src/render/materials.ts +19 -0
- package/templates/rain/src/render/noise-volume.ts +159 -0
- package/templates/rain/src/render/palette.ts +18 -0
- package/templates/rain/src/render/post-shader.ts +178 -0
- package/templates/rain/src/render/postprocessing.ts +226 -0
- package/templates/rain/src/render/quality.ts +135 -0
- package/templates/rain/src/render/rain.ts +299 -0
- package/templates/rain/src/render/sky.ts +28 -0
- package/templates/rain/src/render/world-shader.ts +680 -0
- package/templates/rain/src/render/world.ts +146 -0
- package/templates/rain/src/render/worldEnvironment.ts +809 -0
- package/templates/rain/src/scenes/Boot.ts +334 -0
- package/templates/rain/src/state.ts +257 -0
- package/templates/rain/src/style.css +1160 -0
- package/templates/rain/src/ui/App.tsx +29 -0
- package/templates/rain/src/ui/GameUi.tsx +187 -0
- package/templates/rain/src/ui/Hud.tsx +327 -0
- package/templates/rain/src/ui/LoadingOverlay.tsx +46 -0
- package/templates/rain/src/ui/Menu.tsx +370 -0
- package/templates/rain/src/ui/automation.ts +60 -0
- package/templates/rain/src/ui/main.tsx +7 -0
- package/templates/rain/src/ui/tempest.ts +149 -0
- package/templates/rain/tailwind.config.ts +2 -0
- package/templates/rain/threenative.config.ts +66 -0
- package/templates/rain/tools/generate-shaders.mjs +656 -0
- package/templates/rain/tools/make-storm-audio.mjs +387 -0
- package/templates/rain/tools/storm-dsp.mjs +177 -0
- package/templates/rain/tools/tempest-bloom.frag +10 -0
- package/templates/rain/tools/tempest-clouds.frag +86 -0
- package/templates/rain/tools/tempest-post.frag +47 -0
- package/templates/rain/tools/tempest-world.frag +210 -0
- package/templates/rain/tools/verify-noise-volume.mjs +61 -0
- package/templates/rain/tsconfig.json +17 -0
- package/templates/rain/vite.config.ts +37 -0
- package/templates/rts/package.json +1 -1
- package/templates/runner/package.json +1 -1
- package/templates/sailing/package.json +1 -1
- package/templates/shooter/package.json +1 -1
- package/templates/snow/.codex/config.toml +19 -0
- package/templates/snow/.cursor/mcp.json +32 -0
- package/templates/snow/.gemini/settings.json +32 -0
- package/templates/snow/.mcp.json +32 -0
- package/templates/snow/.vscode/mcp.json +36 -0
- package/templates/snow/.zed/settings.json +36 -0
- package/templates/snow/AGENTS.md +88 -0
- package/templates/snow/CLAUDE.md +90 -0
- package/templates/snow/README.md +26 -0
- package/templates/snow/assets/crunch.wav +0 -0
- package/templates/snow/assets/wind.wav +0 -0
- package/templates/snow/index.html +12 -0
- package/templates/snow/kit.json +7 -0
- package/templates/snow/native-playtests/snow-physics.playtest.json +150 -0
- package/templates/snow/native-playtests/survives.playtest.json +47 -0
- package/templates/snow/opencode.json +41 -0
- package/templates/snow/package.json +58 -0
- package/templates/snow/playtests/footsteps.playtest.json +95 -0
- package/templates/snow/playtests/performance.playtest.json +36 -0
- package/templates/snow/playtests/snow-physics.playtest.json +154 -0
- package/templates/snow/playtests/survives.playtest.json +56 -0
- package/templates/snow/playtests/touch-controls.playtest.json +71 -0
- package/templates/snow/playtests/weather.playtest.json +119 -0
- package/templates/snow/public/favicon.svg +5 -0
- package/templates/snow/src/audio.ts +58 -0
- package/templates/snow/src/commands.ts +8 -0
- package/templates/snow/src/entities/Explorer.ts +279 -0
- package/templates/snow/src/game.ts +69 -0
- package/templates/snow/src/main.ts +15 -0
- package/templates/snow/src/render/bootPrint.ts +50 -0
- package/templates/snow/src/render/camera.ts +84 -0
- package/templates/snow/src/render/explorer.ts +321 -0
- package/templates/snow/src/render/lighting.ts +50 -0
- package/templates/snow/src/render/loading.ts +325 -0
- package/templates/snow/src/render/materials.ts +36 -0
- package/templates/snow/src/render/palette.ts +17 -0
- package/templates/snow/src/render/postprocessing.ts +44 -0
- package/templates/snow/src/render/props.ts +42 -0
- package/templates/snow/src/render/quality.ts +135 -0
- package/templates/snow/src/render/scenery.ts +244 -0
- package/templates/snow/src/render/sky.ts +57 -0
- package/templates/snow/src/render/snowSurface.ts +259 -0
- package/templates/snow/src/render/touch-controls.ts +143 -0
- package/templates/snow/src/render/weather.ts +279 -0
- package/templates/snow/src/render/worldEnvironment.ts +809 -0
- package/templates/snow/src/scenes/Boot.ts +12 -0
- package/templates/snow/src/scenes/Snow.ts +635 -0
- package/templates/snow/src/state.ts +46 -0
- package/templates/snow/src/style.css +143 -0
- package/templates/snow/src/terrain.ts +64 -0
- package/templates/snow/src/ui/App.tsx +15 -0
- package/templates/snow/src/ui/Controls.tsx +322 -0
- package/templates/snow/src/ui/GameUi.tsx +354 -0
- package/templates/snow/src/ui/icons.tsx +54 -0
- package/templates/snow/src/ui/main.tsx +7 -0
- package/templates/snow/tailwind.config.ts +2 -0
- package/templates/snow/threenative.config.ts +51 -0
- package/templates/snow/tsconfig.json +17 -0
- package/templates/snow/vite.config.ts +37 -0
- package/templates/starter/package.json +1 -1
- package/templates/tower-defense/package.json +1 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Generated for you: ordinary Three.js; ThreeNative does not read this file. Delete or rewrite
|
|
2
|
+
// it freely — the tiers below are a starting point, not a framework look.
|
|
3
|
+
//
|
|
4
|
+
// This is the one place this game decides how expensive it looks. Two tables live here:
|
|
5
|
+
//
|
|
6
|
+
// - `low` / `medium` / `high` are the engine render chain's tiers, which `postprocessing.ts` reads
|
|
7
|
+
// through `qualityPreset(resolveQualityTier(...))`. This study turns **every built-in stage off at
|
|
8
|
+
// every tier**: its bloom, ACES grade, vignette, lens beads and dither are the source study's own
|
|
9
|
+
// single post pass, installed as one authored stage, so a built-in bloom or tone map would do the
|
|
10
|
+
// same job a second time. What differs is the rung the chain reports and runs at
|
|
11
|
+
// (`renderChainTier`), so a stage you add later with a `minimumTier` drops out on the cheap tiers.
|
|
12
|
+
// - `STUDY_TIERS` is the player's "Render quality" select — the reference study's own four tiers,
|
|
13
|
+
// `performance` / `balanced` / `high` / `ultra`, number for number: the cloud pass's resolution
|
|
14
|
+
// share and march steps, the rain budget, and whether wet surfaces run the 36-step reflection
|
|
15
|
+
// march. The drawing buffer itself is the engine's adaptive resolution (`resolutionScale: "auto"`
|
|
16
|
+
// in `threenative.config.ts`), which measures the frame instead of trusting a fixed table.
|
|
17
|
+
import type { QualityName } from "../state.js";
|
|
18
|
+
import type { IWorldEnvironmentOptions } from "./worldEnvironment.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The engine chain's three tier names. `low` is what a phone gets and `high` what a desktop gets;
|
|
22
|
+
* for this study all three are the same empty chain — see the note at the top of this file.
|
|
23
|
+
*/
|
|
24
|
+
export type QualityTier = "low" | "medium" | "high";
|
|
25
|
+
|
|
26
|
+
const QUALITY_TIERS: readonly QualityTier[] = ["low", "medium", "high"];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Narrows an arbitrary string — a URL parameter, a saved setting — to a tier name. Not exported:
|
|
30
|
+
* `resolveQualityTier` is the one door in, so an unknown name cannot be waved past the throw.
|
|
31
|
+
*/
|
|
32
|
+
function isQualityTier(value: string): value is QualityTier {
|
|
33
|
+
return (QUALITY_TIERS as readonly string[]).includes(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Picks the tier: an explicit `tier` always wins, otherwise the platform decides.
|
|
38
|
+
*
|
|
39
|
+
* Fails closed. An unrecognised tier name throws with the value it was handed rather than
|
|
40
|
+
* quietly rendering the default, because a silent fallback here looks exactly like a tier that
|
|
41
|
+
* turned out to have no effect.
|
|
42
|
+
*/
|
|
43
|
+
export function resolveQualityTier(
|
|
44
|
+
request: { readonly mobile?: boolean; readonly software?: boolean; readonly tier?: string } = {},
|
|
45
|
+
): QualityTier {
|
|
46
|
+
const requested = request.tier;
|
|
47
|
+
if (requested !== undefined) {
|
|
48
|
+
if (!isQualityTier(requested)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Unknown quality tier ${JSON.stringify(requested)} — expected one of ${QUALITY_TIERS.join(", ")}.`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return requested;
|
|
54
|
+
}
|
|
55
|
+
// A named software adapter — SwiftShader, llvmpipe, a basic-render driver — is the machine this
|
|
56
|
+
// game's desktop look cannot run on: a single `high` frame on one can outlast the device it is
|
|
57
|
+
// drawing on, and no adaptation that reacts to frame times gets to run first. `software` is the
|
|
58
|
+
// fact the renderer read from `adapter.info`, not a guess from a driver string, and an explicit
|
|
59
|
+
// `tier` above still wins over it.
|
|
60
|
+
if (request.software === true) return "low";
|
|
61
|
+
return request.mobile === true ? "low" : "high";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What a desktop gets from the engine chain: no built-in stage, so there is no cost to record beside
|
|
66
|
+
* one; the storm's own post pass is costed in `STUDY_TIERS` below.
|
|
67
|
+
*/
|
|
68
|
+
const high: IWorldEnvironmentOptions = {
|
|
69
|
+
bloomEnabled: false,
|
|
70
|
+
denoiseEnabled: false,
|
|
71
|
+
// The post pass applies the weather's exposure itself, after its own bloom add.
|
|
72
|
+
exposure: 1,
|
|
73
|
+
renderChainTier: "high",
|
|
74
|
+
ssgiEnabled: false,
|
|
75
|
+
ssrEnabled: false,
|
|
76
|
+
tonemapMode: "aces",
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** The rung in between: the same empty chain, run and reported at `medium`. */
|
|
80
|
+
const medium: IWorldEnvironmentOptions = { ...high, renderChainTier: "medium" };
|
|
81
|
+
|
|
82
|
+
/** What a phone gets: the same empty chain at `low`; the `performance` study tier is the cheap look. */
|
|
83
|
+
const low: IWorldEnvironmentOptions = { ...high, renderChainTier: "low" };
|
|
84
|
+
|
|
85
|
+
const QUALITY_PRESETS: Record<QualityTier, IWorldEnvironmentOptions> = { high, low, medium };
|
|
86
|
+
|
|
87
|
+
/** The stages and strengths a tier turns on. Throws on a name that is not a tier. */
|
|
88
|
+
export function qualityPreset(tier: string): IWorldEnvironmentOptions {
|
|
89
|
+
const preset = QUALITY_PRESETS[tier as QualityTier];
|
|
90
|
+
if (preset === undefined) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`Unknown quality tier ${JSON.stringify(tier)} — expected one of ${QUALITY_TIERS.join(", ")}.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return preset;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** One rung of the player's quality select. Every number is the reference study's own. */
|
|
99
|
+
export interface IStudyTier {
|
|
100
|
+
/** The cloud pass renders at this share of the frame's resolution (the study's `q[1]`). */
|
|
101
|
+
readonly cloudScale: number;
|
|
102
|
+
/** Ray-march steps through the cloud volume (the study's `q[2]`). */
|
|
103
|
+
readonly cloudSteps: number;
|
|
104
|
+
/** Rain drops at full precipitation; the drawn count is this times the eased `rain`. */
|
|
105
|
+
readonly rainBudget: number;
|
|
106
|
+
/** Whether wet surfaces run the 36-step secondary reflection march. */
|
|
107
|
+
readonly reflections: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The four tiers the panel offers, cheapest first. The costs are **unmeasured** per stage on this
|
|
112
|
+
* scene: `playtests/tiers.playtest.json` reads back what each tier actually draws, and
|
|
113
|
+
* `TN_FRAME_BUDGET` is the frame-time reading to take after changing a rung.
|
|
114
|
+
*/
|
|
115
|
+
export const STUDY_TIERS: Readonly<Record<QualityName, IStudyTier>> = {
|
|
116
|
+
// The phone rung: the cheapest clouds, a thinner rain and no reflection march (unmeasured).
|
|
117
|
+
performance: { cloudScale: 0.4, cloudSteps: 32, rainBudget: 6_500, reflections: false },
|
|
118
|
+
// Reflections back on; clouds a little finer (unmeasured).
|
|
119
|
+
balanced: { cloudScale: 0.48, cloudSteps: 48, rainBudget: 12_000, reflections: true },
|
|
120
|
+
// The desktop default the panel opens on (unmeasured).
|
|
121
|
+
high: { cloudScale: 0.6, cloudSteps: 64, rainBudget: 12_000, reflections: true },
|
|
122
|
+
// The finest clouds and the full 16,000 drops (unmeasured).
|
|
123
|
+
ultra: { cloudScale: 0.75, cloudSteps: 88, rainBudget: 16_000, reflections: true },
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** A tier by name. Throws on a name the select does not offer, rather than drawing a default. */
|
|
127
|
+
export function studyTier(name: string): IStudyTier {
|
|
128
|
+
const tier = STUDY_TIERS[name as QualityName];
|
|
129
|
+
if (tier === undefined || !Object.hasOwn(STUDY_TIERS, name)) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
`Unknown study tier ${JSON.stringify(name)} — expected performance, balanced, high or ultra.`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return tier;
|
|
135
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// Generated for you. Ordinary Three.js, and the falling rain lives in this file.
|
|
2
|
+
//
|
|
3
|
+
// The reference study drew every drop in one vertex stage: six vertices per drop, the drop's own
|
|
4
|
+
// seed hashed from its index, its position wrapped inside a 66 x 30 x 66 metre cell that follows the
|
|
5
|
+
// camera in 12 metre steps, and its two endpoints projected by hand so each quad comes out stretched
|
|
6
|
+
// along the drop's own fall. That maths is transcribed below, constant for constant, and it is the
|
|
7
|
+
// rain: no sprite sheet, no texture, no particle simulation on the CPU.
|
|
8
|
+
//
|
|
9
|
+
// The engine owns the frame loop, the scene and the renderer, so this file owns three things — the
|
|
10
|
+
// uniforms, the geometry, and its own lifetime — and draws nothing itself. No render loop, no raw
|
|
11
|
+
// GL, no engine package import, so the same file runs wherever the game runs.
|
|
12
|
+
import {
|
|
13
|
+
DoubleSide,
|
|
14
|
+
Float32BufferAttribute,
|
|
15
|
+
InstancedBufferGeometry,
|
|
16
|
+
Mesh,
|
|
17
|
+
type PerspectiveCamera,
|
|
18
|
+
type Scene,
|
|
19
|
+
Vector3,
|
|
20
|
+
} from "three";
|
|
21
|
+
import {
|
|
22
|
+
abs,
|
|
23
|
+
cameraFar,
|
|
24
|
+
cameraNear,
|
|
25
|
+
clamp,
|
|
26
|
+
dot,
|
|
27
|
+
float,
|
|
28
|
+
floor,
|
|
29
|
+
fract,
|
|
30
|
+
frameGroup,
|
|
31
|
+
instanceIndex,
|
|
32
|
+
max,
|
|
33
|
+
mix,
|
|
34
|
+
mod,
|
|
35
|
+
normalize,
|
|
36
|
+
positionGeometry,
|
|
37
|
+
pow,
|
|
38
|
+
screenSize,
|
|
39
|
+
select,
|
|
40
|
+
sin,
|
|
41
|
+
smoothstep,
|
|
42
|
+
uniform,
|
|
43
|
+
varying,
|
|
44
|
+
vec2,
|
|
45
|
+
vec3,
|
|
46
|
+
vec4,
|
|
47
|
+
} from "three/tsl";
|
|
48
|
+
import { MeshBasicNodeMaterial } from "three/webgpu";
|
|
49
|
+
import type { Node } from "three/webgpu";
|
|
50
|
+
import type { QualityName, Weather } from "../state.js";
|
|
51
|
+
import { studyTier } from "./quality.js";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The six corners of one drop's quad, exactly as the study's `rainGeometry` wrote them: `x` runs
|
|
55
|
+
* across the streak from -1 to 1 and `y` runs along it from the drop's head to its tail. Stored as
|
|
56
|
+
* three-component positions because Three's own bounds pass reads a `position` attribute, and the
|
|
57
|
+
* quad is projected by the shader rather than by a camera matrix, so `z` is never read.
|
|
58
|
+
*/
|
|
59
|
+
const DROP_CORNERS = new Float32Array([-1, 0, 0, 1, 0, 0, -1, 1, 0, -1, 1, 0, 1, 0, 0, 1, 1, 0]);
|
|
60
|
+
|
|
61
|
+
/** The cell wraps at this many metres on each horizontal axis, and this many in height. */
|
|
62
|
+
const CELL_XZ = 66;
|
|
63
|
+
const CELL_Y = 30;
|
|
64
|
+
/** The cell follows the camera in steps of this many metres. */
|
|
65
|
+
const CELL_STEP = 12;
|
|
66
|
+
/** The most drops any tier may ask for. */
|
|
67
|
+
const MAX_DROPS = 16_000;
|
|
68
|
+
|
|
69
|
+
/** The study's own hash: one sine, one fract, and every drop's fate in it. */
|
|
70
|
+
function rnd(n: Node<"float">) {
|
|
71
|
+
return fract(sin(n.mul(127.1)).mul(43758.5453123));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* How many drops this budget and this weather actually draw, rounded to a whole drop.
|
|
76
|
+
*
|
|
77
|
+
* The number returned is what `geometry.instanceCount` is set to, so what the renderer issues and
|
|
78
|
+
* what this reports are the same figure — there is no second count anywhere that could disagree with
|
|
79
|
+
* the draw.
|
|
80
|
+
*/
|
|
81
|
+
export function rainInstanceCount(budget: number, rain: number): number {
|
|
82
|
+
if (!Number.isInteger(budget) || budget <= 0 || budget > MAX_DROPS) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`rain budget must be a whole number of drops in 1..${MAX_DROPS}, got ${budget}.`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (!Number.isFinite(rain) || rain < 0 || rain > 1) {
|
|
88
|
+
throw new Error(`weather.rain must be 0..1, got ${rain}.`);
|
|
89
|
+
}
|
|
90
|
+
return Math.round(budget * rain);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface IRainOptions {
|
|
94
|
+
/** Absolute seconds, so the fall does not depend on how the frame was scheduled. */
|
|
95
|
+
readonly elapsed: number;
|
|
96
|
+
/** Current flash envelope. The caller has already gated it on the photosensitivity switch. */
|
|
97
|
+
readonly flash: number;
|
|
98
|
+
readonly weather: Weather;
|
|
99
|
+
/** Which tier's drop budget to draw. An unknown name throws rather than falling back. */
|
|
100
|
+
readonly quality: QualityName;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface IStormRain {
|
|
104
|
+
/** The real mesh, for the engine's culling pass and for a scene walk that counts what is drawn. */
|
|
105
|
+
readonly mesh: Mesh;
|
|
106
|
+
/** The drops the renderer is being asked to draw right now: `geometry.instanceCount`. */
|
|
107
|
+
readonly instanceCount: number;
|
|
108
|
+
update(options: IRainOptions): void;
|
|
109
|
+
dispose(): void;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The rain pass: one instanced draw of six-vertex quads, added to `scene`.
|
|
114
|
+
*
|
|
115
|
+
* The engine's `GPUParticles3D` was the thing to reach for and it cannot carry this
|
|
116
|
+
* shader: it extends `Sprite`, it overwrites the material's `positionNode` with a storage buffer, and
|
|
117
|
+
* it derives every particle's position from a compute node — there is nowhere to hand it an authored
|
|
118
|
+
* vertex stage that hashes its own drop index, wraps its own cell and projects its own two endpoints
|
|
119
|
+
* by hand. This fills that gap with the smallest thing that holds the maths: an
|
|
120
|
+
* `InstancedBufferGeometry`, so the draw count is the geometry's own `instanceCount` and
|
|
121
|
+
* `instanceIndex` is the study's `floor(gl_VertexID / 6)`.
|
|
122
|
+
*/
|
|
123
|
+
export function createStormRain(scene: Scene, camera: PerspectiveCamera): IStormRain {
|
|
124
|
+
// The uniform block the study passed to every pass. Assigned each frame below, from the same
|
|
125
|
+
// camera basis and the same weather the coast is drawn with.
|
|
126
|
+
const uTime = uniform(0, "float").setGroup(frameGroup);
|
|
127
|
+
const uRain = uniform(0, "float").setGroup(frameGroup);
|
|
128
|
+
const uWind = uniform(0, "float").setGroup(frameGroup);
|
|
129
|
+
const uFlash = uniform(0, "float").setGroup(frameGroup);
|
|
130
|
+
const uAspect = uniform(0, "float").setGroup(frameGroup);
|
|
131
|
+
const uTan = uniform(0, "float").setGroup(frameGroup);
|
|
132
|
+
const uCam = uniform(new Vector3(), "vec3").setGroup(frameGroup);
|
|
133
|
+
const uForward = uniform(new Vector3(), "vec3").setGroup(frameGroup);
|
|
134
|
+
const uRight = uniform(new Vector3(), "vec3").setGroup(frameGroup);
|
|
135
|
+
const uUp = uniform(new Vector3(), "vec3").setGroup(frameGroup);
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The study's `proj`. Its x and y are the ordinary perspective divide for this camera's field of
|
|
139
|
+
* view, and its z becomes the depth the coast's own quad already wrote with these same near and far
|
|
140
|
+
* planes — so a drop behind the headland is hidden by it, which is the one comparison that pass
|
|
141
|
+
* existed for.
|
|
142
|
+
*/
|
|
143
|
+
function proj(p: Node<"vec3">) {
|
|
144
|
+
const v = p.sub(uCam);
|
|
145
|
+
const d = dot(v, uForward);
|
|
146
|
+
const depth = clamp(
|
|
147
|
+
cameraNear
|
|
148
|
+
.add(cameraFar)
|
|
149
|
+
.sub(cameraNear.mul(cameraFar).mul(2).div(d))
|
|
150
|
+
.div(cameraFar.sub(cameraNear))
|
|
151
|
+
.add(1)
|
|
152
|
+
.mul(0.5),
|
|
153
|
+
0,
|
|
154
|
+
1,
|
|
155
|
+
);
|
|
156
|
+
return vec4(dot(v, uRight).div(uAspect.mul(uTan)), dot(v, uUp).div(uTan), depth.mul(d), d);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// --- the vertex stage, transcribed from the study's RAIN_VERTEX --------------------------
|
|
160
|
+
|
|
161
|
+
// The study read each drop's own index out of `gl_VertexID / 6`. Instanced, that division is the
|
|
162
|
+
// instance itself: one instance is one drop's six vertices. Written as one composition rather
|
|
163
|
+
// than a run of `assign`s on a `toVar` variable: `assign` only exists inside a generated `Fn`
|
|
164
|
+
// body, and the whole stage is a single expression anyway, so plain composition is both the
|
|
165
|
+
// legal spelling and the smaller one.
|
|
166
|
+
const id = float(instanceIndex);
|
|
167
|
+
const seed = rnd(id.add(1));
|
|
168
|
+
const seedPos = vec3(
|
|
169
|
+
rnd(id.mul(3.1).add(2))
|
|
170
|
+
.mul(CELL_XZ)
|
|
171
|
+
.sub(CELL_XZ / 2),
|
|
172
|
+
rnd(id.mul(7.3).add(5)).mul(CELL_Y),
|
|
173
|
+
rnd(id.mul(5.7).add(8))
|
|
174
|
+
.mul(CELL_XZ)
|
|
175
|
+
.sub(CELL_XZ / 2),
|
|
176
|
+
);
|
|
177
|
+
const anchor = vec3(
|
|
178
|
+
floor(uCam.x.div(CELL_STEP)).mul(CELL_STEP),
|
|
179
|
+
float(0),
|
|
180
|
+
floor(uCam.z.div(CELL_STEP)).mul(CELL_STEP),
|
|
181
|
+
);
|
|
182
|
+
const speed = float(14).add(seed.mul(8));
|
|
183
|
+
const tm = uTime.add(seed.mul(10));
|
|
184
|
+
|
|
185
|
+
// The fall. Each axis wraps inside the cell, so a drop reappears at the far edge instead of
|
|
186
|
+
// running out; the cell then jumps with the camera, which is what keeps the volume around the
|
|
187
|
+
// viewer rather than around the origin.
|
|
188
|
+
const falling = vec3(
|
|
189
|
+
mod(seedPos.x.add(tm.mul(uWind).mul(20)).add(CELL_XZ / 2), float(CELL_XZ)).sub(CELL_XZ / 2),
|
|
190
|
+
mod(seedPos.y.sub(tm.mul(speed)), float(CELL_Y)).sub(0.45),
|
|
191
|
+
mod(seedPos.z.add(tm.mul(uWind).mul(2)).add(CELL_XZ / 2), float(CELL_XZ)).sub(CELL_XZ / 2),
|
|
192
|
+
).add(anchor);
|
|
193
|
+
// The cell sits at the viewer's feet: however high the camera flies, the drops start five metres
|
|
194
|
+
// below it and fall through the same 30 metres of air.
|
|
195
|
+
const p = vec3(falling.x, falling.y.add(max(uCam.y.sub(5), float(0))), falling.z);
|
|
196
|
+
|
|
197
|
+
// The streak is the drop's own velocity over a thirteenth of a second, so the quad comes out
|
|
198
|
+
// stretched along the fall instead of a square hung at the drop.
|
|
199
|
+
const velocity = vec3(uWind.mul(20), speed.negate(), uWind.mul(2));
|
|
200
|
+
const end = p.sub(velocity.mul(float(0.013).add(seed.mul(0.011))));
|
|
201
|
+
const a = proj(p);
|
|
202
|
+
const b = proj(end);
|
|
203
|
+
|
|
204
|
+
const corner = positionGeometry.xy;
|
|
205
|
+
const dir = normalize(
|
|
206
|
+
b.xy
|
|
207
|
+
.div(max(b.w, float(0.1)))
|
|
208
|
+
.sub(a.xy.div(max(a.w, float(0.1))))
|
|
209
|
+
.mul(screenSize)
|
|
210
|
+
.add(0.0001),
|
|
211
|
+
);
|
|
212
|
+
const side = vec2(dir.y.negate(), dir.x);
|
|
213
|
+
// The width is in pixels and the quad is in clip space, so the offset carries the vertex's own
|
|
214
|
+
// `w`: the study's `... / uRes * c.w * 2`.
|
|
215
|
+
const along = mix(a, b, corner.y);
|
|
216
|
+
const across = side
|
|
217
|
+
.mul(corner.x)
|
|
218
|
+
.mul(mix(float(0.42), float(0.85), seed))
|
|
219
|
+
.div(screenSize)
|
|
220
|
+
.mul(along.w)
|
|
221
|
+
.mul(2);
|
|
222
|
+
const stretched = along.add(vec4(across.x, across.y, float(0), float(0)));
|
|
223
|
+
// An endpoint behind the camera has no direction to stretch along; the study threw the quad off
|
|
224
|
+
// screen rather than letting it collapse to a dot on the near plane.
|
|
225
|
+
const clip = select(a.w.lessThan(0.4).or(b.w.lessThan(0.4)), vec4(2, 2, 2, 1), stretched);
|
|
226
|
+
|
|
227
|
+
// Near drops fade up and far ones fade out, so the volume has no visible wall.
|
|
228
|
+
const opacity = float(0.07)
|
|
229
|
+
.add(seed.mul(0.14))
|
|
230
|
+
.mul(uRain)
|
|
231
|
+
.mul(smoothstep(0.3, 1.8, a.w))
|
|
232
|
+
.mul(float(1).sub(smoothstep(12, 43, a.w)));
|
|
233
|
+
|
|
234
|
+
const geometry = new InstancedBufferGeometry();
|
|
235
|
+
geometry.setAttribute("position", new Float32BufferAttribute(DROP_CORNERS, 3));
|
|
236
|
+
geometry.instanceCount = 0;
|
|
237
|
+
|
|
238
|
+
const material = new MeshBasicNodeMaterial();
|
|
239
|
+
// The quads are wound by the projection, not by the vertex order, so either face can end up front
|
|
240
|
+
// facing; the study's pass drew both.
|
|
241
|
+
material.side = DoubleSide;
|
|
242
|
+
material.transparent = true;
|
|
243
|
+
// Drops are light over a coast that has already written depth: they test it and leave it alone,
|
|
244
|
+
// so streaks never occlude each other or the lightning behind them.
|
|
245
|
+
material.depthWrite = false;
|
|
246
|
+
material.vertexNode = clip;
|
|
247
|
+
|
|
248
|
+
const vAcross = varying(corner.x, "vAcross");
|
|
249
|
+
const vAlong = varying(corner.y, "vAlong");
|
|
250
|
+
const vOpacity = varying(opacity, "vOpacity");
|
|
251
|
+
material.fragmentNode = vec4(
|
|
252
|
+
vec3(0.41, 0.56, 0.68).add(uFlash.mul(vec3(0.35, 0.48, 0.65))),
|
|
253
|
+
pow(max(float(0), float(1).sub(abs(vAcross))), 1.6)
|
|
254
|
+
.mul(sin(vAlong.mul(Math.PI)))
|
|
255
|
+
.mul(vOpacity),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const mesh = new Mesh(geometry, material);
|
|
259
|
+
// The geometry holds one quad's worth of corners at the origin while the drops are projected by
|
|
260
|
+
// the shader, so its own bounds say nothing about where any of them land.
|
|
261
|
+
mesh.frustumCulled = false;
|
|
262
|
+
scene.add(mesh);
|
|
263
|
+
|
|
264
|
+
const forward = new Vector3();
|
|
265
|
+
const right = new Vector3();
|
|
266
|
+
const up = new Vector3();
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
mesh,
|
|
270
|
+
get instanceCount(): number {
|
|
271
|
+
return geometry.instanceCount;
|
|
272
|
+
},
|
|
273
|
+
update({ elapsed, flash, weather, quality }): void {
|
|
274
|
+
geometry.instanceCount = rainInstanceCount(studyTier(quality).rainBudget, weather.rain);
|
|
275
|
+
|
|
276
|
+
// The same basis and the same field of view the coast is drawn with, so a drop and the
|
|
277
|
+
// headland behind it agree on where the horizon is.
|
|
278
|
+
camera.getWorldDirection(forward);
|
|
279
|
+
right.set(1, 0, 0).applyQuaternion(camera.quaternion);
|
|
280
|
+
up.set(0, 1, 0).applyQuaternion(camera.quaternion);
|
|
281
|
+
uCam.value.copy(camera.position);
|
|
282
|
+
uForward.value.copy(forward);
|
|
283
|
+
uRight.value.copy(right);
|
|
284
|
+
uUp.value.copy(up);
|
|
285
|
+
uAspect.value = camera.aspect;
|
|
286
|
+
uTan.value = Math.tan((camera.fov * Math.PI) / 360);
|
|
287
|
+
|
|
288
|
+
uTime.value = elapsed;
|
|
289
|
+
uRain.value = weather.rain;
|
|
290
|
+
uWind.value = weather.wind;
|
|
291
|
+
uFlash.value = flash;
|
|
292
|
+
},
|
|
293
|
+
dispose(): void {
|
|
294
|
+
scene.remove(mesh);
|
|
295
|
+
geometry.dispose();
|
|
296
|
+
material.dispose();
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Generated for you. The sky is game-owned source, not an engine preset.
|
|
2
|
+
//
|
|
3
|
+
// The storm's sky is drawn by the cloud pass (`clouds.ts`) and the haze the coast fades into is
|
|
4
|
+
// drawn by the world pass (`world.ts`); this file decides both colours. The deck's own lighting —
|
|
5
|
+
// the sun's direction — is `lighting.ts`.
|
|
6
|
+
import { Color, type Scene } from "three";
|
|
7
|
+
import * as clouds from "./clouds-shader.js";
|
|
8
|
+
import { palette } from "./palette.js";
|
|
9
|
+
import * as world from "./world-shader.js";
|
|
10
|
+
|
|
11
|
+
/** The clear sky behind the deck, near the horizon and overhead: linear RGB, the study's own. */
|
|
12
|
+
const SKY_LOW = new Color(0.22, 0.3, 0.355);
|
|
13
|
+
const SKY_HIGH = new Color(0.055, 0.1, 0.17);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Writes the sky and haze colours into both passes. Run once, before the first frame. The world
|
|
17
|
+
* quad covers every pixel, so `scene.background` is never seen; it is set to the horizon anyway so
|
|
18
|
+
* a frame that lost the quad still reads as this storm's haze rather than as black.
|
|
19
|
+
*/
|
|
20
|
+
export function setupSky(scene: Scene): void {
|
|
21
|
+
const horizon = new Color(palette.horizon);
|
|
22
|
+
scene.background = horizon.clone();
|
|
23
|
+
const zenith = new Color(palette.zenith);
|
|
24
|
+
world.uHazeLow.value.set(horizon.r, horizon.g, horizon.b);
|
|
25
|
+
world.uHazeHigh.value.set(zenith.r, zenith.g, zenith.b);
|
|
26
|
+
clouds.uSkyLow.value.set(SKY_LOW.r, SKY_LOW.g, SKY_LOW.b);
|
|
27
|
+
clouds.uSkyHigh.value.set(SKY_HIGH.r, SKY_HIGH.g, SKY_HIGH.b);
|
|
28
|
+
}
|