diff options
| -rw-r--r-- | Editor/tooner.cs | 4 | ||||
| -rw-r--r-- | globals.cginc | 5 | ||||
| -rw-r--r-- | tooner.shader | 4 | ||||
| -rw-r--r-- | tooner_lighting.cginc | 71 |
4 files changed, 76 insertions, 8 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs index a484bc8..ecad810 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -233,6 +233,10 @@ public class ToonerGUI : ShaderGUI { editor.FloatProperty( bc, "Emission strength"); + bc = FindProperty($"_Decal{i}_Angle"); + editor.RangeProperty( + bc, + "Angle"); } EditorGUI.indentLevel -= 1; diff --git a/globals.cginc b/globals.cginc index 51f8a2f..9a60bf8 100644 --- a/globals.cginc +++ b/globals.cginc @@ -4,6 +4,7 @@ #include "AutoLight.cginc" SamplerState linear_repeat_s; +SamplerState linear_clamp_s; float4 _Color; float _Metallic; @@ -116,21 +117,25 @@ float _PBR_Overlay3_Mask_Invert; texture2D _Decal0_BaseColor; float4 _Decal0_BaseColor_ST; float _Decal0_Emission_Strength; +float _Decal0_Angle; #endif #if defined(_DECAL1) texture2D _Decal1_BaseColor; float4 _Decal1_BaseColor_ST; float _Decal1_Emission_Strength; +float _Decal1_Angle; #endif #if defined(_DECAL2) texture2D _Decal2_BaseColor; float4 _Decal2_BaseColor_ST; float _Decal2_Emission_Strength; +float _Decal2_Angle; #endif #if defined(_DECAL3) texture2D _Decal3_BaseColor; float4 _Decal3_BaseColor_ST; float _Decal3_Emission_Strength; +float _Decal3_Angle; #endif #if defined(_EMISSION) diff --git a/tooner.shader b/tooner.shader index 22cbac2..5a0fea4 100644 --- a/tooner.shader +++ b/tooner.shader @@ -74,18 +74,22 @@ Shader "yum_food/tooner" _Decal0_Enable("Enable decal", Float) = 0.0 _Decal0_BaseColor("Base color", 2D) = "white" {} _Decal0_Emission_Strength("Emission strength", Float) = 0 + _Decal0_Angle("Emission strength", Range(0,1)) = 0 _Decal1_Enable("Enable decal", Float) = 0.0 _Decal1_BaseColor("Base color", 2D) = "white" {} _Decal1_Emission_Strength("Emission strength", Float) = 0 + _Decal1_Angle("Emission strength", Range(0,1)) = 0 _Decal2_Enable("Enable decal", Float) = 0.0 _Decal2_BaseColor("Base color", 2D) = "white" {} _Decal2_Emission_Strength("Emission strength", Float) = 0 + _Decal2_Angle("Emission strength", Range(0,1)) = 0 _Decal3_Enable("Enable decal", Float) = 0.0 _Decal3_BaseColor("Base color", 2D) = "white" {} _Decal3_Emission_Strength("Emission strength", Float) = 0 + _Decal3_Angle("Emission strength", Range(0,1)) = 0 [NoScaleOffset] _EmissionTex("Emission map", 2D) = "black" {} _EmissionStrength("Emission strength", Range(0, 2)) = 0 diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc index 2632250..2eb6a47 100644 --- a/tooner_lighting.cginc +++ b/tooner_lighting.cginc @@ -464,18 +464,73 @@ void getOverlayAlbedo(inout PbrOverlay ov, #endif // _PBR_OVERLAY3 } +void applyDecalAlbedoImpl( + inout float3 albedo, + inout float decal_emission, + v2f i, + texture2D tex, + float4 tex_st, + float emission_strength, + float angle) +{ + float2 d0_uv = ((i.uv - 0.5) - tex_st.zw) * tex_st.xy + 0.5; + + if (abs(angle) > 1E-6) { + float theta = angle * 2.0 * 3.14159265; + float2x2 rot = float2x2( + cos(theta), -sin(theta), + sin(theta), cos(theta)); + d0_uv = mul(rot, d0_uv - 0.5) + 0.5; + } + + float4 d0_c = tex.SampleGrad(linear_clamp_s, + saturate(d0_uv), + ddx(d0_uv.x) * _Mip_Multiplier, + ddy(d0_uv.y) * _Mip_Multiplier); + + float d0_in_range = 1; + d0_in_range *= d0_uv.x > 0; + d0_in_range *= d0_uv.x < 1; + d0_in_range *= d0_uv.y > 0; + d0_in_range *= d0_uv.y < 1; + d0_c *= d0_in_range; + + albedo.rgb = lerp(albedo.rgb, d0_c.rgb, d0_c.a); + decal_emission += d0_c.rgb * emission_strength; +} + void applyDecalAlbedo(inout float3 albedo, inout float decal_emission, - v2f i, float iddx, float iddy) + v2f i) { #if defined(_DECAL0) - float4 d0_c = _Decal0_BaseColor.SampleGrad(linear_repeat_s, - saturate((i.uv - _Decal0_BaseColor_ST.zw) * _Decal0_BaseColor_ST.xy), - iddx * _Decal0_BaseColor_ST.x, - iddy * _Decal0_BaseColor_ST.y); - albedo.rgb = lerp(albedo.rgb, d0_c.rgb, d0_c.a); - decal_emission += d0_c.rgb * _Decal0_Emission_Strength; + applyDecalAlbedoImpl(albedo, decal_emission, i, + _Decal0_BaseColor, + _Decal0_BaseColor_ST, + _Decal0_Emission_Strength, + _Decal0_Angle); #endif // _DECAL0 +#if defined(_DECAL1) + applyDecalAlbedoImpl(albedo, decal_emission, i, + _Decal1_BaseColor, + _Decal1_BaseColor_ST, + _Decal1_Emission_Strength, + _Decal1_Angle); +#endif // _DECAL1 +#if defined(_DECAL2) + applyDecalAlbedoImpl(albedo, decal_emission, i, + _Decal2_BaseColor, + _Decal2_BaseColor_ST, + _Decal2_Emission_Strength, + _Decal2_Angle); +#endif // _DECAL2 +#if defined(_DECAL3) + applyDecalAlbedoImpl(albedo, decal_emission, i, + _Decal3_BaseColor, + _Decal3_BaseColor_ST, + _Decal3_Emission_Strength, + _Decal3_Angle); +#endif // _DECAL3 } void mixOverlayAlbedo(inout float3 albedo, PbrOverlay ov) { @@ -666,7 +721,7 @@ float4 effect(inout v2f i) mixOverlayAlbedo(albedo.rgb, ov); #if defined(_DECAL0) || defined(_DECAL1) || defined(_DECAL2) || defined(_DECAL3) float decal_emission = 0; - applyDecalAlbedo(albedo.rgb, decal_emission, i, iddx, iddy); + applyDecalAlbedo(albedo.rgb, decal_emission, i); #endif #if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) |
