From 5a637977a00ad694275dd5820c156cef5b6ff052 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Wed, 1 Jul 2020 01:28:45 -0700 Subject: [PATCH] Major shader overhaul MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • All shaders merged into one using shader variants • new SDF-based antialiasing engine • Smoothness on SDFs is now disabled --- Assets/Shaders/DecalFeatureBumped.shader | 130 --------- Assets/Shaders/DecalPaint.shader | 143 --------- Assets/Shaders/DecalPaintSDF.shader | 159 ----------- Assets/Shaders/DecalPaintSpecular.shader | 165 ----------- Assets/Shaders/DecalPaintSpecularSDF.shader | 180 ------------ Assets/Shaders/DecalsCommon.cginc | 302 ++++++-------------- Assets/Shaders/DecalsLighting.cginc | 33 +++ Assets/Shaders/DecalsSurface.cginc | 184 ++++++++++++ Assets/Shaders/StandardDecal.cginc | 38 +++ Assets/Shaders/StandardDecal.shader | 85 ++++++ 10 files changed, 428 insertions(+), 991 deletions(-) delete mode 100644 Assets/Shaders/DecalFeatureBumped.shader delete mode 100644 Assets/Shaders/DecalPaint.shader delete mode 100644 Assets/Shaders/DecalPaintSDF.shader delete mode 100644 Assets/Shaders/DecalPaintSpecular.shader delete mode 100644 Assets/Shaders/DecalPaintSpecularSDF.shader create mode 100644 Assets/Shaders/DecalsLighting.cginc create mode 100644 Assets/Shaders/DecalsSurface.cginc create mode 100644 Assets/Shaders/StandardDecal.cginc create mode 100644 Assets/Shaders/StandardDecal.shader diff --git a/Assets/Shaders/DecalFeatureBumped.shader b/Assets/Shaders/DecalFeatureBumped.shader deleted file mode 100644 index c670c5b..0000000 --- a/Assets/Shaders/DecalFeatureBumped.shader +++ /dev/null @@ -1,130 +0,0 @@ -Shader "ConformalDecals/Feature/Bumped" -{ - Properties - { - [Header(Texture Maps)] - _Decal("Decal Texture", 2D) = "gray" {} - _DecalBumpMap("Decal Bump Map", 2D) = "bump" {} - - _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 - _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" "IgnoreProjector" = "true"} - 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_PROJECT DECAL_PREVIEW - - sampler2D _Decal; - sampler2D _DecalBumpMap; - - float4 _Decal_ST; - float4 _DecalBumpMap_ST; - - float _RimFalloff; - float4 _RimColor; - - #define DECAL_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 = UnpackNormalDXT5nm(tex2D(_DecalBumpMap, IN.uv_bump)); - - decalClipAlpha(color.a - _Cutoff); - - half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); - float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; - - o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; - o.Alpha = color.a * _DecalOpacity; - o.Emission = emission; - o.Normal = normal; - } - - 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_PROJECT DECAL_PREVIEW - - sampler2D _Decal; - sampler2D _DecalBumpMap; - - float4 _Decal_ST; - float4 _DecalBumpMap_ST; - - float _RimFalloff; - float4 _RimColor; - - #define DECAL_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 = UnpackNormal(tex2D(_DecalBumpMap, IN.uv_bump)); - - decalClipAlpha(color.a - _Cutoff); - - half rim = 1.0 - saturate(dot (normalize(IN.viewDir), normal)); - float3 emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; - - o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; - o.Alpha = color.a * _DecalOpacity; - o.Emission = emission; - o.Normal = normal; - } - - ENDCG - } - - // shadow casting support - UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" - } -} \ No newline at end of file diff --git a/Assets/Shaders/DecalPaint.shader b/Assets/Shaders/DecalPaint.shader deleted file mode 100644 index 754989d..0000000 --- a/Assets/Shaders/DecalPaint.shader +++ /dev/null @@ -1,143 +0,0 @@ -Shader "ConformalDecals/Paint/Diffuse" -{ - 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 - _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" "IgnoreProjector" = "true"} - 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; - - 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); - - 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; - - 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; - - 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); - - 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; - - 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/DecalPaintSDF.shader b/Assets/Shaders/DecalPaintSDF.shader deleted file mode 100644 index 9070a02..0000000 --- a/Assets/Shaders/DecalPaintSDF.shader +++ /dev/null @@ -1,159 +0,0 @@ -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" "IgnoreProjector" = "true"} - 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 deleted file mode 100644 index d577d36..0000000 --- a/Assets/Shaders/DecalPaintSpecular.shader +++ /dev/null @@ -1,165 +0,0 @@ -Shader "ConformalDecals/Paint/Specular" -{ - 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 - _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" "IgnoreProjector" = "true"} - 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 _SpecMap_ST; - - 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); - - 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; - - float wearFactor = 1 - normal.z; - float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); - - color.a *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); - color.a *= _DecalOpacity; - - o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; - o.Alpha = color.a; - o.Emission = emission; - o.Specular = _Shininess; - o.Gloss = specular.r * color.a; - } - - 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 _SpecMap_ST; - - 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; - - decalClipAlpha(color.a - _Cutoff); - - 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/DecalPaintSpecularSDF.shader b/Assets/Shaders/DecalPaintSpecularSDF.shader deleted file mode 100644 index 59575b6..0000000 --- a/Assets/Shaders/DecalPaintSpecularSDF.shader +++ /dev/null @@ -1,180 +0,0 @@ -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" "IgnoreProjector" = "true"} - 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 ba8039a..84a25d2 100644 --- a/Assets/Shaders/DecalsCommon.cginc +++ b/Assets/Shaders/DecalsCommon.cginc @@ -1,26 +1,91 @@ #ifndef DECALS_COMMON_INCLUDED #define DECALS_COMMON_INCLUDED +#include "AutoLight.cginc" +#include "Lighting.cginc" + +#define CLIP_MARGIN 0.1 + +// UNIFORM VARIABLES +// Projection matrix, normal, and tangent vectors +float4x4 _ProjectionMatrix; +float3 _DecalNormal; +float3 _DecalTangent; + +// Common Shading Paramaters +float _Cutoff; +float _DecalOpacity; +float4 _Background; + +sampler2D _Decal; +float4 _Decal_ST; + +// Variant Shading Parameters +#ifdef DECAL_BASE_NORMAL + sampler2D _BumpMap; + float4 _BumpMap_ST; + float _EdgeWearStrength; + float _EdgeWearOffset; +#endif //DECAL_BASE_NORMAL + +#ifdef DECAL_BUMPMAP + sampler2D _BumpMap; + float4 _BumpMap_ST; +#endif //DECAL_BUMPMAP + +#ifdef DECAL_SPECMAP + sampler2D _SpecMap; + float4 _SpecMap_ST; + fixed _Shininess; +#endif //DECAL_SPECMAP + +#ifdef DECAL_EMISSIVE + sampler2D _Emissive; + float4 _Emissive_ST; + fixed4 _Emissive_Color; +#endif //DECAL_EMISSIVE + +// KSP EFFECTS +// opacity and color +float _Opacity; +float4 _Color; +float _RimFalloff; +float4 _RimColor; + +// fog +float4 _LocalCameraPos; +float4 _LocalCameraDir; +float4 _UnderwaterFogColor; +float _UnderwaterMinAlphaFogDistance; +float _UnderwaterMaxAlbedoFog; +float _UnderwaterMaxAlphaFog; +float _UnderwaterAlbedoDistanceScalar; +float _UnderwaterAlphaDistanceScalar; +float _UnderwaterFogFactor; + +// SURFACE INPUT STRUCT struct DecalSurfaceInput { + float3 uv; float2 uv_decal; - #ifdef DECAL_NORMAL - float2 uv_bump; - #endif //DECAL_NORMAL + #ifdef DECAL_BUMPMAP + float2 uv_bumpmap; + #endif //DECAL_BUMPMAP - #ifdef DECAL_SPECULAR - float2 uv_spec; - #endif //DECAL_SPECULAR + #ifdef DECAL_SPECMAP + float2 uv_specmap; + #endif //DECAL_SPECMAP #ifdef DECAL_EMISSIVE - float2 uv_glow; + float2 uv_emissive; #endif //DECAL_EMISSIVE #ifdef DECAL_BASE_NORMAL float3 normal; #endif - + + float3 vertex_normal; float3 viewDir; float3 worldPosition; }; @@ -59,20 +124,6 @@ struct v2f #endif //UNITY_PASS_FORWARDADD }; -// Projection matrix, normal, and tangent vectors -float4x4 _ProjectionMatrix; -float3 _DecalNormal; -float3 _DecalTangent; - -#ifdef DECAL_BASE_NORMAL - sampler2D _BumpMap; - float4 _BumpMap_ST; -#endif //DECAL_BASE_NORMAL - -float _Cutoff; -float _DecalOpacity; -float _Opacity; -float4 _Background; inline void decalClipAlpha(float alpha) { #ifndef DECAL_PREVIEW @@ -88,200 +139,23 @@ inline float CalcMipLevel(float2 texture_coord) { 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) -{ - s.Normal = normalize(s.Normal); - half3 h = normalize(lightDir + viewDir); - - fixed diff = max(0, dot(s.Normal, lightDir)); - - float nh = max(0, dot(s.Normal, h)); - float spec = pow(nh, s.Specular*128.0) * s.Gloss; - - fixed4 c = 0; - c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten); - return c; -} - -// declare surf function, -// this must be defined in any shader using this cginc -void surf (DecalSurfaceInput IN, inout SurfaceOutput o); - -v2f vert_forward(appdata_decal v) -{ - v2f o; - UNITY_INITIALIZE_OUTPUT(v2f,o); - - o.pos = UnityObjectToClipPos(v.vertex); - o.normal = v.normal; - +inline float BoundsDist(float3 p, float3 normal, float3 projNormal) { + float3 q = abs(p - 0.5) - 0.5; // 1x1 square/cube centered at (0.5,0.5) + //float dist = length(max(q,0)) + min(max(q.x,max(q.y,q.z)),0.0); // true SDF #ifdef DECAL_PREVIEW - o.uv_decal = v.texcoord; + return 10 * max(q.x, q.y); // 2D pseudo SDF #else - o.uv_decal = mul (_ProjectionMatrix, v.vertex); - #endif //DECAL_PREVIEW - - #ifdef DECAL_BASE_NORMAL - o.uv_base = TRANSFORM_TEX(v.texcoord, _BumpMap); - #endif //DECAL_BASE_NORMAL - - float3 worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz; - float3 worldNormal = UnityObjectToWorldNormal(v.normal); - - #if defined(DECAL_BASE_NORMAL) || defined(DECAL_PREVIEW) - // use tangent of base geometry - fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); - fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w; - fixed3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign; - #else - // use tangent of projector - fixed3 decalTangent = UnityObjectToWorldDir(_DecalTangent); - fixed3 worldBinormal = cross(decalTangent, worldNormal); - fixed3 worldTangent = cross(worldNormal, worldBinormal); - #endif //defined(DECAL_BASE_NORMAL) || defined(DECAL_PREVIEW) - - o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPosition.x); - o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPosition.y); - o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPosition.z); - - // forward base pass specific lighting code - #ifdef UNITY_PASS_FORWARDBASE - // SH/ambient light - #if UNITY_SHOULD_SAMPLE_SH - float3 shlight = ShadeSH9 (float4(worldNormal,1.0)); - o.vlight = shlight; - #else - o.vlight = 0.0; - #endif // UNITY_SHOULD_SAMPLE_SH - - // vertex light - #ifdef VERTEXLIGHT_ON - o.vlight += Shade4PointLights ( - unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, - unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, - unity_4LightAtten0, worldPosition, worldNormal ); - #endif // VERTEXLIGHT_ON - #endif // UNITY_PASS_FORWARDBASE - - // pass shadow and, possibly, light cookie coordinates to pixel shader - UNITY_TRANSFER_LIGHTING(o, 0.0); - - return o; -} - -fixed4 frag_forward(v2f IN) : SV_Target -{ - // declare data - DecalSurfaceInput i; - SurfaceOutput o; - fixed4 c = 0; - - // setup world-space TBN vectors - UNITY_EXTRACT_TBN(IN); - - float3 worldPosition = float3(IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w); - float3 worldTangent = float3(IN.tSpace0.x, IN.tSpace1.x, IN.tSpace2.x); - - // setup world-space light and view direction vectors - #ifndef USING_DIRECTIONAL_LIGHT - fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPosition)); - #else - fixed3 lightDir = _WorldSpaceLightPos0.xyz; + float dist = max(max(q.x, q.y), q.z); // pseudo SDF + float ndist = -dot(normal, projNormal); // SDF to normal + return 10 * max(dist, ndist); // return intersection #endif - float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPosition)); - float3 viewDir = _unity_tbn_0 * worldViewDir.x + _unity_tbn_1 * worldViewDir.y + _unity_tbn_2 * worldViewDir.z; - - #ifdef DECAL_PREVIEW - fixed4 uv_projected = IN.uv_decal; - #else - // perform decal projection - fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); - - // clip texture outside of xyz bounds - clip(uv_projected.xyz); - clip(1-uv_projected.xyz); - - // clip backsides - clip(dot(_DecalNormal, IN.normal)); - #endif //DECAL_PREVIEW - - // initialize surface input - UNITY_INITIALIZE_OUTPUT(DecalSurfaceInput, i) - i.uv_decal = TRANSFORM_TEX(uv_projected, _Decal); - - #ifdef DECAL_NORMAL - i.uv_bump = TRANSFORM_TEX(uv_projected, _DecalBumpMap); - #endif //DECAL_NORMAL - - #ifdef DECAL_SPECULAR - i.uv_spec = TRANSFORM_TEX(uv_projected, _SpecMap); - #endif //DECAL_SPECULAR - - #ifdef DECAL_EMISSIVE - i.uv_glow = TRANSFORM_TEX(uv_projected, _GlowMap); - #endif //DECAL_EMISSIVE - - #ifdef DECAL_BASE_NORMAL - #ifdef DECAL_PREVIEW - i.normal = fixed3(0,0,1); - #else - i.normal = UnpackNormalDXT5nm(tex2D(_BumpMap, IN.uv_base)); - #endif //DECAL_PREVIEW - #endif //DECAL_BASE_NORMAL - - //i.normal = IN.normal; - i.viewDir = viewDir; - i.worldPosition = worldPosition; - - // initialize surface output - o.Albedo = 0.0; - o.Emission = 0.0; - o.Specular = 0.0; - o.Alpha = 0.0; - o.Gloss = 0.0; - o.Normal = fixed3(0,0,1); - - // call surface function - surf(i, o); - - #ifdef DECAL_PREVIEW - if (any(IN.uv_decal > 1) || any(IN.uv_decal < 0)) o.Alpha = 0; - - o.Albedo = lerp(_Background.rgb, o.Albedo, o.Alpha) * _Color.rgb; - o.Normal = lerp(float3(0,0,1), o.Normal, o.Alpha); - o.Gloss = lerp(_Background.a, o.Gloss, o.Alpha); - o.Emission = lerp(0, o.Emission, o.Alpha); - o.Alpha = _Opacity; - #endif //DECAL_PREVIEW - - // compute lighting & shadowing factor - UNITY_LIGHT_ATTENUATION(atten, IN, worldPosition) - - // compute world normal - float3 WorldNormal; - WorldNormal.x = dot(_unity_tbn_0, o.Normal); - WorldNormal.y = dot(_unity_tbn_1, o.Normal); - WorldNormal.z = dot(_unity_tbn_2, o.Normal); - WorldNormal = normalize(WorldNormal); - o.Normal = WorldNormal; - - //call modified KSP lighting function - c += LightingBlinnPhongDecal(o, lightDir, worldViewDir, atten); - - // Forward base emission and ambient/vertex lighting - #ifdef UNITY_PASS_FORWARDBASE - c.rgb += o.Emission; - c.rgb += o.Albedo * IN.vlight; - c.a = o.Alpha; - #endif //UNITY_PASS_FORWARDBASE - - // Forward add multiply by alpha - #ifdef UNITY_PASS_FORWARDADD - c.rgb *= o.Alpha; - #endif - - return c; } -#endif +inline float SDFAA(float dist) { + float ddist = length(float2(ddx(dist), ddy(dist))); + float pixelDist = dist / ddist; + return saturate(0.5-pixelDist); + return saturate(0.5 - dist); +} + +#endif \ No newline at end of file diff --git a/Assets/Shaders/DecalsLighting.cginc b/Assets/Shaders/DecalsLighting.cginc new file mode 100644 index 0000000..38f766e --- /dev/null +++ b/Assets/Shaders/DecalsLighting.cginc @@ -0,0 +1,33 @@ +#ifndef DECALS_LIGHTING_INCLUDED +#define DECALS_LIGHTING_INCLUDED + +// modifed version of the KSP BlinnPhong because it does some weird things +inline fixed4 LightingBlinnPhongDecal(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) +{ + s.Normal = normalize(s.Normal); + half3 h = normalize(lightDir + viewDir); + + fixed diff = max(0, dot(s.Normal, lightDir)); + + float nh = max(0, dot(s.Normal, h)); + float spec = pow(nh, s.Specular*128.0) * s.Gloss; + + fixed4 c = 0; + c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten); + return c; +} + +// KSP underwater fog function +float4 UnderwaterFog(float3 worldPos, float3 color) +{ + float3 toPixel = worldPos - _LocalCameraPos.xyz; + float toPixelLength = length(toPixel); + + float underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; + float albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate(toPixelLength * _UnderwaterAlbedoDistanceScalar)); + float alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate((toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar)); + + return float4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor); +} + +#endif diff --git a/Assets/Shaders/DecalsSurface.cginc b/Assets/Shaders/DecalsSurface.cginc new file mode 100644 index 0000000..e5c8dd8 --- /dev/null +++ b/Assets/Shaders/DecalsSurface.cginc @@ -0,0 +1,184 @@ +#ifndef DECALS_SURFACE_INCLUDED +#define DECALS_SURFACE_INCLUDED + +#include "DecalsCommon.cginc" +#include "DecalsLighting.cginc" + +// declare surf function, +// this must be defined in any shader using this cginc +void surf (DecalSurfaceInput IN, inout SurfaceOutput o); + +v2f vert_forward(appdata_decal v) +{ + v2f o; + UNITY_INITIALIZE_OUTPUT(v2f,o); + + o.pos = UnityObjectToClipPos(v.vertex); + o.normal = v.normal; + + #ifdef DECAL_PREVIEW + o.uv_decal = v.texcoord; + #else + o.uv_decal = mul (_ProjectionMatrix, v.vertex); + #endif //DECAL_PREVIEW + + #ifdef DECAL_BASE_NORMAL + o.uv_base = TRANSFORM_TEX(v.texcoord, _BumpMap); + #endif //DECAL_BASE_NORMAL + + float3 worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz; + float3 worldNormal = UnityObjectToWorldNormal(v.normal); + + #if defined(DECAL_BASE_NORMAL) || defined(DECAL_PREVIEW) + // use tangent of base geometry + fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); + fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w; + fixed3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign; + #else + // use tangent of projector + fixed3 decalTangent = UnityObjectToWorldDir(_DecalTangent); + fixed3 worldBinormal = cross(decalTangent, worldNormal); + fixed3 worldTangent = cross(worldNormal, worldBinormal); + #endif //defined(DECAL_BASE_NORMAL) || defined(DECAL_PREVIEW) + + o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPosition.x); + o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPosition.y); + o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPosition.z); + + // forward base pass specific lighting code + #ifdef UNITY_PASS_FORWARDBASE + // SH/ambient light + #if UNITY_SHOULD_SAMPLE_SH + float3 shlight = ShadeSH9 (float4(worldNormal,1.0)); + o.vlight = shlight; + #else + o.vlight = 0.0; + #endif // UNITY_SHOULD_SAMPLE_SH + + // vertex light + #ifdef VERTEXLIGHT_ON + o.vlight += Shade4PointLights ( + unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, + unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, + unity_4LightAtten0, worldPosition, worldNormal ); + #endif // VERTEXLIGHT_ON + #endif // UNITY_PASS_FORWARDBASE + + // pass shadow and, possibly, light cookie coordinates to pixel shader + UNITY_TRANSFER_LIGHTING(o, 0.0); + + return o; +} + +fixed4 frag_forward(v2f IN) : SV_Target +{ + #ifdef DECAL_PREVIEW + fixed4 uv_projected = IN.uv_decal; + #else + // perform decal projection + fixed4 uv_projected = UNITY_PROJ_COORD(IN.uv_decal); + + clip(uv_projected.xyz + CLIP_MARGIN); + clip(CLIP_MARGIN + (1-uv_projected.xyz)); + #endif //DECAL_PREVIEW + + // declare data + DecalSurfaceInput i; + SurfaceOutput o; + fixed4 c = 0; + + // setup world-space TBN vectors + UNITY_EXTRACT_TBN(IN); + + float3 worldPosition = float3(IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w); + float3 worldTangent = float3(IN.tSpace0.x, IN.tSpace1.x, IN.tSpace2.x); + + // setup world-space light and view direction vectors + #ifndef USING_DIRECTIONAL_LIGHT + fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPosition)); + #else + fixed3 lightDir = _WorldSpaceLightPos0.xyz; + #endif + float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPosition)); + float3 viewDir = _unity_tbn_0 * worldViewDir.x + _unity_tbn_1 * worldViewDir.y + _unity_tbn_2 * worldViewDir.z; + + // initialize surface input + UNITY_INITIALIZE_OUTPUT(DecalSurfaceInput, i) + i.uv_decal = TRANSFORM_TEX(uv_projected, _Decal); + i.uv = uv_projected; + + #ifdef DECAL_BUMPMAP + i.uv_bumpmap = TRANSFORM_TEX(uv_projected, _BumpMap); + #endif //DECAL_BUMPMAP + + #ifdef DECAL_SPECMAP + i.uv_specmap = TRANSFORM_TEX(uv_projected, _SpecMap); + #endif //DECAL_SPECMAP + + #ifdef DECAL_EMISSIVE + i.uv_emissive = TRANSFORM_TEX(uv_projected, _Emissive); + #endif //DECAL_EMISSIVE + + #ifdef DECAL_BASE_NORMAL + #ifdef DECAL_PREVIEW + i.normal = fixed3(0,0,1); + #else + i.normal = UnpackNormalDXT5nm(tex2D(_BumpMap, IN.uv_base)); + #endif //DECAL_PREVIEW + #endif //DECAL_BASE_NORMAL + + i.vertex_normal = IN.normal; + i.viewDir = viewDir; + i.worldPosition = worldPosition; + + // initialize surface output + o.Albedo = 0.0; + o.Emission = 0.0; + o.Specular = 0.0; + o.Alpha = 0.0; + o.Gloss = 0.0; + o.Normal = fixed3(0,0,1); + + // call surface function + surf(i, o); + + #ifdef DECAL_PREVIEW + if (any(IN.uv_decal > 1) || any(IN.uv_decal < 0)) o.Alpha = 0; + + o.Albedo = lerp(_Background.rgb, o.Albedo, o.Alpha) * _Color.rgb; + o.Normal = lerp(float3(0,0,1), o.Normal, o.Alpha); + o.Gloss = lerp(_Background.a, o.Gloss, o.Alpha); + o.Emission = lerp(0, o.Emission, o.Alpha); + o.Alpha = _Opacity; + #endif //DECAL_PREVIEW + + // compute lighting & shadowing factor + UNITY_LIGHT_ATTENUATION(atten, IN, worldPosition) + + // compute world normal + float3 WorldNormal; + WorldNormal.x = dot(_unity_tbn_0, o.Normal); + WorldNormal.y = dot(_unity_tbn_1, o.Normal); + WorldNormal.z = dot(_unity_tbn_2, o.Normal); + WorldNormal = normalize(WorldNormal); + o.Normal = WorldNormal; + + //call modified KSP lighting function + c += LightingBlinnPhongDecal(o, lightDir, worldViewDir, atten); + + // Forward base emission and ambient/vertex lighting + #ifdef UNITY_PASS_FORWARDBASE + c.rgb += o.Emission; + c.rgb += o.Albedo * IN.vlight; + c.a = o.Alpha; + #endif //UNITY_PASS_FORWARDBASE + + // Forward add multiply by alpha + #ifdef UNITY_PASS_FORWARDADD + c.rgb *= o.Alpha; + #endif + + return c; +} + +#endif \ No newline at end of file diff --git a/Assets/Shaders/StandardDecal.cginc b/Assets/Shaders/StandardDecal.cginc new file mode 100644 index 0000000..2b8ac7b --- /dev/null +++ b/Assets/Shaders/StandardDecal.cginc @@ -0,0 +1,38 @@ +void surf(DecalSurfaceInput IN, inout SurfaceOutput o) { + float4 color = tex2D(_Decal, IN.uv_decal); + o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb; + o.Alpha = _DecalOpacity; + + #ifdef DECAL_BASE_NORMAL + float3 normal = IN.normal; + float wearFactor = 1 - normal.z; + float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor); + o.Alpha *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor)); + #endif + + #ifdef DECAL_BUMPMAP + o.Normal = tex2D(_BumpMap, IN.uv_bumpmap); + #endif + + #ifdef DECAL_SPECMAP + float4 specular = tex2D(_SpecMap, IN.uv_specmap); + o.Gloss = specular.r; + o.Specular = _Shininess; + #endif + + half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal)); + o.Emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a; + + #ifdef DECAL_EMISSIVE + o.Emission += tex2D(_Emissive, IN.uv_emissive).rgb * _Emissive_Color.rgb * _Emissive_Color.a; + #endif + + float dist = BoundsDist(IN.uv, IN.vertex_normal, _DecalNormal); + #ifdef DECAL_SDF_ALPHA + float decalDist = _Cutoff - color.a; + o.Alpha *= SDFAA(max(decalDist, dist)); + #else + o.Alpha *= SDFAA(dist); + o.Alpha *= color.a; + #endif +} \ No newline at end of file diff --git a/Assets/Shaders/StandardDecal.shader b/Assets/Shaders/StandardDecal.shader new file mode 100644 index 0000000..cc59fbe --- /dev/null +++ b/Assets/Shaders/StandardDecal.shader @@ -0,0 +1,85 @@ +Shader "ConformalDecals/Decal/Standard" +{ + 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 + _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" "IgnoreProjector" = "true"} + 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_local __ DECAL_PREVIEW + #pragma multi_compile_local __ DECAL_BASE_NORMAL DECAL_BUMPMAP + #pragma multi_compile_local __ DECAL_SPECMAP + #pragma multi_compile_local __ DECAL_EMISSIVE + #pragma multi_compile_local __ DECAL_SDF_ALPHA + + #include "UnityCG.cginc" + #include "DecalsCommon.cginc" + #include "DecalsSurface.cginc" + #include "StandardDecal.cginc" + + ENDCG + } + + Pass + { + Name "FORWARD_ADD" + 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_local __ DECAL_PREVIEW + #pragma multi_compile_local __ DECAL_BASE_NORMAL DECAL_BUMPMAP + #pragma multi_compile_local __ DECAL_SPECMAP + #pragma multi_compile_local __ DECAL_EMISSIVE + #pragma multi_compile_local __ DECAL_SDF_ALPHA + + #include "UnityCG.cginc" + #include "DecalsCommon.cginc" + #include "DecalsSurface.cginc" + #include "StandardDecal.cginc" + + ENDCG + } + + // shadow casting support + UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" + } +} \ No newline at end of file