Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Let a volume fog thin with height: volumeFog.heightFalloff (part of #394)

Nguyen Xuan Ana7094ea66598
diff --git a/changes/394.md b/changes/394.md
index d389bcbc..800d6476 100644
--- a/changes/394.md
+++ b/changes/394.md
@@ -1,3 +1,4 @@
- Tree levels 1 to 3 now read the sky visibility field measured on level 0 and keep its mean, so every level sees about as much sky as level 0 (it was 0.04 to 0.06 against 0.08 to 0.12). Part of #394.
- Leaves and grass blades now let sun through to the face you see. Before, a tree's flat lower-level cards seen from below took transmitted light from the wrong side and lost the rest to their own shadow, so under the sun they read at about 0.3 to 0.6 of level 0's brightness on the leaf pixels, now about 0.5 to 0.9. Part of #394.
- The dawn forest of the `mountain-valley-dawn` reference scene was black because its mist was lit by almost nothing: the scene copied `ambient: 0.02` from the sunlit scenes, but here the sun is behind the massif and Cycles lights the mist from the sky. The mist now has the sky's mean radiance (0.5), and the forest pixels read 0.030 to 0.037 luminance against 0.014 to 0.021 before and 0.020 to 0.033 in Cycles (FLIP against Cycles 0.200 to 0.167). Before the change the near trees (70 to 150 m) already read 0.021 against 0.020 in Cycles, so no shader term changed. Part of #394.
+- `volumeFog` takes `heightFalloff` (metres): density thins as exp(-height above the volume floor / heightFalloff), so a mist can be thick on the valley floor and fade with height without a hard top. 0 keeps the flat volume. Part of #394.
diff --git a/changes/394.size.md b/changes/394.size.md
index 9f462de6..3ae115d7 100644
--- a/changes/394.size.md
+++ b/changes/394.size.md
@@ -1 +1,3 @@
-- scene:agent-scene+lazy: the lazy vegetation code now shares one sky field between tree levels and matches their mean (94 B), which keeps the lower levels from drawing near black.
+- scene:lit-box: volumeFog heightFalloff (one uniform, one shader line, one validated field), needs An to accept it as it crosses the 50000 goal
+- scene:agent-scene: same change
+- scene:agent-scene+lazy: same change
diff --git a/src/agent-look.js b/src/agent-look.js
index 798d5c48..9fbee670 100644
--- a/src/agent-look.js
+++ b/src/agent-look.js
@@ -21,7 +21,7 @@ const POINT_LIGHT_FIELDS = ['position', 'color', 'intensity', 'radius', 'range',
const SPOT_LIGHT_FIELDS = ['position', 'direction', 'angle', 'penumbra', 'color', 'intensity', 'radius', 'range', 'castShadow', 'affects'];
const AREA_LIGHT_FIELDS = ['position', 'direction', 'up', 'width', 'height', 'color', 'intensity', 'affects'];
const VOLUME_VECTOR_FIELDS = ['minimum', 'maximum', 'color', 'ambient', 'drift'];
-const VOLUME_NUMBER_FIELDS = ['density', 'steps', 'anisotropy', 'noiseAmount', 'noiseScale', 'maxDistance', 'shadowAmbient'];
+const VOLUME_NUMBER_FIELDS = ['density', 'steps', 'anisotropy', 'noiseAmount', 'noiseScale', 'maxDistance', 'shadowAmbient', 'heightFalloff'];
const BLOOM_FIELDS = ['strength', 'threshold', 'radius'];
const DEPTH_OF_FIELD_FIELDS = ['blur', 'focusDistance'];
const MOTION_BLUR_FIELDS = ['shutter'];
diff --git a/src/post.js b/src/post.js
index ca5f5f57..78d5df9c 100644
--- a/src/post.js
+++ b/src/post.js
@@ -41,6 +41,7 @@ uniform float volumeShadowAmbient;
uniform float volumeNoiseAmount;
uniform float volumeNoiseScale;
uniform float volumeMaxDistance;
+uniform float volumeHeightFalloff;
uniform float orthographic;
uniform highp sampler3D fogSkyVisibility;
uniform sampler2D occlusionTexture;
@@ -94,6 +95,7 @@ vec3 volumeFog(vec3 background) {
vec3 position = origin + direction * (entry + (float(index) + jitter) * stepSize);
float noise = 1.0;
if (volumeNoiseAmount > 0.0) noise = mix(1.0, cloudNoise((position - volumeDrift * time) * volumeNoiseScale) * 2.0, volumeNoiseAmount);
+ if (volumeHeightFalloff > 0.0) noise *= exp((volumeMinimum.y - position.y) / volumeHeightFalloff);
float opacity = 1.0 - exp(-volumeDensity * noise * stepSize);
vec4 lightPosition = volumeLightProjection * vec4(position, 1);
vec3 shadow = lightPosition.xyz / lightPosition.w * 0.5 + 0.5;
diff --git a/src/renderer.js b/src/renderer.js
index 8c175bc0..fc234915 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -46,7 +46,7 @@ const DEFORMING_TAGS = new Set(['foliage', 'grass', 'water', 'fire', 'smoke']);
// strength, so they can be culled and given levels like rigid parts.
const SWAY_REACH_PER_STRENGTH = 0.56;
-const POST_UNIFORMS = ['transparent', 'frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye', 'sceneDepth', 'volumeShadowMap', 'volumeLightProjection', 'volumeLightDirection', 'volumeLightColor', 'volumeDensity', 'volumeMinimum', 'volumeMaximum', 'volumeColor', 'volumeAmbient', 'volumeDrift', 'volumeSteps', 'volumeAnisotropy', 'volumeShadowAmbient', 'volumeNoiseAmount', 'volumeNoiseScale', 'volumeMaxDistance', 'fogSkyVisibility', 'orthographic', 'occlusionTexture', 'occlusionStrength', 'occlusionTexel'];
+const POST_UNIFORMS = ['transparent', 'frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye', 'sceneDepth', 'volumeShadowMap', 'volumeLightProjection', 'volumeLightDirection', 'volumeLightColor', 'volumeDensity', 'volumeMinimum', 'volumeMaximum', 'volumeColor', 'volumeAmbient', 'volumeDrift', 'volumeSteps', 'volumeAnisotropy', 'volumeShadowAmbient', 'volumeNoiseAmount', 'volumeNoiseScale', 'volumeHeightFalloff', 'volumeMaxDistance', 'fogSkyVisibility', 'orthographic', 'occlusionTexture', 'occlusionStrength', 'occlusionTexel'];
function postUniformLocations(context, program) {
return Object.fromEntries(POST_UNIFORMS.map(name => [name, context.getUniformLocation(program, name)]));
@@ -1770,6 +1770,7 @@ export class Renderer67 {
context.uniform1f(post.uniforms.volumeNoiseAmount, volume.noiseAmount);
context.uniform1f(post.uniforms.volumeNoiseScale, volume.noiseScale);
context.uniform1f(post.uniforms.volumeMaxDistance, volume.maxDistance);
+ context.uniform1f(post.uniforms.volumeHeightFalloff, volume.heightFalloff);
context.uniform1f(post.uniforms.orthographic, frame.projection === 'orthographic' ? 1 : 0);
context.uniform1i(post.uniforms.fogSkyVisibility, 5);
context.activeTexture(context.TEXTURE2);
diff --git a/src/volumetric.js b/src/volumetric.js
index beace050..7402b469 100644
--- a/src/volumetric.js
+++ b/src/volumetric.js
@@ -34,7 +34,8 @@ export function resolveVolumeFog(input = {}) {
const noiseAmount = finiteRange(input.noiseAmount ?? 0.5, 0, 1, 'Volume noise amount');
const noiseScale = finiteRange(input.noiseScale ?? 0.3, 0.001, 100, 'Volume noise scale');
const maxDistance = finiteRange(input.maxDistance ?? 100, 0.01, 1e4, 'Volume maximum distance');
- const resolved = { density, minimum, maximum, color, ambient, drift, steps, anisotropy, noiseAmount, noiseScale, maxDistance };
+ const heightFalloff = finiteRange(input.heightFalloff ?? 0, 0, 1e5, 'Volume height falloff');
+ const resolved = { density, minimum, maximum, color, ambient, drift, steps, anisotropy, noiseAmount, noiseScale, maxDistance, heightFalloff };
// Left out when unset, so the renderer can derive it from the scene (volumeShadowAmbient).
if (input.shadowAmbient !== undefined) {
resolved.shadowAmbient = finiteRange(input.shadowAmbient, 0, 1, 'Volume shadow ambient');
diff --git a/tests/volumetric.test.js b/tests/volumetric.test.js
index 3e71f11b..b379ab80 100644
--- a/tests/volumetric.test.js
+++ b/tests/volumetric.test.js
@@ -45,3 +45,9 @@ test('an unset volume shadow ambient follows how open the sky is above the fog',
assert.equal(volumeShadowAmbient({ ...fog, shadowAmbient: 0.6 }, room, skyOpenness), 0.6);
assert.equal(volumeShadowAmbient(fog, [floor]), 0.15, 'without a measure the scene counts as enclosed');
});
+
+test('height falloff in metres keeps its metres, and a flat volume keeps zero', () => {
+ assert.equal(resolveVolumeFog({ density: 0.1 }).heightFalloff, 0);
+ assert.equal(resolveVolumeFog({ density: 0.1, heightFalloff: 25 }).heightFalloff, 25);
+ assert.equal(resolveVolumeFog({ density: 0.1, heightFalloff: 0 }).heightFalloff, 0);
+});