@casmo/tree.js 0.4.0 → 0.5.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/README.md +590 -4
- package/dist/effects/custom.d.ts +62 -0
- package/dist/effects/custom.d.ts.map +1 -0
- package/dist/effects/effects.d.ts +42 -1
- package/dist/effects/effects.d.ts.map +1 -1
- package/dist/effects/index.d.ts +2 -0
- package/dist/effects/index.d.ts.map +1 -1
- package/dist/effects/mask.d.ts +9 -1
- package/dist/effects/mask.d.ts.map +1 -1
- package/dist/effects/tree-id.d.ts.map +1 -1
- package/dist/effects-qHx_8Dlk.js +1052 -0
- package/dist/effects-qHx_8Dlk.js.map +1 -0
- package/dist/effects.js +2 -963
- package/dist/forest/Forest.d.ts +80 -4
- package/dist/forest/Forest.d.ts.map +1 -1
- package/dist/forest/forest-lod.d.ts +70 -0
- package/dist/forest/forest-lod.d.ts.map +1 -0
- package/dist/forest/impostor-material.d.ts +106 -0
- package/dist/forest/impostor-material.d.ts.map +1 -0
- package/dist/forest/impostor-season.d.ts +13 -0
- package/dist/forest/impostor-season.d.ts.map +1 -0
- package/dist/forest/impostor.d.ts +105 -0
- package/dist/forest/impostor.d.ts.map +1 -0
- package/dist/forest/index.d.ts +1 -0
- package/dist/forest/index.d.ts.map +1 -1
- package/dist/forest/levels.d.ts +144 -0
- package/dist/forest/levels.d.ts.map +1 -0
- package/dist/forest/lod.d.ts +106 -0
- package/dist/forest/lod.d.ts.map +1 -0
- package/dist/forest/nodes.d.ts +42 -1
- package/dist/forest/nodes.d.ts.map +1 -1
- package/dist/forest/octahedral.d.ts +33 -0
- package/dist/forest/octahedral.d.ts.map +1 -0
- package/dist/{forest-lg61rhKM.js → forest-CM5DrklM.js} +1723 -340
- package/dist/forest-CM5DrklM.js.map +1 -0
- package/dist/generator/fruit.d.ts +2 -0
- package/dist/generator/fruit.d.ts.map +1 -1
- package/dist/generator/index.d.ts +1 -0
- package/dist/generator/index.d.ts.map +1 -1
- package/dist/generator/light.d.ts +36 -0
- package/dist/generator/light.d.ts.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/materials/crown-nodes.d.ts +5 -0
- package/dist/materials/crown-nodes.d.ts.map +1 -1
- package/dist/materials/leaf-translucency-model.d.ts +6 -1
- package/dist/materials/leaf-translucency-model.d.ts.map +1 -1
- package/dist/materials/material-features.d.ts +12 -0
- package/dist/materials/material-features.d.ts.map +1 -1
- package/dist/materials/shader-hooks.d.ts +155 -0
- package/dist/materials/shader-hooks.d.ts.map +1 -0
- package/dist/motion-Qd7xjeZM.js +276 -0
- package/dist/motion-Qd7xjeZM.js.map +1 -0
- package/dist/nodes-DxgTyF9B.js +436 -0
- package/dist/nodes-DxgTyF9B.js.map +1 -0
- package/dist/react/Forest.d.ts +17 -3
- package/dist/react/Forest.d.ts.map +1 -1
- package/dist/react/ScenePipeline.d.ts +63 -0
- package/dist/react/ScenePipeline.d.ts.map +1 -0
- package/dist/react/SceneShaderHooks.d.ts +25 -0
- package/dist/react/SceneShaderHooks.d.ts.map +1 -0
- package/dist/react/Tree.d.ts +9 -2
- package/dist/react/Tree.d.ts.map +1 -1
- package/dist/react/Weather.d.ts +45 -0
- package/dist/react/Weather.d.ts.map +1 -0
- package/dist/react/Wind.d.ts +12 -1
- package/dist/react/Wind.d.ts.map +1 -1
- package/dist/react/index.d.ts +7 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react.js +347 -10
- package/dist/react.js.map +1 -1
- package/dist/renderer/index.d.ts +6 -0
- package/dist/renderer/index.d.ts.map +1 -1
- package/dist/scene/antialias.d.ts +35 -0
- package/dist/scene/antialias.d.ts.map +1 -0
- package/dist/scene/ao.d.ts +46 -0
- package/dist/scene/ao.d.ts.map +1 -0
- package/dist/scene/bloom.d.ts +19 -0
- package/dist/scene/bloom.d.ts.map +1 -0
- package/dist/scene/fog.d.ts +49 -0
- package/dist/scene/fog.d.ts.map +1 -0
- package/dist/scene/godrays.d.ts +37 -0
- package/dist/scene/godrays.d.ts.map +1 -0
- package/dist/scene/index.d.ts +14 -0
- package/dist/scene/index.d.ts.map +1 -0
- package/dist/scene/motion-blur.d.ts +23 -0
- package/dist/scene/motion-blur.d.ts.map +1 -0
- package/dist/scene/pipeline.d.ts +109 -0
- package/dist/scene/pipeline.d.ts.map +1 -0
- package/dist/scene/settings.d.ts +36 -0
- package/dist/scene/settings.d.ts.map +1 -0
- package/dist/scene/stack.d.ts +25 -0
- package/dist/scene/stack.d.ts.map +1 -0
- package/dist/scene/stage.d.ts +52 -0
- package/dist/scene/stage.d.ts.map +1 -0
- package/dist/scene/suggested.d.ts +12 -0
- package/dist/scene/suggested.d.ts.map +1 -0
- package/dist/scene/tone-mapping.d.ts +20 -0
- package/dist/scene/tone-mapping.d.ts.map +1 -0
- package/dist/scene/velocity.d.ts +34 -0
- package/dist/scene/velocity.d.ts.map +1 -0
- package/dist/scene.js +889 -0
- package/dist/scene.js.map +1 -0
- package/dist/season/nodes.d.ts +13 -0
- package/dist/season/nodes.d.ts.map +1 -1
- package/dist/shader-hooks-BFRtlWGI.js +275 -0
- package/dist/shader-hooks-BFRtlWGI.js.map +1 -0
- package/dist/tree/Tree.d.ts +8 -3
- package/dist/tree/Tree.d.ts.map +1 -1
- package/dist/tree/index.d.ts.map +1 -1
- package/dist/weather/index.d.ts +3 -0
- package/dist/weather/index.d.ts.map +1 -0
- package/dist/weather/shared.d.ts +43 -0
- package/dist/weather/shared.d.ts.map +1 -0
- package/dist/weather/snow.d.ts +87 -0
- package/dist/weather/snow.d.ts.map +1 -0
- package/dist/weather/wetness.d.ts +75 -0
- package/dist/weather/wetness.d.ts.map +1 -0
- package/dist/weather.js +288 -0
- package/dist/weather.js.map +1 -0
- package/dist/wind/Wind.d.ts +100 -5
- package/dist/wind/Wind.d.ts.map +1 -1
- package/dist/wind/burst.d.ts +28 -0
- package/dist/wind/burst.d.ts.map +1 -0
- package/dist/wind/index.d.ts +3 -2
- package/dist/wind/index.d.ts.map +1 -1
- package/dist/wind/motion.d.ts +30 -0
- package/dist/wind/motion.d.ts.map +1 -0
- package/dist/wind/node.d.ts +17 -3
- package/dist/wind/node.d.ts.map +1 -1
- package/package.json +9 -1
- package/dist/effects.js.map +0 -1
- package/dist/forest-lg61rhKM.js.map +0 -1
package/dist/scene.js
ADDED
|
@@ -0,0 +1,889 @@
|
|
|
1
|
+
import { S as treeFromId, a as collectEffectNodes, c as resolveTreeEffects, g as treeEffectsPass, i as channelsKey, l as treeEffectsShape, r as channelsFor, s as liveTreeEffects, u as CustomTreeEffectList, x as readTreeId } from "./effects-qHx_8Dlk.js";
|
|
2
|
+
import { n as windMotionOf, r as windTimeOf } from "./motion-Qd7xjeZM.js";
|
|
3
|
+
import { ACESFilmicToneMapping, AgXToneMapping, CineonToneMapping, Color, LinearToneMapping, NeutralToneMapping, NoToneMapping, ReinhardToneMapping, RenderPipeline, VelocityNode } from "three/webgpu";
|
|
4
|
+
import { Fn, Loop, builtinAOContext, convertToTexture, float, fog, int, modelViewMatrix, mrt, normalView, pass, positionLocal, positionWorld, rangeFogFactor, renderOutput, screenUV, smoothstep, toneMapping, uniform, uv, vec2, vec4 } from "three/tsl";
|
|
5
|
+
import { godrays } from "three/addons/tsl/display/GodraysNode.js";
|
|
6
|
+
import { depthAwareBlend } from "three/addons/tsl/display/depthAwareBlend.js";
|
|
7
|
+
import { ao } from "three/addons/tsl/display/GTAONode.js";
|
|
8
|
+
import { fxaa } from "three/addons/tsl/display/FXAANode.js";
|
|
9
|
+
import { smaa } from "three/addons/tsl/display/SMAANode.js";
|
|
10
|
+
import TRAANode from "three/addons/tsl/display/TRAANode.js";
|
|
11
|
+
import { bloom } from "three/addons/tsl/display/BloomNode.js";
|
|
12
|
+
// src/scene/suggested.ts
|
|
13
|
+
/**
|
|
14
|
+
* The suggested Scene Effects for each built-in Style (plain data: read, copy or change it), tuned
|
|
15
|
+
* against the PR #51 concept images on the `examples/scene-pipeline` scene (a tree 10-15 m away in
|
|
16
|
+
* front of a small Forest, a plain sky, a side sun). TRAA and motion blur are never suggested.
|
|
17
|
+
* SMAA for every Style: the Tree ID channel turns MSAA off (ADR 0009), and SMAA keeps low-poly
|
|
18
|
+
* facets and leaf cards sharper than FXAA. Fog in metres: the front trees stay clear and the trees
|
|
19
|
+
* behind them fade a little; scale the distances up for large worlds.
|
|
20
|
+
*/
|
|
21
|
+
var SUGGESTED_SCENE_EFFECTS = {
|
|
22
|
+
"low-poly": {
|
|
23
|
+
toneMapping: { mapping: "neutral" },
|
|
24
|
+
ao: {
|
|
25
|
+
radius: 1,
|
|
26
|
+
scale: .7,
|
|
27
|
+
thickness: 1
|
|
28
|
+
},
|
|
29
|
+
antialias: "smaa",
|
|
30
|
+
fog: {
|
|
31
|
+
near: 15,
|
|
32
|
+
far: 120
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
stylized: {
|
|
36
|
+
toneMapping: {
|
|
37
|
+
mapping: "agx",
|
|
38
|
+
exposure: 1.2
|
|
39
|
+
},
|
|
40
|
+
ao: {
|
|
41
|
+
radius: 1.2,
|
|
42
|
+
scale: 1.3,
|
|
43
|
+
thickness: 2
|
|
44
|
+
},
|
|
45
|
+
bloom: {
|
|
46
|
+
strength: .1,
|
|
47
|
+
radius: .6,
|
|
48
|
+
threshold: .55
|
|
49
|
+
},
|
|
50
|
+
antialias: "smaa",
|
|
51
|
+
fog: {
|
|
52
|
+
near: 15,
|
|
53
|
+
far: 130,
|
|
54
|
+
mist: {
|
|
55
|
+
height: 1.5,
|
|
56
|
+
strength: .5,
|
|
57
|
+
near: 10,
|
|
58
|
+
far: 50
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
realistic: {
|
|
63
|
+
toneMapping: {
|
|
64
|
+
mapping: "agx",
|
|
65
|
+
exposure: 1.2
|
|
66
|
+
},
|
|
67
|
+
ao: {
|
|
68
|
+
radius: 1.2,
|
|
69
|
+
scale: 1.6,
|
|
70
|
+
thickness: 2
|
|
71
|
+
},
|
|
72
|
+
bloom: {
|
|
73
|
+
strength: .06,
|
|
74
|
+
radius: .3,
|
|
75
|
+
threshold: .55
|
|
76
|
+
},
|
|
77
|
+
godrays: {
|
|
78
|
+
density: .2,
|
|
79
|
+
maxDensity: .05,
|
|
80
|
+
distanceAttenuation: 2,
|
|
81
|
+
color: "#fff0c8"
|
|
82
|
+
},
|
|
83
|
+
antialias: "smaa",
|
|
84
|
+
fog: {
|
|
85
|
+
near: 15,
|
|
86
|
+
far: 140,
|
|
87
|
+
mist: {
|
|
88
|
+
height: 2,
|
|
89
|
+
strength: .4,
|
|
90
|
+
near: 10,
|
|
91
|
+
far: 60,
|
|
92
|
+
farLayer: {
|
|
93
|
+
near: 40,
|
|
94
|
+
far: 200,
|
|
95
|
+
strength: .2
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
// src/scene/settings.ts
|
|
102
|
+
/** The settings of a choice; throws for an unknown Style. */
|
|
103
|
+
function resolveSceneEffects(choice) {
|
|
104
|
+
if (choice === null) return {};
|
|
105
|
+
if (typeof choice !== "string") return choice;
|
|
106
|
+
const settings = SUGGESTED_SCENE_EFFECTS[choice];
|
|
107
|
+
if (!settings) throw new Error(`No suggested Scene Effects for Style "${choice}"`);
|
|
108
|
+
return settings;
|
|
109
|
+
}
|
|
110
|
+
// src/scene/stage.ts
|
|
111
|
+
/**
|
|
112
|
+
* `color` as a texture an effect can sample around each pixel: itself when it already is one,
|
|
113
|
+
* otherwise rendered into one (a render target the caller owns: dispose it when it is not `color`).
|
|
114
|
+
*/
|
|
115
|
+
function asTexture(color) {
|
|
116
|
+
return color.isTextureNode ? color : convertToTexture(color);
|
|
117
|
+
}
|
|
118
|
+
// src/scene/godrays.ts
|
|
119
|
+
/** Why `sun` cannot drive god rays, or `undefined` when it can. */
|
|
120
|
+
function godraysSunProblem(sun) {
|
|
121
|
+
if (!sun) return "no `sun` light was given";
|
|
122
|
+
const { isDirectionalLight, isPointLight } = sun;
|
|
123
|
+
if (!isDirectionalLight && !isPointLight) return "the `sun` must be a DirectionalLight or a PointLight";
|
|
124
|
+
if (!sun.castShadow) return "the `sun` does not cast shadows (the pipeline never turns shadows on)";
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Scene Effect: god rays, light shafts through the canopy, raymarched through the shadow map of a
|
|
128
|
+
* shadow-casting `sun` and blended depth-aware onto `color` (no light leaking around trunks and
|
|
129
|
+
* leaves). Needs a full shadow setup by the app: `renderer.shadowMap.enabled`, the sun's
|
|
130
|
+
* `castShadow` and casting objects; nothing is changed here. `depth` and `camera` are those of the
|
|
131
|
+
* scene pass. Returns a colour node (not display colours).
|
|
132
|
+
*/
|
|
133
|
+
function sceneGodrays(color, depth, camera, sun, options = {}) {
|
|
134
|
+
return liveSceneGodrays(color, depth, camera, sun, options).node;
|
|
135
|
+
}
|
|
136
|
+
function liveSceneGodrays(color, depth, camera, sun, options) {
|
|
137
|
+
const rays = godrays(depth, camera, sun);
|
|
138
|
+
const tint = uniform(new Color());
|
|
139
|
+
const edgeRadius = uniform(2, "int");
|
|
140
|
+
const edgeStrength = uniform(2, "float");
|
|
141
|
+
const update = (o) => {
|
|
142
|
+
rays.density.value = o.density ?? 1.5;
|
|
143
|
+
rays.maxDensity.value = o.maxDensity ?? .5;
|
|
144
|
+
rays.distanceAttenuation.value = o.distanceAttenuation ?? .6;
|
|
145
|
+
tint.value.set(o.color ?? "#fff0c8");
|
|
146
|
+
edgeRadius.value = Math.round(o.edgeRadius ?? 2);
|
|
147
|
+
edgeStrength.value = o.edgeStrength ?? 2;
|
|
148
|
+
};
|
|
149
|
+
update(options);
|
|
150
|
+
const base = asTexture(color);
|
|
151
|
+
const node = depthAwareBlend(base, rays.getTextureNode(), depth, camera, {
|
|
152
|
+
blendColor: tint,
|
|
153
|
+
edgeRadius,
|
|
154
|
+
edgeStrength
|
|
155
|
+
});
|
|
156
|
+
const dispose = () => {
|
|
157
|
+
rays.dispose();
|
|
158
|
+
if (base !== color) base.dispose();
|
|
159
|
+
};
|
|
160
|
+
return {
|
|
161
|
+
node,
|
|
162
|
+
update,
|
|
163
|
+
dispose
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/** Warns that god rays are skipped, and why. */
|
|
167
|
+
function warnGodraysSkipped(problem) {
|
|
168
|
+
console.warn(`tree.js Scene Effects: god rays skipped, ${problem}.`);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The god rays step of the stack. It reads `input.depth`, `input.camera` and `input.sun`; without a
|
|
172
|
+
* usable sun it is skipped with a console warning (the Scene Pipeline leaves it out before, warning once).
|
|
173
|
+
*/
|
|
174
|
+
var godraysStage = {
|
|
175
|
+
shape: (settings) => settings.godrays ? true : void 0,
|
|
176
|
+
build(input, settings) {
|
|
177
|
+
const problem = godraysSunProblem(input.sun) ?? (input.depth && input.camera ? void 0 : "the scene depth and camera are missing");
|
|
178
|
+
if (problem) {
|
|
179
|
+
warnGodraysSkipped(problem);
|
|
180
|
+
return {
|
|
181
|
+
node: input.color,
|
|
182
|
+
update: () => {}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const live = liveSceneGodrays(input.color, input.depth, input.camera, input.sun, settings.godrays);
|
|
186
|
+
return {
|
|
187
|
+
node: live.node,
|
|
188
|
+
update: (s) => live.update(s.godrays),
|
|
189
|
+
dispose: live.dispose
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
// src/scene/ao.ts
|
|
194
|
+
/**
|
|
195
|
+
* Scene Effect: ground-contact ambient occlusion (GTAO). Returns a depth + view-normal pre-pass,
|
|
196
|
+
* GTAO on it and a lighting context; in your own pipeline set `scenePass.contextNode = context`.
|
|
197
|
+
* The scene is drawn twice while AO is on.
|
|
198
|
+
*/
|
|
199
|
+
function sceneAO(scene, camera, options = {}) {
|
|
200
|
+
const { prePass, node, context } = liveSceneAO(scene, camera, options);
|
|
201
|
+
return {
|
|
202
|
+
prePass,
|
|
203
|
+
node,
|
|
204
|
+
context
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/** {@link sceneAO} with `update()` (numbers only) and `dispose()` (the pre-pass and the AO target). */
|
|
208
|
+
function liveSceneAO(scene, camera, options) {
|
|
209
|
+
const prePass = pass(scene, camera);
|
|
210
|
+
prePass.setMRT(mrt({ output: normalView }));
|
|
211
|
+
const node = ao(prePass.getTextureNode("depth"), prePass.getTextureNode(), camera);
|
|
212
|
+
const update = (o) => {
|
|
213
|
+
node.radius.value = o.radius ?? 1;
|
|
214
|
+
node.scale.value = o.scale ?? 1;
|
|
215
|
+
node.thickness.value = o.thickness ?? 1;
|
|
216
|
+
node.distanceExponent.value = o.distanceExponent ?? 1;
|
|
217
|
+
node.distanceFallOff.value = o.distanceFallOff ?? 1;
|
|
218
|
+
node.samples.value = o.samples ?? 16;
|
|
219
|
+
node.resolutionScale = o.resolutionScale ?? 1;
|
|
220
|
+
};
|
|
221
|
+
update(options);
|
|
222
|
+
return {
|
|
223
|
+
prePass,
|
|
224
|
+
node,
|
|
225
|
+
context: builtinAOContext(node.getTextureNode().sample(screenUV).r),
|
|
226
|
+
update,
|
|
227
|
+
dispose() {
|
|
228
|
+
node.dispose();
|
|
229
|
+
prePass.dispose();
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
// src/scene/fog.ts
|
|
234
|
+
/** The fog colour when neither the settings nor a plain-colour background give one. */
|
|
235
|
+
var DEFAULT_COLOR = "#c9d8e2";
|
|
236
|
+
/**
|
|
237
|
+
* Scene Effect: distance fog plus an optional low height mist, as a fog node for `scene.fogNode`
|
|
238
|
+
* (three applies it in every material). `background` is the scene's background: when it is a plain
|
|
239
|
+
* Color, that is the default fog colour, so far trees fade into the sky. Pass `scene.background`.
|
|
240
|
+
*/
|
|
241
|
+
function sceneFog(options = {}, background) {
|
|
242
|
+
return liveSceneFog(options, background).node;
|
|
243
|
+
}
|
|
244
|
+
/** {@link sceneFog} with `update()`: new numbers and colour, the same node (no material recompile). */
|
|
245
|
+
function liveSceneFog(options, background) {
|
|
246
|
+
const color = uniform(new Color());
|
|
247
|
+
const near = uniform(0);
|
|
248
|
+
const far = uniform(0);
|
|
249
|
+
const mistHeight = uniform(0);
|
|
250
|
+
const mistStrength = uniform(0);
|
|
251
|
+
const mistNear = uniform(0);
|
|
252
|
+
const mistFar = uniform(0);
|
|
253
|
+
const layerNear = uniform(0);
|
|
254
|
+
const layerFar = uniform(0);
|
|
255
|
+
const layerStrength = uniform(0);
|
|
256
|
+
const update = (o, bg) => {
|
|
257
|
+
color.value.set(o.color ?? (bg instanceof Color ? bg : DEFAULT_COLOR));
|
|
258
|
+
near.value = o.near ?? 10;
|
|
259
|
+
far.value = o.far ?? 80;
|
|
260
|
+
const mist = o.mist ?? null;
|
|
261
|
+
mistHeight.value = Math.max(mist?.height ?? 2, .001);
|
|
262
|
+
mistStrength.value = mist ? mist.strength ?? .7 : 0;
|
|
263
|
+
mistNear.value = mist?.near ?? 8;
|
|
264
|
+
mistFar.value = mist?.far ?? 40;
|
|
265
|
+
const layer = mist?.farLayer ?? null;
|
|
266
|
+
layerNear.value = layer?.near ?? 30;
|
|
267
|
+
layerFar.value = layer?.far ?? 120;
|
|
268
|
+
layerStrength.value = layer ? layer.strength ?? .4 : 0;
|
|
269
|
+
};
|
|
270
|
+
update(options, background);
|
|
271
|
+
const distance = rangeFogFactor(near, far);
|
|
272
|
+
const mist = rangeFogFactor(mistNear, mistFar).mul(smoothstep(mistHeight, 0, positionWorld.y)).mul(mistStrength);
|
|
273
|
+
const layer = rangeFogFactor(layerNear, layerFar).mul(layerStrength);
|
|
274
|
+
const factor = distance.oneMinus().mul(mist.oneMinus()).mul(layer.oneMinus()).oneMinus().clamp(0, 1);
|
|
275
|
+
return {
|
|
276
|
+
node: fog(color, factor),
|
|
277
|
+
update
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
// src/scene/velocity.ts
|
|
281
|
+
/**
|
|
282
|
+
* Name of the velocity output in a pass's MRT (`scenePass.getTextureNode(WIND_VELOCITY)`). It is
|
|
283
|
+
* `'velocity'` on purpose: three only keeps the previous frame's data (instance matrices, bones)
|
|
284
|
+
* for a scene pass whose MRT has an output of that name.
|
|
285
|
+
*/
|
|
286
|
+
var WIND_VELOCITY = "velocity";
|
|
287
|
+
/**
|
|
288
|
+
* The Wind time step behind a pinned Wind time (a still), and before a tree's first draw: one
|
|
289
|
+
* frame at 60 fps. A pinned clock thus still shows the Wind's sway as motion, deterministically.
|
|
290
|
+
*/
|
|
291
|
+
var STILL_STEP = 1 / 60;
|
|
292
|
+
/** The Wind time each object was last drawn with in a velocity pass. */
|
|
293
|
+
var lastWindTimes = /* @__PURE__ */ new WeakMap();
|
|
294
|
+
/** The Wind time a tree drawn at Wind time `now` was at in the previous frame (see {@link STILL_STEP}). */
|
|
295
|
+
function previousWindTime(last, now) {
|
|
296
|
+
return last === void 0 || last === now ? now - STILL_STEP : last;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* three's `velocity` (screen-space motion since the previous frame: camera, object and instance
|
|
300
|
+
* movement), plus the Wind sway of tree.js trees: for a tree material it moves each vertex back by
|
|
301
|
+
* the Wind displacement between the previous frame's Wind time and now (the same Wind formula, with
|
|
302
|
+
* each Forest tree's own phase). Growth and Season are not motion. Other materials get three's
|
|
303
|
+
* velocity unchanged. The tree materials themselves are never changed (ADR 0011).
|
|
304
|
+
*/
|
|
305
|
+
var WindVelocityNode = class extends VelocityNode {
|
|
306
|
+
/** The previous frame's Wind time of the tree being drawn (set per object). */
|
|
307
|
+
previousWindTime = uniform(0);
|
|
308
|
+
update(frame) {
|
|
309
|
+
super.update(frame);
|
|
310
|
+
const motion = motionOf(frame);
|
|
311
|
+
if (!motion) return void 0;
|
|
312
|
+
const now = windTimeOf(motion.source, frame.time);
|
|
313
|
+
this.previousWindTime.value = previousWindTime(lastWindTimes.get(frame.object), now);
|
|
314
|
+
}
|
|
315
|
+
updateAfter(frame) {
|
|
316
|
+
super.updateAfter(frame);
|
|
317
|
+
const motion = motionOf(frame);
|
|
318
|
+
if (motion) lastWindTimes.set(frame.object, windTimeOf(motion.source, frame.time));
|
|
319
|
+
}
|
|
320
|
+
setup(builder) {
|
|
321
|
+
const motion = windMotionOf(positionNodeOf(builder.material));
|
|
322
|
+
if (!motion) return super.setup(builder);
|
|
323
|
+
const previousLocal = positionLocal.add(motion.shift(this.previousWindTime).toVarying("v_windVelocityShift"));
|
|
324
|
+
const previousModelView = this.previousCameraViewMatrix.mul(this.previousModelWorldMatrix);
|
|
325
|
+
const current = this.currentProjectionMatrix.mul(modelViewMatrix).mul(positionLocal);
|
|
326
|
+
const previous = this.previousProjectionMatrix.mul(previousModelView).mul(previousLocal);
|
|
327
|
+
return current.xy.div(current.w).sub(previous.xy.div(previous.w));
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
function motionOf(frame) {
|
|
331
|
+
return frame.object ? windMotionOf(positionNodeOf(frame.material)) : void 0;
|
|
332
|
+
}
|
|
333
|
+
function positionNodeOf(material) {
|
|
334
|
+
return material?.positionNode;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* TSL node: Wind-aware velocity (vec2, NDC units per frame), a drop-in for three's `velocity` that
|
|
338
|
+
* also sees the Wind sway of tree.js trees. Use it through {@link windVelocityMRT}.
|
|
339
|
+
*/
|
|
340
|
+
var windVelocity = new WindVelocityNode();
|
|
341
|
+
/**
|
|
342
|
+
* The MRT output that renders Wind-aware velocity ({@link windVelocity}) to a target named
|
|
343
|
+
* `velocity` (cleared to 0), for TRAA or motion blur in your own pipeline:
|
|
344
|
+
* `scenePass.setMRT(mrt({ output }).merge(windVelocityMRT()))`, then
|
|
345
|
+
* `scenePass.getTextureNode(WIND_VELOCITY)`. Only a scene pass with this output pays for velocity:
|
|
346
|
+
* without it, trees, their geometry and their materials render exactly as before.
|
|
347
|
+
*/
|
|
348
|
+
function windVelocityMRT() {
|
|
349
|
+
const node = mrt({ [WIND_VELOCITY]: windVelocity });
|
|
350
|
+
node.setClearColor(WIND_VELOCITY, 0, 0);
|
|
351
|
+
return node;
|
|
352
|
+
}
|
|
353
|
+
/** `treeEffectsPass` with the Scene Pipeline's channels (the velocity MRT merged in when asked for). */
|
|
354
|
+
function createScenePass(scene, camera, selection, channels) {
|
|
355
|
+
const { velocity, ...treeChannels } = channels;
|
|
356
|
+
const input = treeEffectsPass(scene, camera, selection, treeChannels);
|
|
357
|
+
if (velocity) {
|
|
358
|
+
input.pass.setMRT(input.pass.getMRT().merge(windVelocityMRT()));
|
|
359
|
+
input.velocity = input.pass.getTextureNode(WIND_VELOCITY);
|
|
360
|
+
}
|
|
361
|
+
return input;
|
|
362
|
+
}
|
|
363
|
+
// src/scene/antialias.ts
|
|
364
|
+
var NODES = {
|
|
365
|
+
smaa: (color) => smaa(color),
|
|
366
|
+
fxaa: (color) => fxaa(color)
|
|
367
|
+
};
|
|
368
|
+
function modeOf(mode) {
|
|
369
|
+
if (mode !== "traa" && !(mode in NODES)) throw new Error(`Unknown anti-aliasing "${mode}"`);
|
|
370
|
+
return mode;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Scene Effect: anti-aliasing. SMAA and FXAA run on display colours: pass them the tone mapped
|
|
374
|
+
* colour in the output colour space (e.g. the output of {@link sceneToneMapping}, or
|
|
375
|
+
* `renderOutput(color)`), and set `pipeline.outputColorTransform = false`. TRAA runs on linear
|
|
376
|
+
* colours, before tone mapping, and needs `input`: the scene pass's depth, its velocity (from
|
|
377
|
+
* `windVelocityMRT()`, so swaying leaves don't ghost) and the camera (which it jitters while the
|
|
378
|
+
* RenderPipeline renders).
|
|
379
|
+
*
|
|
380
|
+
* Side effect of `'traa'`: it sets `options.samples = 0` on the scene pass `depth` comes from (your
|
|
381
|
+
* `pass(scene, camera)`), turning its MSAA off for good: TRAA copies the depth each frame, which a
|
|
382
|
+
* multisampled depth cannot be (TRAA anti-aliases instead). The Scene Pipeline restores the old value
|
|
383
|
+
* when TRAA goes off; with this function, set `samples` back yourself if you drop TRAA.
|
|
384
|
+
*/
|
|
385
|
+
function sceneAntialias(color, mode, input) {
|
|
386
|
+
return mode === "traa" ? liveSceneTRAA(color, input ?? {}).node : liveSceneAntialias(color, mode).node;
|
|
387
|
+
}
|
|
388
|
+
function liveSceneAntialias(color, mode) {
|
|
389
|
+
const node = NODES[modeOf(mode)](color);
|
|
390
|
+
return {
|
|
391
|
+
node,
|
|
392
|
+
dispose: () => {
|
|
393
|
+
node.dispose();
|
|
394
|
+
if (node.textureNode !== color) node.textureNode.dispose();
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* three's TRAA, which also keeps the Wind-aware velocity unjittered: TRAA gives three's own
|
|
400
|
+
* `velocity` the unjittered projection while it jitters the camera; {@link windVelocity} is another
|
|
401
|
+
* VelocityNode, so it gets the same (otherwise the jitter itself would read as motion).
|
|
402
|
+
*/
|
|
403
|
+
var WindTRAANode = class extends TRAANode {
|
|
404
|
+
setViewOffset(width, height) {
|
|
405
|
+
super.setViewOffset(width, height);
|
|
406
|
+
windVelocity.setProjectionMatrix(this._originalProjectionMatrix);
|
|
407
|
+
}
|
|
408
|
+
clearViewOffset() {
|
|
409
|
+
super.clearViewOffset();
|
|
410
|
+
windVelocity.setProjectionMatrix(null);
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
function liveSceneTRAA(color, { depth, velocity, camera }) {
|
|
414
|
+
const missing = Object.entries({
|
|
415
|
+
depth,
|
|
416
|
+
velocity,
|
|
417
|
+
camera
|
|
418
|
+
}).filter(([, value]) => !value).map(([name]) => name);
|
|
419
|
+
if (missing.length) throw new Error(`TRAA ("traa" anti-aliasing) needs the scene pass's ${missing.join(", ")}: pass { depth, velocity, camera } (velocity from windVelocityMRT())`);
|
|
420
|
+
const scenePass = depth.passNode;
|
|
421
|
+
const samples = scenePass?.options.samples;
|
|
422
|
+
if (scenePass) scenePass.options.samples = 0;
|
|
423
|
+
const beauty = convertToTexture(color);
|
|
424
|
+
const node = new WindTRAANode(beauty, depth, velocity, camera);
|
|
425
|
+
return {
|
|
426
|
+
node,
|
|
427
|
+
dispose: () => {
|
|
428
|
+
node.dispose();
|
|
429
|
+
if (beauty !== color) beauty.dispose();
|
|
430
|
+
if (scenePass) scenePass.options.samples = samples;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* The TRAA step of the stack, before tone mapping (as three requires), on the scene pass's depth and
|
|
436
|
+
* Wind-aware velocity (the Scene Pipeline turns the velocity channel on for it).
|
|
437
|
+
*/
|
|
438
|
+
var traaStage = {
|
|
439
|
+
shape: (settings) => settings.antialias === "traa" ? "traa" : void 0,
|
|
440
|
+
build(input) {
|
|
441
|
+
const live = liveSceneTRAA(input.color, input);
|
|
442
|
+
return {
|
|
443
|
+
node: live.node,
|
|
444
|
+
update: () => {},
|
|
445
|
+
dispose: live.dispose
|
|
446
|
+
};
|
|
447
|
+
},
|
|
448
|
+
needsVelocity: true
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* The SMAA / FXAA step of the stack, last (after tone mapping). Without tone mapping it applies the
|
|
452
|
+
* renderer's output transform itself, so it always runs on (and outputs) display colours.
|
|
453
|
+
*/
|
|
454
|
+
var antialiasStage = {
|
|
455
|
+
shape: (settings) => {
|
|
456
|
+
const mode = settings.antialias;
|
|
457
|
+
return mode && mode !== "traa" ? [modeOf(mode), !!settings.toneMapping] : void 0;
|
|
458
|
+
},
|
|
459
|
+
build(input, settings) {
|
|
460
|
+
const live = liveSceneAntialias(settings.toneMapping ? input.color : renderOutput(input.color), settings.antialias);
|
|
461
|
+
return {
|
|
462
|
+
node: live.node,
|
|
463
|
+
update: () => {},
|
|
464
|
+
dispose: live.dispose
|
|
465
|
+
};
|
|
466
|
+
},
|
|
467
|
+
display: true
|
|
468
|
+
};
|
|
469
|
+
// src/scene/bloom.ts
|
|
470
|
+
var DEFAULTS$1 = {
|
|
471
|
+
strength: .3,
|
|
472
|
+
radius: .5,
|
|
473
|
+
threshold: .6
|
|
474
|
+
};
|
|
475
|
+
/**
|
|
476
|
+
* Scene Effect: bloom, a soft glare around the bright (HDR) parts of the frame, added onto the
|
|
477
|
+
* colour. Use it on linear colours before tone mapping.
|
|
478
|
+
*/
|
|
479
|
+
function sceneBloom(color, options = {}) {
|
|
480
|
+
return liveSceneBloom(color, options).node;
|
|
481
|
+
}
|
|
482
|
+
function liveSceneBloom(color, options) {
|
|
483
|
+
const strength = uniform(0);
|
|
484
|
+
const radius = uniform(0);
|
|
485
|
+
const threshold = uniform(0);
|
|
486
|
+
const update = (o) => {
|
|
487
|
+
strength.value = o.strength ?? DEFAULTS$1.strength;
|
|
488
|
+
radius.value = o.radius ?? DEFAULTS$1.radius;
|
|
489
|
+
threshold.value = o.threshold ?? DEFAULTS$1.threshold;
|
|
490
|
+
};
|
|
491
|
+
update(options);
|
|
492
|
+
const glare = bloom(color, strength, radius, threshold);
|
|
493
|
+
return {
|
|
494
|
+
node: vec4(color.rgb.add(glare.rgb), color.a),
|
|
495
|
+
update,
|
|
496
|
+
dispose: () => glare.dispose()
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
/** The bloom step of the stack (one graph for all settings: every number is a uniform). */
|
|
500
|
+
var bloomStage = {
|
|
501
|
+
shape: (settings) => settings.bloom ? true : void 0,
|
|
502
|
+
build(input, settings) {
|
|
503
|
+
const live = liveSceneBloom(input.color, settings.bloom);
|
|
504
|
+
return {
|
|
505
|
+
node: live.node,
|
|
506
|
+
update: (s) => live.update(s.bloom),
|
|
507
|
+
dispose: live.dispose
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
// src/scene/motion-blur.ts
|
|
512
|
+
var DEFAULTS = {
|
|
513
|
+
strength: 1,
|
|
514
|
+
samples: 16
|
|
515
|
+
};
|
|
516
|
+
/**
|
|
517
|
+
* Scene Effect: motion blur. Smears each pixel along its screen-space motion, read from `velocity`
|
|
518
|
+
* (NDC units per frame, as three's `velocity`). Use the scene pass's Wind-aware velocity
|
|
519
|
+
* (`windVelocityMRT()`, or `scenePass.velocity` of a Scene Pipeline with motion blur), so that Wind
|
|
520
|
+
* sway blurs as well as camera and object motion. Use it on linear colours, after god rays and
|
|
521
|
+
* before bloom. Returns a colour node.
|
|
522
|
+
*/
|
|
523
|
+
function sceneMotionBlur(color, velocity, options = {}) {
|
|
524
|
+
return liveSceneMotionBlur(color, velocity, options).node;
|
|
525
|
+
}
|
|
526
|
+
function liveSceneMotionBlur(color, velocity, options) {
|
|
527
|
+
if (!velocity) throw new Error("tree.js sceneMotionBlur: needs a velocity channel (e.g. a scene pass with windVelocityMRT()).");
|
|
528
|
+
const strength = uniform(0);
|
|
529
|
+
const samples = uniform(0, "int");
|
|
530
|
+
const update = (o) => {
|
|
531
|
+
strength.value = o.strength ?? DEFAULTS.strength;
|
|
532
|
+
samples.value = Math.max(1, Math.round(o.samples ?? DEFAULTS.samples));
|
|
533
|
+
};
|
|
534
|
+
update(options);
|
|
535
|
+
const base = asTexture(color);
|
|
536
|
+
const node = Fn(() => {
|
|
537
|
+
const coord = uv();
|
|
538
|
+
const streak = velocity.sample(coord).xy.mul(vec2(.5, -.5)).mul(strength);
|
|
539
|
+
const sum = vec4(0).toVar();
|
|
540
|
+
Loop({
|
|
541
|
+
start: int(0),
|
|
542
|
+
end: samples,
|
|
543
|
+
type: "int",
|
|
544
|
+
condition: "<"
|
|
545
|
+
}, ({ i }) => {
|
|
546
|
+
sum.addAssign(base.sample(coord.add(streak.mul(float(i).add(.5).div(float(samples)).sub(.5)))));
|
|
547
|
+
});
|
|
548
|
+
return sum.div(float(samples));
|
|
549
|
+
})();
|
|
550
|
+
const dispose = () => {
|
|
551
|
+
if (base !== color) base.dispose();
|
|
552
|
+
};
|
|
553
|
+
return {
|
|
554
|
+
node,
|
|
555
|
+
update,
|
|
556
|
+
dispose
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
/** The motion blur step of the stack: reads `input.velocity` (the Scene Pipeline requests it). */
|
|
560
|
+
var motionBlurStage = {
|
|
561
|
+
shape: (settings) => settings.motionBlur ? true : void 0,
|
|
562
|
+
build(input, settings) {
|
|
563
|
+
const live = liveSceneMotionBlur(input.color, input.velocity, settings.motionBlur);
|
|
564
|
+
return {
|
|
565
|
+
node: live.node,
|
|
566
|
+
update: (s) => live.update(s.motionBlur),
|
|
567
|
+
dispose: live.dispose
|
|
568
|
+
};
|
|
569
|
+
},
|
|
570
|
+
needsVelocity: true
|
|
571
|
+
};
|
|
572
|
+
// src/scene/tone-mapping.ts
|
|
573
|
+
var CURVES = {
|
|
574
|
+
agx: AgXToneMapping,
|
|
575
|
+
neutral: NeutralToneMapping,
|
|
576
|
+
aces: ACESFilmicToneMapping,
|
|
577
|
+
reinhard: ReinhardToneMapping,
|
|
578
|
+
cineon: CineonToneMapping,
|
|
579
|
+
linear: LinearToneMapping
|
|
580
|
+
};
|
|
581
|
+
function curveOf(options) {
|
|
582
|
+
const mapping = options.mapping ?? "agx";
|
|
583
|
+
if (!(mapping in CURVES)) throw new Error(`Unknown tone mapping "${mapping}"`);
|
|
584
|
+
return mapping;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Scene Effect: tone mapping plus the conversion to the renderer's output colour space, inside the
|
|
588
|
+
* pipeline (the renderer's `toneMapping` is not used or changed). Returns display colours: in your
|
|
589
|
+
* own `RenderPipeline`, set `pipeline.outputColorTransform = false` (as for three's `renderOutput`).
|
|
590
|
+
*/
|
|
591
|
+
function sceneToneMapping(color, options = {}) {
|
|
592
|
+
return liveSceneToneMapping(color, options).node;
|
|
593
|
+
}
|
|
594
|
+
function liveSceneToneMapping(color, options) {
|
|
595
|
+
const exposure = uniform(1);
|
|
596
|
+
const update = (o) => {
|
|
597
|
+
exposure.value = o.exposure ?? 1;
|
|
598
|
+
};
|
|
599
|
+
update(options);
|
|
600
|
+
const mapped = toneMapping(CURVES[curveOf(options)], exposure, color);
|
|
601
|
+
return {
|
|
602
|
+
node: renderOutput(mapped, NoToneMapping, null),
|
|
603
|
+
update
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
// src/scene/stack.ts
|
|
607
|
+
/**
|
|
608
|
+
* The colour steps of the Scene Effects, in stack order. Fog (in the materials) and ambient
|
|
609
|
+
* occlusion (in the scene pass's lighting) are not colour steps; the Tree Effects come before all
|
|
610
|
+
* of these. Order: god rays -> motion blur -> bloom -> (TRAA) -> tone mapping + output colour
|
|
611
|
+
* space -> SMAA / FXAA.
|
|
612
|
+
*/
|
|
613
|
+
var STACK = [
|
|
614
|
+
godraysStage,
|
|
615
|
+
motionBlurStage,
|
|
616
|
+
bloomStage,
|
|
617
|
+
traaStage,
|
|
618
|
+
{
|
|
619
|
+
shape: (settings) => settings.toneMapping ? curveOf(settings.toneMapping) : void 0,
|
|
620
|
+
build(input, settings) {
|
|
621
|
+
const live = liveSceneToneMapping(input.color, settings.toneMapping);
|
|
622
|
+
return {
|
|
623
|
+
node: live.node,
|
|
624
|
+
update: (s) => live.update(s.toneMapping)
|
|
625
|
+
};
|
|
626
|
+
},
|
|
627
|
+
display: true
|
|
628
|
+
},
|
|
629
|
+
antialiasStage
|
|
630
|
+
];
|
|
631
|
+
/**
|
|
632
|
+
* The shape of a combination: equal for settings whose graphs are the same (that differ only in
|
|
633
|
+
* uniforms).
|
|
634
|
+
*/
|
|
635
|
+
function sceneEffectsShape(settings) {
|
|
636
|
+
return JSON.stringify(STACK.map((stage) => stage.shape(settings) ?? null));
|
|
637
|
+
}
|
|
638
|
+
/** Whether the steps that are on read the Wind-aware velocity channel (`input.velocity`). */
|
|
639
|
+
function sceneEffectsNeedVelocity(settings) {
|
|
640
|
+
return STACK.some((stage) => stage.needsVelocity === true && stage.shape(settings) !== void 0);
|
|
641
|
+
}
|
|
642
|
+
/** {@link applySceneEffects} with every colour and number as a uniform; `update()` needs the same shape. */
|
|
643
|
+
function liveSceneEffects(input, settings) {
|
|
644
|
+
const built = [];
|
|
645
|
+
let color = input.color;
|
|
646
|
+
let display = false;
|
|
647
|
+
for (const stage of STACK) {
|
|
648
|
+
if (stage.shape(settings) === void 0) continue;
|
|
649
|
+
const live = stage.build({
|
|
650
|
+
...input,
|
|
651
|
+
color
|
|
652
|
+
}, settings);
|
|
653
|
+
built.push(live);
|
|
654
|
+
color = live.node;
|
|
655
|
+
display ||= stage.display === true;
|
|
656
|
+
}
|
|
657
|
+
return {
|
|
658
|
+
node: color,
|
|
659
|
+
display,
|
|
660
|
+
update: (s) => built.forEach((live) => live.update(s)),
|
|
661
|
+
dispose: () => built.forEach((live) => live.dispose?.())
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Applies a combination of Scene Effects to `input.color` (e.g. the output of `applyTreeEffects`)
|
|
666
|
+
* and returns the resulting colour node, for your own pipeline. Empty settings return
|
|
667
|
+
* `input.color`. With tone mapping the result is display colours: set
|
|
668
|
+
* `pipeline.outputColorTransform = false`.
|
|
669
|
+
*/
|
|
670
|
+
function applySceneEffects(input, settings) {
|
|
671
|
+
return liveSceneEffects(input, settings).node;
|
|
672
|
+
}
|
|
673
|
+
// src/scene/pipeline.ts
|
|
674
|
+
function resolveChoice(choice) {
|
|
675
|
+
const style = choice.style ?? null;
|
|
676
|
+
return {
|
|
677
|
+
treeEffects: resolveTreeEffects(choice.treeEffects !== void 0 ? choice.treeEffects : style),
|
|
678
|
+
sceneEffects: resolveSceneEffects(choice.sceneEffects !== void 0 ? choice.sceneEffects : style)
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
/** Nothing is on: every part is left out, `undefined` or `null` (e.g. `{ antialias: null }`). */
|
|
682
|
+
function isEmpty(settings) {
|
|
683
|
+
return [settings.treeEffects, settings.sceneEffects].every((group) => Object.values(group).every((part) => part == null));
|
|
684
|
+
}
|
|
685
|
+
/** The scene pass channels the settings need (with the `treeId` and `velocity` options and the custom effects' channels). */
|
|
686
|
+
function channelsOf(settings, options, custom) {
|
|
687
|
+
return {
|
|
688
|
+
...channelsFor(settings.treeEffects, options, custom),
|
|
689
|
+
velocity: wantsVelocity(settings, options)
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
/** Whether the scene pass renders Wind-aware velocity (ADR 0011): only when something reads it. */
|
|
693
|
+
function wantsVelocity(settings, options) {
|
|
694
|
+
return options.velocity === true || sceneEffectsNeedVelocity(settings.sceneEffects);
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* The Scene Pipeline: renders the scene with Tree Effects and Scene Effects in one `RenderPipeline`.
|
|
698
|
+
* A superset of `TreeEffects` (use it instead). It never changes the renderer (tone mapping and the
|
|
699
|
+
* output colour space are applied inside the pipeline). With nothing set it adds nothing: the
|
|
700
|
+
* same image as `renderer.render(scene, camera)`.
|
|
701
|
+
*/
|
|
702
|
+
var ScenePipeline = class {
|
|
703
|
+
renderer;
|
|
704
|
+
scene;
|
|
705
|
+
camera;
|
|
706
|
+
options;
|
|
707
|
+
pipeline;
|
|
708
|
+
/** The trees the Highlight marks (the `highlighted` option, or an empty list): change it in place. */
|
|
709
|
+
highlighted;
|
|
710
|
+
current = {
|
|
711
|
+
treeEffects: {},
|
|
712
|
+
sceneEffects: {}
|
|
713
|
+
};
|
|
714
|
+
custom = new CustomTreeEffectList();
|
|
715
|
+
graph = null;
|
|
716
|
+
input;
|
|
717
|
+
/** Nothing is on (no effects, no extra channel): render with `renderer.render()` itself. */
|
|
718
|
+
passThrough = true;
|
|
719
|
+
/** The pipeline's fog while it is on, and the app's `scene.fogNode` it replaced (restored when off). */
|
|
720
|
+
fog = null;
|
|
721
|
+
/** God rays were skipped for want of a shadow-casting sun, and that was said once. */
|
|
722
|
+
warnedSun = false;
|
|
723
|
+
constructor(renderer, scene, camera, options = {}) {
|
|
724
|
+
this.renderer = renderer;
|
|
725
|
+
this.scene = scene;
|
|
726
|
+
this.camera = camera;
|
|
727
|
+
this.options = options;
|
|
728
|
+
this.pipeline = new RenderPipeline(renderer);
|
|
729
|
+
this.highlighted = options.highlighted ?? [];
|
|
730
|
+
this.set(options);
|
|
731
|
+
}
|
|
732
|
+
/** The scene pass; replaced (the old one disposed) when {@link set} needs another set of channels. */
|
|
733
|
+
get scenePass() {
|
|
734
|
+
return this.input.pass;
|
|
735
|
+
}
|
|
736
|
+
/** The active Tree Effects and Scene Effects. */
|
|
737
|
+
get settings() {
|
|
738
|
+
return this.current;
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Switches Style or settings (the same rules as the options). With the same parts on, only
|
|
742
|
+
* uniforms change (no recompile); otherwise a new graph is built and the old one freed.
|
|
743
|
+
*/
|
|
744
|
+
set(choice) {
|
|
745
|
+
this.current = resolveChoice(choice);
|
|
746
|
+
this.rebuild();
|
|
747
|
+
}
|
|
748
|
+
/** The app's own Tree Effects, in the order they were added (see {@link addEffect}). */
|
|
749
|
+
get customEffects() {
|
|
750
|
+
return this.custom.effects;
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Adds a Tree Effect of the app's own (as `TreeEffects.addEffect()`): it runs at the place it names
|
|
754
|
+
* among the built-in Tree Effects, so before every Scene Effect. It stays through {@link set} until
|
|
755
|
+
* {@link removeEffect}. Builds a new graph, and a new scene pass if it declares a channel that is not on yet.
|
|
756
|
+
*/
|
|
757
|
+
addEffect(effect, ...[options]) {
|
|
758
|
+
this.custom.add(effect, options);
|
|
759
|
+
this.rebuild();
|
|
760
|
+
}
|
|
761
|
+
/** Changes the options of an added custom effect live, through its `update()` (no new graph, no recompile). */
|
|
762
|
+
updateEffect(effect, options) {
|
|
763
|
+
this.custom.update(effect, options);
|
|
764
|
+
}
|
|
765
|
+
/** Removes a custom effect (nothing happens if it is not added); its channels go when nothing else needs them. */
|
|
766
|
+
removeEffect(effect) {
|
|
767
|
+
if (this.custom.remove(effect)) this.rebuild();
|
|
768
|
+
}
|
|
769
|
+
/** Brings the scene pass, the graph and the fog in line with the settings and the custom effects. */
|
|
770
|
+
rebuild() {
|
|
771
|
+
const channels = channelsKey(channelsOf(this.current, this.options, this.custom));
|
|
772
|
+
if (this.input?.channels !== channels) {
|
|
773
|
+
this.disposeGraph();
|
|
774
|
+
this.input?.pass.pass.dispose();
|
|
775
|
+
this.input = {
|
|
776
|
+
channels,
|
|
777
|
+
pass: createScenePass(this.scene, this.camera, this.options.selection ?? null, channelsOf(this.current, this.options, this.custom))
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
const sceneEffects = this.usableSceneEffects(this.current.sceneEffects);
|
|
781
|
+
this.passThrough = channels === "" && isEmpty({
|
|
782
|
+
...this.current,
|
|
783
|
+
sceneEffects
|
|
784
|
+
}) && this.custom.effects.length === 0;
|
|
785
|
+
this.setFog();
|
|
786
|
+
const { ao: aoSettings } = sceneEffects;
|
|
787
|
+
const shape = JSON.stringify([
|
|
788
|
+
treeEffectsShape(this.current.treeEffects, this.scenePass, this.custom),
|
|
789
|
+
sceneEffectsShape(sceneEffects),
|
|
790
|
+
aoSettings !== void 0
|
|
791
|
+
]);
|
|
792
|
+
if (this.graph?.shape === shape) {
|
|
793
|
+
this.graph.tree.update(this.current.treeEffects);
|
|
794
|
+
this.graph.scene.update(sceneEffects);
|
|
795
|
+
if (aoSettings) this.graph.ao?.update(aoSettings);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
this.disposeGraph();
|
|
799
|
+
const tree = liveTreeEffects(this.scenePass, this.current.treeEffects, this.highlighted, this.custom);
|
|
800
|
+
const scene = liveSceneEffects({
|
|
801
|
+
...this.scenePass,
|
|
802
|
+
color: tree.node,
|
|
803
|
+
sun: this.options.sun
|
|
804
|
+
}, sceneEffects);
|
|
805
|
+
const ao = aoSettings ? liveSceneAO(this.scene, this.camera, aoSettings) : null;
|
|
806
|
+
this.scenePass.pass.contextNode = ao?.context ?? null;
|
|
807
|
+
this.scenePass.pass.needsUpdate = true;
|
|
808
|
+
this.graph = {
|
|
809
|
+
shape,
|
|
810
|
+
tree,
|
|
811
|
+
scene,
|
|
812
|
+
treeNodes: collectEffectNodes(tree.node, this.scenePass.color),
|
|
813
|
+
ao
|
|
814
|
+
};
|
|
815
|
+
this.pipeline.outputNode = scene.node;
|
|
816
|
+
this.pipeline.outputColorTransform = !scene.display;
|
|
817
|
+
this.pipeline.needsUpdate = true;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* GPU picking, as `TreeEffects.pick()`: the Tree or Forest tree under a canvas point (CSS pixels)
|
|
821
|
+
* in the last render. Needs the Tree ID channel (the `treeId: true` option).
|
|
822
|
+
*/
|
|
823
|
+
pick(x, y) {
|
|
824
|
+
const { pass, treeId } = this.scenePass;
|
|
825
|
+
if (!treeId) throw new Error("ScenePipeline.pick() needs the Tree ID channel: create the pipeline with the `treeId: true` option");
|
|
826
|
+
return readTreeId(this.renderer, pass, x, y).then(treeFromId);
|
|
827
|
+
}
|
|
828
|
+
/** Renders the scene (call instead of `renderer.render(scene, camera)`). */
|
|
829
|
+
render() {
|
|
830
|
+
if (this.passThrough) this.renderer.render(this.scene, this.camera);
|
|
831
|
+
else this.pipeline.render();
|
|
832
|
+
}
|
|
833
|
+
dispose() {
|
|
834
|
+
this.restoreFog();
|
|
835
|
+
this.disposeGraph();
|
|
836
|
+
this.scenePass.pass.dispose();
|
|
837
|
+
this.pipeline.dispose();
|
|
838
|
+
}
|
|
839
|
+
/** Fog is the one scene change (ADR 0010): take over `scene.fogNode`, remembering the app's value. */
|
|
840
|
+
setFog() {
|
|
841
|
+
const settings = this.current.sceneEffects.fog;
|
|
842
|
+
if (!settings) return this.restoreFog();
|
|
843
|
+
if (this.fog) return this.fog.live.update(settings, this.scene.background);
|
|
844
|
+
const live = liveSceneFog(settings, this.scene.background);
|
|
845
|
+
this.fog = {
|
|
846
|
+
live,
|
|
847
|
+
app: this.scene.fogNode
|
|
848
|
+
};
|
|
849
|
+
this.scene.fogNode = live.node;
|
|
850
|
+
}
|
|
851
|
+
/** Gives `scene.fogNode` back to the app (unless the app replaced the pipeline's fog meanwhile). */
|
|
852
|
+
restoreFog() {
|
|
853
|
+
if (!this.fog) return;
|
|
854
|
+
if (this.scene.fogNode === this.fog.live.node) this.scene.fogNode = this.fog.app;
|
|
855
|
+
this.fog = null;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* The Scene Effects that can run: without a shadow-casting `sun`, god rays are left out (with one
|
|
859
|
+
* console warning per pipeline). Shadows and lights are never changed.
|
|
860
|
+
*/
|
|
861
|
+
usableSceneEffects(settings) {
|
|
862
|
+
if (!settings.godrays) return settings;
|
|
863
|
+
const problem = godraysSunProblem(this.options.sun);
|
|
864
|
+
if (!problem) return settings;
|
|
865
|
+
if (!this.warnedSun) warnGodraysSkipped(problem);
|
|
866
|
+
this.warnedSun = true;
|
|
867
|
+
return {
|
|
868
|
+
...settings,
|
|
869
|
+
godrays: void 0
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
disposeGraph() {
|
|
873
|
+
if (!this.graph) return;
|
|
874
|
+
for (const node of this.graph.treeNodes) node.dispose();
|
|
875
|
+
this.graph.scene.dispose();
|
|
876
|
+
if (this.graph.ao) {
|
|
877
|
+
this.graph.ao.dispose();
|
|
878
|
+
this.scenePass.pass.contextNode = null;
|
|
879
|
+
}
|
|
880
|
+
this.graph = null;
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
/** Shorthand for `new ScenePipeline(renderer, scene, camera, options)`. */
|
|
884
|
+
function createScenePipeline(renderer, scene, camera, options) {
|
|
885
|
+
return new ScenePipeline(renderer, scene, camera, options);
|
|
886
|
+
}
|
|
887
|
+
export { SUGGESTED_SCENE_EFFECTS, ScenePipeline, WIND_VELOCITY, applySceneEffects, createScenePipeline, sceneAO, sceneAntialias, sceneBloom, sceneFog, sceneGodrays, sceneMotionBlur, sceneToneMapping, windVelocity, windVelocityMRT };
|
|
888
|
+
|
|
889
|
+
//# sourceMappingURL=scene.js.map
|