From e43b6ec4fbe592abb6eb04897631317c09cb024a Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 25 Sep 2020 18:50:23 +0200 Subject: [PATCH 1/3] Support alpha keys Add support for gradient key alpha value to allow partial recolor --- .../Resources/Recolor.hlsl | 52 +++++++++++++------ .../Runtime/Internal/Utility.cs | 25 +++++++++ .../Runtime/Recolor.cs | 3 ++ 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl index ccf2951..697c253 100644 --- a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl +++ b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl @@ -41,6 +41,15 @@ float4 _ColorKey5; float4 _ColorKey6; float4 _ColorKey7; +float4 _AlphaKey0; +float4 _AlphaKey1; +float4 _AlphaKey2; +float4 _AlphaKey3; +float4 _AlphaKey4; +float4 _AlphaKey5; +float4 _AlphaKey6; +float4 _AlphaKey7; + TEXTURE2D(_DitherTexture); float _DitherStrength; @@ -126,20 +135,31 @@ float4 Fragment(Varyings input) : SV_Target float dither = LOAD_TEXTURE2D(_DitherTexture, positionSS % uint2(tw, th)).x; dither = (dither - 0.5) * _DitherStrength; - // Apply fill gradient. - float3 fill = _ColorKey0.rgb; - float lum = Luminance(c0.rgb) + dither; + // Apply fill gradient. + float3 fill = _ColorKey0.rgb; + float fillAlpha = _AlphaKey0.r; + float lum = Luminance(c0.rgb) + dither; #ifdef RECOLOR_GRADIENT_LERP - fill = lerp(fill, _ColorKey1.rgb, saturate((lum - _ColorKey0.w) / (_ColorKey1.w - _ColorKey0.w))); - fill = lerp(fill, _ColorKey2.rgb, saturate((lum - _ColorKey1.w) / (_ColorKey2.w - _ColorKey1.w))); - fill = lerp(fill, _ColorKey3.rgb, saturate((lum - _ColorKey2.w) / (_ColorKey3.w - _ColorKey2.w))); - #ifdef RECOLOR_GRADIENT_EXT - fill = lerp(fill, _ColorKey4.rgb, saturate((lum - _ColorKey3.w) / (_ColorKey4.w - _ColorKey3.w))); - fill = lerp(fill, _ColorKey5.rgb, saturate((lum - _ColorKey4.w) / (_ColorKey5.w - _ColorKey4.w))); - fill = lerp(fill, _ColorKey6.rgb, saturate((lum - _ColorKey5.w) / (_ColorKey6.w - _ColorKey5.w))); - fill = lerp(fill, _ColorKey7.rgb, saturate((lum - _ColorKey6.w) / (_ColorKey7.w - _ColorKey6.w))); - #endif + fill = lerp(fill, _ColorKey1.rgb, saturate((lum - _ColorKey0.w) / (_ColorKey1.w - _ColorKey0.w))); + fill = lerp(fill, _ColorKey2.rgb, saturate((lum - _ColorKey1.w) / (_ColorKey2.w - _ColorKey1.w))); + fill = lerp(fill, _ColorKey3.rgb, saturate((lum - _ColorKey2.w) / (_ColorKey3.w - _ColorKey2.w))); + + fillAlpha = lerp(fillAlpha, _AlphaKey1.r, saturate((lum - _AlphaKey0.w) / (_AlphaKey1.w - _AlphaKey0.w))); + fillAlpha = lerp(fillAlpha, _AlphaKey2.r, saturate((lum - _AlphaKey1.w) / (_AlphaKey2.w - _AlphaKey1.w))); + fillAlpha = lerp(fillAlpha, _AlphaKey3.r, saturate((lum - _AlphaKey2.w) / (_AlphaKey3.w - _AlphaKey2.w))); + + #ifdef RECOLOR_GRADIENT_EXT + fill = lerp(fill, _ColorKey4.rgb, saturate((lum - _ColorKey3.w) / (_ColorKey4.w - _ColorKey3.w))); + fill = lerp(fill, _ColorKey5.rgb, saturate((lum - _ColorKey4.w) / (_ColorKey5.w - _ColorKey4.w))); + fill = lerp(fill, _ColorKey6.rgb, saturate((lum - _ColorKey5.w) / (_ColorKey6.w - _ColorKey5.w))); + fill = lerp(fill, _ColorKey7.rgb, saturate((lum - _ColorKey6.w) / (_ColorKey7.w - _ColorKey6.w))); + + fillAlpha = lerp(fillAlpha, _AlphaKey4.r, saturate((lum - _AlphaKey3.w) / (_AlphaKey4.w - _AlphaKey3.w))); + fillAlpha = lerp(fillAlpha, _AlphaKey5.r, saturate((lum - _AlphaKey4.w) / (_AlphaKey5.w - _AlphaKey4.w))); + fillAlpha = lerp(fillAlpha, _AlphaKey6.r, saturate((lum - _AlphaKey5.w) / (_AlphaKey6.w - _AlphaKey5.w))); + fillAlpha = lerp(fillAlpha, _AlphaKey7.r, saturate((lum - _AlphaKey6.w) / (_AlphaKey7.w - _AlphaKey6.w))); + #endif #else fill = lum > _ColorKey0.w ? _ColorKey1.rgb : fill; fill = lum > _ColorKey1.w ? _ColorKey2.rgb : fill; @@ -152,8 +172,8 @@ float4 Fragment(Varyings input) : SV_Target #endif #endif - float edge = smoothstep(_EdgeThresholds.x, _EdgeThresholds.y, g); - float3 cb = lerp(c0.rgb, fill, _FillOpacity); - float3 co = lerp(cb, _EdgeColor.rgb, edge * _EdgeColor.a); - return float4(co, c0.a); + float edge = smoothstep(_EdgeThresholds.x, _EdgeThresholds.y, g); + float3 cb = lerp(c0.rgb, fill.rgb, _FillOpacity * fillAlpha); + float3 co = lerp(cb, _EdgeColor.rgb, edge * _EdgeColor.a); + return float4(co, c0.a); } diff --git a/Packages/jp.keijiro.kino.post-processing/Runtime/Internal/Utility.cs b/Packages/jp.keijiro.kino.post-processing/Runtime/Internal/Utility.cs index 8b007f9..7d7c442 100644 --- a/Packages/jp.keijiro.kino.post-processing/Runtime/Internal/Utility.cs +++ b/Packages/jp.keijiro.kino.post-processing/Runtime/Internal/Utility.cs @@ -38,6 +38,18 @@ public static class GradientUtility Shader.PropertyToID("_ColorKey7") }; + static readonly int[] _alphaKeyPropertyIDs = new[] +{ + Shader.PropertyToID("_AlphaKey0"), + Shader.PropertyToID("_AlphaKey1"), + Shader.PropertyToID("_AlphaKey2"), + Shader.PropertyToID("_AlphaKey3"), + Shader.PropertyToID("_AlphaKey4"), + Shader.PropertyToID("_AlphaKey5"), + Shader.PropertyToID("_AlphaKey6"), + Shader.PropertyToID("_AlphaKey7") + }; + public static Gradient DefaultGradient { get { var g = new Gradient(); @@ -51,6 +63,11 @@ public static int GetColorKeyPropertyID(int index) return _colorKeyPropertyIDs[index]; } + public static int GetAlphaKeyPropertyID(int index) + { + return _alphaKeyPropertyIDs[index]; + } + public static void SetColorKeys(Material material, GradientColorKey[] colorKeys) { for (var i = 0; i < 8; i++) @@ -59,6 +76,14 @@ public static void SetColorKeys(Material material, GradientColorKey[] colorKeys) colorKeys[Mathf.Min(i, colorKeys.Length - 1)].ToVector() ); } + + public static void SetAlphaKeys(Material material, GradientAlphaKey[] alphaKeys) + { + for (var i = 0; i < 8; i++) + material.SetVector( + GetAlphaKeyPropertyID(i),new Vector4(alphaKeys[Mathf.Min(i, alphaKeys.Length - 1)].alpha,0,0, alphaKeys[Mathf.Min(i, alphaKeys.Length - 1)].time) + ); + } } public static class GradientColorKeyExtension diff --git a/Packages/jp.keijiro.kino.post-processing/Runtime/Recolor.cs b/Packages/jp.keijiro.kino.post-processing/Runtime/Recolor.cs index f28725e..b97588a 100644 --- a/Packages/jp.keijiro.kino.post-processing/Runtime/Recolor.cs +++ b/Packages/jp.keijiro.kino.post-processing/Runtime/Recolor.cs @@ -47,6 +47,7 @@ static class ShaderIDs Gradient _cachedGradient; GradientColorKey [] _cachedColorKeys; + GradientAlphaKey[] _cachedAlphaKeys; DitherType _ditherType; Texture2D _ditherTexture; @@ -91,6 +92,7 @@ public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle srcRT, { _cachedGradient = fillGradient.value; _cachedColorKeys = _cachedGradient.colorKeys; + _cachedAlphaKeys = _cachedGradient.alphaKeys; } Vector2 edgeThresh; @@ -112,6 +114,7 @@ public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle srcRT, _material.SetVector(ShaderIDs.EdgeThresholds, edgeThresh); _material.SetFloat(ShaderIDs.FillOpacity, fillOpacity.value); GradientUtility.SetColorKeys(_material, _cachedColorKeys); + GradientUtility.SetAlphaKeys(_material, _cachedAlphaKeys); _material.SetTexture(ShaderIDs.DitherTexture, _ditherTexture); _material.SetFloat(ShaderIDs.DitherStrength, ditherStrength.value); From fc1594cdbba26f9c950abfef23d90c1a1cf7592a Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 25 Sep 2020 18:57:09 +0200 Subject: [PATCH 2/3] Add alpha support to non interpolating gradients --- .../Resources/Recolor.hlsl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl index 697c253..99e28ce 100644 --- a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl +++ b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl @@ -164,11 +164,21 @@ float4 Fragment(Varyings input) : SV_Target fill = lum > _ColorKey0.w ? _ColorKey1.rgb : fill; fill = lum > _ColorKey1.w ? _ColorKey2.rgb : fill; fill = lum > _ColorKey2.w ? _ColorKey3.rgb : fill; + + fill = lum > _AlphaKey0.w ? _AlphaKey1.rgb : fillAlpha; + fill = lum > _AlphaKey1.w ? _AlphaKey2.rgb : fillAlpha; + fill = lum > _AlphaKey2.w ? _AlphaKey3.rgb : fillAlpha; + #ifdef RECOLOR_GRADIENT_EXT fill = lum > _ColorKey3.w ? _ColorKey4.rgb : fill; fill = lum > _ColorKey4.w ? _ColorKey5.rgb : fill; fill = lum > _ColorKey5.w ? _ColorKey6.rgb : fill; fill = lum > _ColorKey6.w ? _ColorKey7.rgb : fill; + + fill = lum > _AlphaKey3.w ? _AlphaKey4.rgb : fillAlpha; + fill = lum > _AlphaKey4.w ? _AlphaKey5.rgb : fillAlpha; + fill = lum > _AlphaKey5.w ? _AlphaKey6.rgb : fillAlpha; + fill = lum > _AlphaKey6.w ? _AlphaKey7.rgb : fillAlpha; #endif #endif From e142e66314f384a8f68beadfaee5d3027be5916b Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 25 Sep 2020 19:08:22 +0200 Subject: [PATCH 3/3] Test and fix non interpolating gradient --- .../Resources/Recolor.hlsl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl index 99e28ce..7de5540 100644 --- a/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl +++ b/Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl @@ -165,9 +165,9 @@ float4 Fragment(Varyings input) : SV_Target fill = lum > _ColorKey1.w ? _ColorKey2.rgb : fill; fill = lum > _ColorKey2.w ? _ColorKey3.rgb : fill; - fill = lum > _AlphaKey0.w ? _AlphaKey1.rgb : fillAlpha; - fill = lum > _AlphaKey1.w ? _AlphaKey2.rgb : fillAlpha; - fill = lum > _AlphaKey2.w ? _AlphaKey3.rgb : fillAlpha; + fillAlpha = lum > _AlphaKey0.w ? _AlphaKey1.r : fillAlpha; + fillAlpha = lum > _AlphaKey1.w ? _AlphaKey2.r : fillAlpha; + fillAlpha = lum > _AlphaKey2.w ? _AlphaKey3.r : fillAlpha; #ifdef RECOLOR_GRADIENT_EXT fill = lum > _ColorKey3.w ? _ColorKey4.rgb : fill; @@ -175,10 +175,10 @@ float4 Fragment(Varyings input) : SV_Target fill = lum > _ColorKey5.w ? _ColorKey6.rgb : fill; fill = lum > _ColorKey6.w ? _ColorKey7.rgb : fill; - fill = lum > _AlphaKey3.w ? _AlphaKey4.rgb : fillAlpha; - fill = lum > _AlphaKey4.w ? _AlphaKey5.rgb : fillAlpha; - fill = lum > _AlphaKey5.w ? _AlphaKey6.rgb : fillAlpha; - fill = lum > _AlphaKey6.w ? _AlphaKey7.rgb : fillAlpha; + fillAlpha = lum > _AlphaKey3.w ? _AlphaKey4.r : fillAlpha; + fillAlpha = lum > _AlphaKey4.w ? _AlphaKey5.r : fillAlpha; + fillAlpha = lum > _AlphaKey5.w ? _AlphaKey6.r : fillAlpha; + fillAlpha = lum > _AlphaKey6.w ? _AlphaKey7.r : fillAlpha; #endif #endif