From b377dd05175d5bffaeef9c55051cd396c127daef Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 20 Mar 2025 18:57:24 -0700 Subject: Add false color visualizer Based on Sotalo's Furality talk - I wanted a way to visualize when my albedo is outside the recommended [0.025, 90]% brightness range. This feature does that. Dark is rendered red, bright is rendered blue. --- 2ner.cginc | 4 + 2ner.shader | 8 ++ Scripts/ThryEditor | 2 +- false_color_visualization.cginc | 28 +++++ features.cginc | 5 + globals.cginc | 5 + math.cginc | 6 +- oklab.cginc | 236 ++++++++++++++++++++-------------------- yum_lighting.cginc | 4 +- 9 files changed, 173 insertions(+), 125 deletions(-) create mode 100644 false_color_visualization.cginc diff --git a/2ner.cginc b/2ner.cginc index 8a16018..e8737a5 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -6,6 +6,7 @@ #include "eyes.cginc" #include "face_me.cginc" +#include "false_color_visualization.cginc" #include "features.cginc" #include "globals.cginc" #include "harnack_tracing.cginc" @@ -207,6 +208,9 @@ float4 frag(v2f i l.specular = max(0, l.specular); #endif + + pbr.albedo.rgb = visualizeInFalseColor(pbr.albedo.rgb); + float4 lit = YumBRDF(i, l, pbr); #if defined(_HARNACK_TRACING) diff --git a/2ner.shader b/2ner.shader index 6d8a289..fdcb3fa 100644 --- a/2ner.shader +++ b/2ner.shader @@ -468,6 +468,14 @@ Shader "yum_food/2ner" [HideInInspector] m_end_Face_Me("Face me", Float) = 0 //endex + //ifex _False_Color_Visualization_Enabled==0 + [HideInInspector] m_start_False_Color_Visualization("False color", Float) = 0 + [ThryToggle(_FALSE_COLOR_VISUALIZATION)] _False_Color_Visualization_Enabled("Enable", Float) = 0 + [MaterialToggle] _False_Color_Visualization_Luminance("Luminance", Float) = 0 + [MaterialToggle] _False_Color_Visualization_Luminance_Bounded("Luminance (bounded)", Float) = 0 + [HideInInspector] m_end_False_Color_Visualization("False color", Float) = 0 + //endex + //ifex _Vertex_Domain_Warping_Enabled==0 [HideInInspector] m_start_Vertex_Domain_Warping("Vertex domain warping", Float) = 0 [ThryToggle(_VERTEX_DOMAIN_WARPING)]_Vertex_Domain_Warping_Enabled("Enable", Float) = 0 diff --git a/Scripts/ThryEditor b/Scripts/ThryEditor index c1a6bcb..45ade77 160000 --- a/Scripts/ThryEditor +++ b/Scripts/ThryEditor @@ -1 +1 @@ -Subproject commit c1a6bcbdc0ca7e79294e0ca57712e38a111e5448 +Subproject commit 45ade77ceecba50439d91b24c7700655e7ef6e3d diff --git a/false_color_visualization.cginc b/false_color_visualization.cginc new file mode 100644 index 0000000..d3508f1 --- /dev/null +++ b/false_color_visualization.cginc @@ -0,0 +1,28 @@ +#ifndef __FALSE_COLOR_VISUALIZATION_INC +#define __FALSE_COLOR_VISUALIZATION_INC + +#include "features.cginc" +#include "globals.cginc" +#include "math.cginc" + +float3 visualizeInFalseColor(float3 color) +{ +#if defined(_FALSE_COLOR_VISUALIZATION) + [branch] + if (_False_Color_Visualization_Luminance) { + color.xyz = luminance(color); + } else if (_False_Color_Visualization_Luminance_Bounded) { + color.xyz = luminance(color); + // Sotalo suggests keeping albedo within the range 0.015% to 90%. He + // might have meant any(color < blah), but I think this makes more + // sense. I.e. it takes into account perceptual bias. + color.xyz = + color.x < 00.015 * 0.01 ? float3(1, 0, 0) : + color.x > 90.000 * 0.01 ? float3(0, 0, 1) : color.xyz; + } +#endif // _FALSE_COLOR_VISUALIZATION +return color; +} + +#endif // __FALSE_COLOR_VISUALIZATION_INC + diff --git a/features.cginc b/features.cginc index d263ca8..2d587be 100644 --- a/features.cginc +++ b/features.cginc @@ -143,6 +143,11 @@ #pragma shader_feature_local _DECAL3_TILING_MODE #pragma shader_feature_local _DECAL3_REPLACE_ALPHA //endex + +//ifex _False_Color_Visualization_Enabled==0 +#pragma shader_feature_local _FALSE_COLOR_VISUALIZATION +//endex + //ifex _Vertex_Domain_Warping_Enabled==0 #pragma shader_feature_local _VERTEX_DOMAIN_WARPING //endex diff --git a/globals.cginc b/globals.cginc index 1e56785..899f67a 100644 --- a/globals.cginc +++ b/globals.cginc @@ -384,4 +384,9 @@ float _Harnack_Tracing_Gyroid_Speed; float _Harnack_Tracing_Gyroid_Scale; #endif // _HARNACK_TRACING_GYROID +#if defined(_FALSE_COLOR_VISUALIZATION) +float _False_Color_Visualization_Luminance; +float _False_Color_Visualization_Luminance_Bounded; +#endif // _FALSE_COLOR_VISUALIZATION + #endif // __GLOBALS_INC diff --git a/math.cginc b/math.cginc index 436cc56..2f05054 100644 --- a/math.cginc +++ b/math.cginc @@ -194,6 +194,8 @@ float3 blendNormalsHill12(float3 n0, float3 n1) { return normalize(n0 * dot(n0, n1) - n1 * n0.z); } -#endif // __MATH_INC - +float3 luminance(float3 color) { + return dot(color, float3(0.2126, 0.7152, 0.0722)); +} +#endif // __MATH_INC diff --git a/oklab.cginc b/oklab.cginc index c5f87f6..3c1d3bb 100644 --- a/oklab.cginc +++ b/oklab.cginc @@ -1,120 +1,116 @@ -/* - * MIT License - * - * Copyright (c) 2023-2025 yum_food - * - * 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. - */ - -#ifndef __OKLAB_INC -#define __OKLAB_INC - -#if defined(_EYE_EFFECT_00) - -// Utilities relating to the OKLAB color space, as defined here: -// https://bottosson.github.io/posts/oklab/ -// Code is derived from the samples there, which are in the public domain. - -// Weights: https://en.wikipedia.org/wiki/SRGB -float3 LRGBtoXYZ(float3 c) { - float3x3 rgb_to_xyz = float3x3( - 0.4124, 0.3576, 0.1805, - 0.2126, 0.7152, 0.0722, - 0.0193, 0.1192, 0.9505); - return saturate(mul(rgb_to_xyz, c)); -} - -// Weights: https://en.wikipedia.org/wiki/SRGB -float3 XYZtoLRGB(float3 c) { - float3x3 xyz_to_rgb = float3x3( - 3.24062548, -1.53720797, -0.4986286, - -0.96893071, 1.87575606, 0.04151752, - 0.05571012, -0.20402105, 1.05699594); - return saturate(mul(xyz_to_rgb, c)); -} - -// Source: https://bottosson.github.io/posts/oklab/ -float3 XYZtoOKLAB(float3 c) { - float3x3 m1 = float3x3( - 0.8189330101, 0.3618667424, -0.1288597137, - 0.0329845436, 0.9293118715, 0.0361456387, - 0.0482003018, 0.2643662691, 0.6338517070); - float3x3 m2 = float3x3( - 0.2104542553, 0.7936177850, -0.0040720468, - 1.9779984951, -2.4285922050, 0.4505937099, - 0.0259040371, 0.7827717662, -0.8086757660); - c = mul(m1, c); - c = pow(c, 0.33333333333); - return mul(m2, c); -} - -// Source: https://bottosson.github.io/posts/oklab/ -float3 OKLABtoXYZ(float3 c) { - float3x3 m1i = float3x3( - 1.22701385, -0.55779998, 0.28125615, - -0.04058018, 1.11225687, -0.07167668, - -0.07638128, -0.42148198, 1.58616322); - float3x3 m2i = float3x3( - 1.00000000, 0.39633779, 0.21580376, - 1.00000001, -0.10556134, -0.06385417, - 1.00000005, -0.08948418, -1.29148554); - c = mul(m2i, c); - c = pow(c, 3.0); - return mul(m1i, c); -} - -// Source: https://bottosson.github.io/posts/oklab/ -float3 OKLABtoOKLCH(float3 c) { - float c_ = length(c.yz); - float h_ = atan2(c.z, c.y); - return float3(c.x, c_, h_); -} - -// Source: https://bottosson.github.io/posts/oklab/ -// Note: hue must be in units of radians. -float3 OKLCHtoOKLAB(float3 c) { - float a = c.y * cos(c.z); - float b = c.y * sin(c.z); - return float3(c.x, a, b); -} - -float3 LRGBtoOKLAB(float3 c) { - return XYZtoOKLAB(LRGBtoXYZ(c)); -} - -float3 OKLABtoLRGB(float3 c) { - return XYZtoLRGB(OKLABtoXYZ(c)); -} - -float3 LRGBtoOKLCH(float3 c) { - return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c))); -} - -float3 OKLCHtoLRGB(float3 c) { - return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c))); -} - -#endif // _OKLAB - -#endif // __OKLAB_INC - +/* + * MIT License + * + * Copyright (c) 2023-2024 yum_food + * + * 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. + */ + +#ifndef __OKLAB_INC +#define __OKLAB_INC + +// Utilities relating to the OKLAB color space, as defined here: +// https://bottosson.github.io/posts/oklab/ +// Code is derived from the samples there, which are in the public domain. + +// Weights: https://en.wikipedia.org/wiki/SRGB +float3 LRGBtoXYZ(float3 c) { + float3x3 rgb_to_xyz = float3x3( + 0.4124, 0.3576, 0.1805, + 0.2126, 0.7152, 0.0722, + 0.0193, 0.1192, 0.9505); + return saturate(mul(rgb_to_xyz, c)); +} + +// Weights: https://en.wikipedia.org/wiki/SRGB +float3 XYZtoLRGB(float3 c) { + float3x3 xyz_to_rgb = float3x3( + 3.24062548, -1.53720797, -0.4986286, + -0.96893071, 1.87575606, 0.04151752, + 0.05571012, -0.20402105, 1.05699594); + return saturate(mul(xyz_to_rgb, c)); +} + +// Source: https://bottosson.github.io/posts/oklab/ +float3 XYZtoOKLAB(float3 c) { + float3x3 m1 = float3x3( + 0.8189330101, 0.3618667424, -0.1288597137, + 0.0329845436, 0.9293118715, 0.0361456387, + 0.0482003018, 0.2643662691, 0.6338517070); + float3x3 m2 = float3x3( + 0.2104542553, 0.7936177850, -0.0040720468, + 1.9779984951, -2.4285922050, 0.4505937099, + 0.0259040371, 0.7827717662, -0.8086757660); + c = mul(m1, c); + c = pow(c, 0.33333333333); + return mul(m2, c); +} + +// Source: https://bottosson.github.io/posts/oklab/ +float3 OKLABtoXYZ(float3 c) { + float3x3 m1i = float3x3( + 1.22701385, -0.55779998, 0.28125615, + -0.04058018, 1.11225687, -0.07167668, + -0.07638128, -0.42148198, 1.58616322); + float3x3 m2i = float3x3( + 1.00000000, 0.39633779, 0.21580376, + 1.00000001, -0.10556134, -0.06385417, + 1.00000005, -0.08948418, -1.29148554); + c = mul(m2i, c); + c = pow(c, 3.0); + return mul(m1i, c); +} + +// Source: https://bottosson.github.io/posts/oklab/ +float3 OKLABtoOKLCH(float3 c) { + float c_ = length(c.yz); + float h_ = atan2(c.z, c.y); + return float3(c.x, c_, h_); +} + +// Source: https://bottosson.github.io/posts/oklab/ +// Note: hue must be in units of radians. +float3 OKLCHtoOKLAB(float3 c) { + float a = c.y * cos(c.z); + float b = c.y * sin(c.z); + return float3(c.x, a, b); +} + +float3 LRGBtoOKLAB(float3 c) { + return XYZtoOKLAB(LRGBtoXYZ(c)); +} + +float3 OKLABtoLRGB(float3 c) { + return XYZtoLRGB(OKLABtoXYZ(c)); +} + +float3 LRGBtoOKLCH(float3 c) { + return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c))); +} + +float3 OKLCHtoLRGB(float3 c) { + return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c))); +} + +#endif // __OKLAB_INC + diff --git a/yum_lighting.cginc b/yum_lighting.cginc index 23e360e..0b4e077 100644 --- a/yum_lighting.cginc +++ b/yum_lighting.cginc @@ -231,11 +231,11 @@ YumLighting GetYumLighting(v2f i, YumPbr pbr) { #endif #if defined(_QUANTIZE_SPECULAR) - float specular_luminance = dot(light.specular, float3(0.2126, 0.7152, 0.0722)); // convert to luminance + float specular_luminance = luminance(light.specular); light.specular = light.specular * floor(specular_luminance * _Quantize_Specular_Steps) / _Quantize_Specular_Steps; #endif #if defined(_QUANTIZE_DIFFUSE) - float diffuse_luminance = dot(light.diffuse, float3(0.2126, 0.7152, 0.0722)); // convert to luminance + float diffuse_luminance = luminance(light.diffuse); light.diffuse = light.diffuse * floor(diffuse_luminance * _Quantize_Diffuse_Steps) / _Quantize_Diffuse_Steps; #endif -- cgit v1.2.3