Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of gradient alpha to Recolor #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions Packages/jp.keijiro.kino.post-processing/Resources/Recolor.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -126,34 +135,55 @@ 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;
fill = lum > _ColorKey2.w ? _ColorKey3.rgb : fill;

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;
fill = lum > _ColorKey4.w ? _ColorKey5.rgb : fill;
fill = lum > _ColorKey5.w ? _ColorKey6.rgb : fill;
fill = lum > _ColorKey6.w ? _ColorKey7.rgb : fill;

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

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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++)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Packages/jp.keijiro.kino.post-processing/Runtime/Recolor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static class ShaderIDs

Gradient _cachedGradient;
GradientColorKey [] _cachedColorKeys;
GradientAlphaKey[] _cachedAlphaKeys;

DitherType _ditherType;
Texture2D _ditherTexture;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down