diff --git a/Assets/Shaders/DecalPaintSDF.shader b/Assets/Shaders/DecalPaintSDF.shader new file mode 100644 index 0000000..bf69581 --- /dev/null +++ b/Assets/Shaders/DecalPaintSDF.shader @@ -0,0 +1,159 @@ +Shader "ConformalDecals/Paint/DiffuseSDF" +{ + Properties + { + [Header(Texture Maps)] + _Decal("Decal Texture", 2D) = "gray" {} + _BumpMap("Bump Map", 2D) = "bump" {} + + _EdgeWearStrength("Edge Wear Strength", Range(0,500)) = 100 + _EdgeWearOffset("Edge Wear Offset", Range(0,1)) = 0.1 + + _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 + _Smoothness ("SDF smoothness", Range(0,1)) = 0.15 + _SmoothnessMipScale ("Smoothness fadeout", Range(0,1)) = 0.1 + _DecalOpacity("Opacity", Range(0,1) ) = 1 + _Background("Background Color", Color) = (0.9,0.9,0.9,0.7) + + [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", int) = 2 + [Toggle(DECAL_PREVIEW)] _Preview ("Preview", int) = 0 + + [Header(Effects)] + [PerRendererData]_Opacity("_Opacity", Range(0,1) ) = 1 + [PerRendererData]_Color("_Color", Color) = (1,1,1,1) + [PerRendererData]_RimFalloff("_RimFalloff", Range(0.01,5) ) = 0.1 + [PerRendererData]_RimColor("_RimColor", Color) = (0,0,0,0) + [PerRendererData]_UnderwaterFogFactor ("Underwater Fog Factor", Range(0,1)) = 0 + } + SubShader + { + Tags { "Queue" = "Geometry+100" } + Cull [_Cull] + Ztest LEqual + + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardBase" } + Blend SrcAlpha OneMinusSrcAlpha + + CGPROGRAM + #pragma vertex vert_forward + #pragma fragment frag_forward + + #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap + #pragma multi_compile __ DECAL_PREVIEW + + sampler2D _Decal; + + float4 _Decal_ST; + float4 _Decal_TexelSize; + + float _Smoothness; + float _SmoothnessMipScale; + + float _EdgeWearStrength; + float _EdgeWearOffset; + + float _RimFalloff; + float4 _RimColor; + + #define DECAL_BASE_NORMAL + + #include "UnityCG.cginc" + #include "Lighting.cginc" + #include "AutoLight.cginc" + #include "LightingKSP.cginc" + #include "DecalsCommon.cginc" + + void surf (DecalSurfaceInput IN, inout SurfaceOutput o) + { + float4 color = tex2D(_Decal, IN.uv_decal); + float3 normal = IN.normal; + + float smoothScale = (1 - saturate(1-(CalcMipLevel(IN.uv_decal * _Decal_TexelSize.zw) * _SmoothnessMipScale))) / 2; + color.a = smoothstep(_Cutoff - smoothScale, saturate(_Smoothness + smoothScale + _Cutoff), color.a); + + decalClipAlpha(color.a); + + half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); + float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; + + float wearFactor = 1 - normal.z; + float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); + + color.a *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); + + o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; + o.Alpha = color.a * _DecalOpacity; + o.Emission = emission; + } + + ENDCG + } + + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardAdd" } + Blend One One + + CGPROGRAM + #pragma vertex vert_forward + #pragma fragment frag_forward + + #pragma multi_compile_fwdadd nolightmap nodirlightmap nodynlightmap + #pragma multi_compile __ DECAL_PREVIEW + + sampler2D _Decal; + + float4 _Decal_ST; + float4 _Decal_TexelSize; + + float _Smoothness; + float _SmoothnessMipScale; + + float _EdgeWearStrength; + float _EdgeWearOffset; + + float _RimFalloff; + float4 _RimColor; + + #define DECAL_BASE_NORMAL + + #include "UnityCG.cginc" + #include "Lighting.cginc" + #include "AutoLight.cginc" + #include "LightingKSP.cginc" + #include "DecalsCommon.cginc" + + void surf (DecalSurfaceInput IN, inout SurfaceOutput o) + { + float4 color = tex2D(_Decal, IN.uv_decal); + float3 normal = IN.normal; + + float smoothScale = (1 - saturate(1-(CalcMipLevel(IN.uv_decal * _Decal_TexelSize.zw) * _SmoothnessMipScale))) / 2; + color.a = smoothstep(_Cutoff - smoothScale, saturate(_Smoothness + smoothScale + _Cutoff), color.a); + + decalClipAlpha(color.a); + + half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); + float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; + + float wearFactor = 1 - normal.z; + float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); + + color.a *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); + + o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; + o.Alpha = color.a * _DecalOpacity; + o.Emission = emission; + } + + ENDCG + } + + // shadow casting support + UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" + } +} \ No newline at end of file diff --git a/Assets/Shaders/DecalPaintSpecular.shader b/Assets/Shaders/DecalPaintSpecular.shader index 4383382..0c38b0e 100644 --- a/Assets/Shaders/DecalPaintSpecular.shader +++ b/Assets/Shaders/DecalPaintSpecular.shader @@ -74,13 +74,10 @@ Shader "ConformalDecals/Paint/Specular" { float4 color = tex2D(_Decal, IN.uv_decal); float3 specular = tex2D(_SpecMap, IN.uv_spec); - float3 normal = IN.normal; - #ifdef DECAL_PROJECT - // clip alpha - clip(color.a - _Cutoff + 0.01); - #endif //DECAL_PROJECT - + decalClipAlpha(color.a - _Cutoff); + + float3 normal = IN.normal; half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; @@ -142,10 +139,7 @@ Shader "ConformalDecals/Paint/Specular" float3 specular = tex2D(_SpecMap, IN.uv_spec); float3 normal = IN.normal; - #ifdef DECAL_PROJECT - // clip alpha - clip(color.a - _Cutoff + 0.01); - #endif //DECAL_PROJECT + decalClipAlpha(color.a - _Cutoff); half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; diff --git a/Assets/Shaders/DecalPaintSpecularSDF.shader b/Assets/Shaders/DecalPaintSpecularSDF.shader new file mode 100644 index 0000000..01e1544 --- /dev/null +++ b/Assets/Shaders/DecalPaintSpecularSDF.shader @@ -0,0 +1,180 @@ +Shader "ConformalDecals/Paint/SpecularSDF" +{ + Properties + { + [Header(Texture Maps)] + _Decal("Decal Texture", 2D) = "gray" {} + _BumpMap("Bump Map", 2D) = "bump" {} + _SpecMap("Specular Map", 2D) = "black" {} + + _EdgeWearStrength("Edge Wear Strength", Range(0,500)) = 100 + _EdgeWearOffset("Edge Wear Offset", Range(0,1)) = 0.1 + + _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 + _Smoothness ("SDF smoothness", Range(0,1)) = 0.15 + _SmoothnessMipScale ("Smoothness fadeout", Range(0,1)) = 0.1 + _DecalOpacity("Opacity", Range(0,1) ) = 1 + _Background("Background Color", Color) = (0.9,0.9,0.9,0.7) + + [Header(Specularity)] + _SpecColor ("_SpecColor", Color) = (0.25, 0.25, 0.25, 1) + _Shininess ("Shininess", Range (0.03, 10)) = 0.3 + + [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", int) = 2 + [Toggle(DECAL_PREVIEW)] _Preview ("Preview", int) = 0 + + [Header(Effects)] + [PerRendererData]_Opacity("_Opacity", Range(0,1) ) = 1 + [PerRendererData]_Color("_Color", Color) = (1,1,1,1) + [PerRendererData]_RimFalloff("_RimFalloff", Range(0.01,5) ) = 0.1 + [PerRendererData]_RimColor("_RimColor", Color) = (0,0,0,0) + [PerRendererData]_UnderwaterFogFactor ("Underwater Fog Factor", Range(0,1)) = 0 + } + SubShader + { + Tags { "Queue" = "Geometry+100" } + Cull [_Cull] + Ztest LEqual + + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardBase" } + Blend SrcAlpha OneMinusSrcAlpha + + CGPROGRAM + #pragma vertex vert_forward + #pragma fragment frag_forward + + #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap + #pragma multi_compile __ DECAL_PREVIEW + + sampler2D _Decal; + sampler2D _SpecMap; + + float4 _Decal_ST; + float4 _Decal_TexelSize; + float4 _SpecMap_ST; + + float _Smoothness; + float _SmoothnessMipScale; + + float _EdgeWearStrength; + float _EdgeWearOffset; + + half _Shininess; + + float _RimFalloff; + float4 _RimColor; + + #define DECAL_BASE_NORMAL + #define DECAL_SPECULAR + + #include "UnityCG.cginc" + #include "Lighting.cginc" + #include "AutoLight.cginc" + #include "LightingKSP.cginc" + #include "DecalsCommon.cginc" + + void surf (DecalSurfaceInput IN, inout SurfaceOutput o) + { + float4 color = tex2D(_Decal, IN.uv_decal); + float3 specular = tex2D(_SpecMap, IN.uv_spec); + float3 normal = IN.normal; + + float smoothScale = (1 - saturate(1-(CalcMipLevel(IN.uv_decal * _Decal_TexelSize.zw) * _SmoothnessMipScale))) / 2; + color.a = smoothstep(_Cutoff - smoothScale, saturate(_Smoothness + smoothScale + _Cutoff), color.a); + + decalClipAlpha(color.a); + + half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); + float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; + + float wearFactor = 1 - normal.z; + float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); + + color.a *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); + + o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; + o.Alpha = color.a * _DecalOpacity; + o.Emission = emission; + o.Specular = _Shininess; + o.Gloss = specular.r; + } + + ENDCG + } + + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardAdd" } + Blend One One + + CGPROGRAM + #pragma vertex vert_forward + #pragma fragment frag_forward + + #pragma multi_compile_fwdadd nolightmap nodirlightmap nodynlightmap + #pragma multi_compile __ DECAL_PREVIEW + + sampler2D _Decal; + sampler2D _SpecMap; + + float4 _Decal_ST; + float4 _Decal_TexelSize; + float4 _SpecMap_ST; + + float _Smoothness; + float _SmoothnessMipScale; + + float _EdgeWearStrength; + float _EdgeWearOffset; + + half _Shininess; + + float _RimFalloff; + float4 _RimColor; + + #define DECAL_BASE_NORMAL + #define DECAL_SPECULAR + + #include "UnityCG.cginc" + #include "Lighting.cginc" + #include "AutoLight.cginc" + #include "LightingKSP.cginc" + #include "DecalsCommon.cginc" + + void surf (DecalSurfaceInput IN, inout SurfaceOutput o) + { + float4 color = tex2D(_Decal, IN.uv_decal); + float3 specular = tex2D(_SpecMap, IN.uv_spec); + float3 normal = IN.normal; + + float smoothScale = (1 - saturate(1-(CalcMipLevel(IN.uv_decal * _Decal_TexelSize.zw) * _SmoothnessMipScale))) / 2; + color.a = smoothstep(_Cutoff - smoothScale, saturate(_Smoothness + smoothScale + _Cutoff), color.a); + + decalClipAlpha(color.a); + + half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); + float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; + + float wearFactor = 1 - normal.z; + float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); + + color.a *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); + + o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; + o.Alpha = color.a * _DecalOpacity; + o.Emission = emission; + o.Specular = _Shininess; + o.Gloss = specular.r; + } + + ENDCG + } + + // shadow casting support + UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" + } +} \ No newline at end of file diff --git a/Assets/Shaders/DecalsCommon.cginc b/Assets/Shaders/DecalsCommon.cginc index 335696b..68dab09 100644 --- a/Assets/Shaders/DecalsCommon.cginc +++ b/Assets/Shaders/DecalsCommon.cginc @@ -76,10 +76,18 @@ float4 _Background; inline void decalClipAlpha(float alpha) { #ifndef DECAL_PREVIEW - clip(alpha - _Cutoff + 0.01); + clip(alpha - 0.001); #endif } +inline float CalcMipLevel(float2 texture_coord) { + float2 dx = ddx(texture_coord); + float2 dy = ddy(texture_coord); + float delta_max_sqr = max(dot(dx, dx), dot(dy, dy)); + + return 0.5 * log2(delta_max_sqr); +} + // modifed version of the KSP BlinnPhong because it does some weird things inline fixed4 LightingBlinnPhongDecal(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) { diff --git a/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab b/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab index afe0877..21cdaee 100644 Binary files a/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab and b/Distribution/GameData/ConformalDecals/Resources/conformaldecals.shab differ