summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-11-17 01:54:27 -0800
committeryum <yum.food.vr@gmail.com>2024-11-17 01:54:27 -0800
commit2f8b9552ad99c525f02f3c48c4e451fa6d5d44e8 (patch)
tree82517f04b40ca6ce96d7bf761106dea3480d940f
parentc9a49e8def64702966afe901624ca85a48bac2f1 (diff)
Add brightness clamping to fog
Use a nice "almost identity" function to create a smooth transition to the clamped value.
-rw-r--r--Editor/tooner.cs2
-rw-r--r--fog.cginc39
-rw-r--r--globals.cginc1
-rw-r--r--oklab.cginc2
-rw-r--r--tooner.shader1
5 files changed, 42 insertions, 3 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs
index df3fa9d..2f87fb3 100644
--- a/Editor/tooner.cs
+++ b/Editor/tooner.cs
@@ -2087,6 +2087,8 @@ public class ToonerGUI : ShaderGUI {
RangeProperty(bc, "Ray origin randomization");
bc = FindProperty("_Gimmick_Fog_00_Lod_Half_Life");
FloatProperty(bc, "LOD half life");
+ bc = FindProperty("_Gimmick_Fog_00_Max_Brightness");
+ RangeProperty(bc, "Max brightness");
bc = FindProperty("_Gimmick_Fog_00_Noise");
TexturePropertySingleLine(
MakeLabel(bc, "3D Noise"),
diff --git a/fog.cginc b/fog.cginc
index 51b0936..5eb2145 100644
--- a/fog.cginc
+++ b/fog.cginc
@@ -220,6 +220,34 @@ float3 ACES(float3 x) {
return saturate((x*(a*x+b))/(x*(c*x+d)+e));
}
+// Clamp x to [0, k].
+// Assumes that x is already on [0, 1].
+float3 SmoothClamp(float3 x, float k) {
+ // Derivation of `b` from `k`:
+ // f(x, b) = b * x / (x + b)
+ // We want f(1, b) = k.
+ // In other words, we want the max value the function can take on [0, 1] to
+ // be k.
+ // k = f(1, b)
+ // = b / (1 + b)
+ // b = k * (1 + b)
+ // = k + kb
+ // 1 = k/b + k
+ // 1 - k = k/b
+ // 1/(1-k) = b/k
+ // b = k/(1-k)
+ float e = 1E-4;
+ k = min(1-e, k);
+ float b = k/(1-k);
+ return b * x / (x + b);
+}
+float SmoothClamp(float x, float k) {
+ float e = 1E-4;
+ k = min(1-e, k);
+ float b = k/(1-k);
+ return b * x / (x + b);
+}
+
Fog00PBR getFog00(v2f i, ToonerData tdata) {
float3 cam_pos = _WorldSpaceCameraPos;
float3 obj_pos = i.worldPos;
@@ -382,10 +410,17 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) {
Fog00PBR pbr;
pbr.albedo = acc;
- pbr.albedo.rgb = ACES(pbr.albedo.rgb);
// Add some dithering to lit color to break up banding
- pbr.albedo.rgb += ign(tdata.screen_uv_round) * .00390625;
+ //pbr.albedo.rgb += ign(tdata.screen_uv_round) * .00390625;
+
+ // Remap onto [0, 1]
+ pbr.albedo.rgb = ACES(pbr.albedo.rgb);
+ // Clamp so max brightness is comfortable. Do it in perceptually uniform
+ // space to avoid affecting saturation.
+ pbr.albedo.rgb = LRGBtoOKLAB(pbr.albedo.rgb);
+ pbr.albedo.x = SmoothClamp(pbr.albedo.x, _Gimmick_Fog_00_Max_Brightness);
+ pbr.albedo.rgb = OKLABtoLRGB(pbr.albedo.rgb);
float4 clip_pos = mul(UNITY_MATRIX_VP, float4(ro, 1.0));
pbr.depth = clip_pos.z / clip_pos.w;
diff --git a/globals.cginc b/globals.cginc
index 1c81c2b..b5b2160 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -758,6 +758,7 @@ float _Gimmick_Fog_00_Normal_Cutoff;
float _Gimmick_Fog_00_Alpha_Cutoff;
float _Gimmick_Fog_00_Ray_Origin_Randomization;
float _Gimmick_Fog_00_Lod_Half_Life;
+float _Gimmick_Fog_00_Max_Brightness;
texture3D _Gimmick_Fog_00_Noise;
#if defined(_GIMMICK_FOG_00_NOISE_2D)
texture2D _Gimmick_Fog_00_Noise_2D;
diff --git a/oklab.cginc b/oklab.cginc
index 337eb94..004dbc4 100644
--- a/oklab.cginc
+++ b/oklab.cginc
@@ -29,7 +29,7 @@
#ifndef __OKLAB_INC
#define __OKLAB_INC
-#if defined(_OKLAB) || defined(_GIMMICK_LETTER_GRID_COLOR_WAVE) || defined(_GIMMICK_AL_CHROMA_00_HUE_SHIFT) || defined(_GIMMICK_FOG_00_RAY_MARCH_0)
+#if defined(_OKLAB) || defined(_GIMMICK_LETTER_GRID_COLOR_WAVE) || defined(_GIMMICK_AL_CHROMA_00_HUE_SHIFT) || defined(_GIMMICK_FOG_00)
// Utilities relating to the OKLAB color space, as defined here:
// https://bottosson.github.io/posts/oklab/
diff --git a/tooner.shader b/tooner.shader
index e47f35c..6cf6c68 100644
--- a/tooner.shader
+++ b/tooner.shader
@@ -662,6 +662,7 @@ Shader "yum_food/tooner"
_Gimmick_Fog_00_Alpha_Cutoff("Albedo cutoff (alpha)", Range(0,1)) = 0.9
_Gimmick_Fog_00_Ray_Origin_Randomization("Enable ray origin randomization", Range(0,1)) = 1
_Gimmick_Fog_00_Lod_Half_Life("fog", Float) = 5
+ _Gimmick_Fog_00_Max_Brightness("max brightness", Range(0, 1)) = 1
_Gimmick_Fog_00_Emitter_Texture("Emitter texture", 2D) = "black" {}
_Gimmick_Fog_00_Emitter_Variable_Density("Enable emitter variable density", Float) = 0