diff options
| -rw-r--r-- | fog.cginc | 44 | ||||
| -rw-r--r-- | iq_sdf.cginc | 2 | ||||
| -rw-r--r-- | tone.cginc | 51 | ||||
| -rw-r--r-- | tone_iq.cginc | 31 |
4 files changed, 86 insertions, 42 deletions
@@ -9,6 +9,7 @@ #include "oklab.cginc" #include "pbr.cginc" #include "poi.cginc" +#include "tone.cginc" #ifndef __FOG_INC #define __FOG_INC @@ -209,45 +210,6 @@ float fog00_map_dr( } #endif -// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ -// cc0 -float3 AcesFilmic(float3 x) { - float a = 2.51f; - float b = 0.03f; - float c = 2.43f; - float d = 0.59f; - float e = 0.14f; - 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; @@ -415,11 +377,11 @@ Fog00PBR getFog00(v2f i, ToonerData tdata) { //pbr.albedo.rgb += ign(tdata.screen_uv_round) * .00390625; // Remap onto [0, 1] - pbr.albedo.rgb = AcesFilmic(pbr.albedo.rgb); + pbr.albedo.rgb = aces_filmic(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.x = smooth_clamp(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)); diff --git a/iq_sdf.cginc b/iq_sdf.cginc index c728ab5..6320e57 100644 --- a/iq_sdf.cginc +++ b/iq_sdf.cginc @@ -4,7 +4,7 @@ #define __IQ_SDF_INC // The MIT License -// Copyright © 2019-2021 Inigo Quilez +// Copyright © 2019-2024 Inigo Quilez // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. float distance_from_octahedron(in float3 p) diff --git a/tone.cginc b/tone.cginc new file mode 100644 index 0000000..371a8db --- /dev/null +++ b/tone.cginc @@ -0,0 +1,51 @@ +#include "tone_iq.cginc" + +#ifndef __TONE_INC +#define __TONE_INC + +// This library contains a bunch of useful tonemapping curves. + +// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ +// cc0 +float3 aces_filmic(float3 x) { + float a = 2.51f; + float b = 0.03f; + float c = 2.43f; + float d = 0.59f; + float e = 0.14f; + return saturate((x*(a*x+b))/(x*(c*x+d)+e)); +} + +// Clamp x to [0, k]. +// Assumes that x is already on [0, 1]. +// Nice properties: +// 1. At x=0, the derivative is 1. +// 2. No transcendental ops, and branchless. +float3 smooth_clamp(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 smooth_clamp(float x, float k) { + float e = 1E-4; + k = min(1-e, k); + float b = k/(1-k); + return b * x / (x + b); +} + +#endif // __TONE_INC + diff --git a/tone_iq.cginc b/tone_iq.cginc new file mode 100644 index 0000000..d9d3bde --- /dev/null +++ b/tone_iq.cginc @@ -0,0 +1,31 @@ +#ifndef __TONE_IQ_INC +#define __TONE_IQ_INC + +// The MIT License +// Copyright © 2019-2024 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// https://iquilezles.org/articles/functions/ +float almost_identity( float x, float m, float n ) +{ + float a = 2.0*n - m; + float b = 2.0*m - 3.0*n; + float t = x/m; + return lerp( + (a*t + b)*t*t + n, + x, + x > m); +} +float3 almost_identity( float3 x, float3 m, float3 n ) +{ + float3 a = 2.0*n - m; + float3 b = 2.0*m - 3.0*n; + float3 t = x/m; + return lerp( + (a*t + b)*t*t + n, + x, + x > m); +} + +#endif // __TONE_IQ_INC + |
