Advertisement
Guest User

DecalShader.shader

a guest
Apr 7th, 2021
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://www.popekim.com/2012/10/siggraph-2012-screen-space-decals-in.html
  2.  
  3. Shader "Decal/DecalShader"
  4. {
  5.     Properties
  6.     {
  7.         _LightStrength("Point/Spot Light Strength", Range(0,1)) = 0.5
  8.     }
  9.         SubShader
  10.        
  11.     {
  12.         Tags {"Queue"="Transparent"}
  13.         Pass
  14.     {
  15.         Tags{ "LightMode" = "ForwardBase" }
  16.  
  17.         ZWrite On
  18.         Blend SrcAlpha OneMinusSrcAlpha
  19.        
  20. // uncomment to have selective decals
  21. // NOTE: Also uncomment on the SECOND PASS down below
  22.         // Stencil {
  23.         // Ref 5
  24.         // Comp Equal
  25.         // Fail zero
  26.         // }
  27.  
  28. // Then add the following to the shader you want the decals to show up on, not this one!:
  29.     //  Stencil {
  30.     //       Ref 5
  31.     //          Comp always
  32.     //      Pass Replace
  33.     //      }
  34.  
  35.     CGPROGRAM
  36.  
  37.     #include "UnityCG.cginc"
  38.     #include "AutoLight.cginc"
  39.     #include "Lighting.cginc"
  40.     #pragma multi_compile_fog
  41.     #pragma multi_compile_fwdbase_fullshadows
  42.     #pragma vertex vert
  43.     #pragma fragment frag
  44.  
  45.         struct v2f
  46.         {
  47.             float4 pos : SV_POSITION;
  48.             half2 uv : TEXCOORD0;
  49.             float4 screenUV : TEXCOORD1;
  50.             float3 ray : TEXCOORD2;
  51.             UNITY_FOG_COORDS(4)
  52.             LIGHTING_COORDS(5, 6)
  53.         };
  54.  
  55.         v2f vert(appdata_full v)
  56.         {
  57.             v2f o;
  58.             o.pos = UnityObjectToClipPos(v.vertex);
  59.             o.uv = v.texcoord;
  60.             o.screenUV = ComputeScreenPos(o.pos);
  61.             o.ray = UnityObjectToViewPos(float4(v.vertex.xyz,0)).xyz * float3(-1,-1,1);
  62.             UNITY_TRANSFER_FOG(o, o.pos);
  63.             TRANSFER_VERTEX_TO_FRAGMENT(o);
  64.             return o;
  65.         }
  66.  
  67.  
  68.         UNITY_INSTANCING_BUFFER_START(Props)
  69.         UNITY_DEFINE_INSTANCED_PROP(sampler2D, _MainTex)
  70.         UNITY_DEFINE_INSTANCED_PROP(float4, _Tint)
  71.         UNITY_INSTANCING_BUFFER_END(Props)
  72.  
  73.         sampler2D _CameraDepthTexture;
  74.         sampler2D_float _DirectionalShadowMap;
  75.         SamplerState sampler_DirectionalShadowMap;
  76.  
  77.         fixed4 frag(v2f i) : SV_Target
  78.         {
  79.         i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
  80.  
  81.         //Screenspace UV
  82.         float2 uv = i.screenUV.xy / i.screenUV.w;
  83.         // read depth
  84.         float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
  85.         depth = Linear01Depth(depth);
  86.  
  87.  
  88.         // reconstruct world space
  89.         float4 vpos = float4(i.ray * depth, 1);
  90.         float4 wpos = mul(unity_CameraToWorld, vpos);
  91.         float3 opos = mul(unity_WorldToObject, float4(wpos)).xyz;
  92.         clip(float3(0.5, 0.5, 0.5) - abs(opos.xyz));
  93.  
  94.         // offset uvs
  95.         i.uv = opos.xz + 0.5;
  96.  
  97.         // add texture from decal script
  98.         fixed4 col = tex2D(UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex), i.uv);
  99.         // discard outside of texture alpha
  100.         clip(col.a - 0.1);
  101.         col *= col.a;
  102.  
  103.         // directional light shadows from commandbuffer
  104.         float shadow = tex2D(_DirectionalShadowMap, uv).r;
  105.  
  106.         // multiply with light color and shadows, ambient
  107.         col *= (_LightColor0 * shadow) + unity_AmbientSky;
  108.  
  109.         // add tinting and transparency
  110.         col.rgb *= _Tint.rgb;
  111.         col *= _Tint.a;
  112.  
  113.         // add in the fog
  114.         UNITY_APPLY_FOG(i.fogCoord, col.rgb);
  115.  
  116.         return saturate(col);
  117.     }
  118.          ENDCG
  119.     }
  120.  
  121.      Pass{
  122.         // secondpass for the extra lights
  123.         Tags{ "LightMode" = "ForwardAdd" }
  124.         // Soft additive
  125.         Blend OneMinusDstColor One
  126.  
  127. // uncomment to have selective decals
  128.         // Stencil{
  129.         //  Ref 5
  130.         //  Comp Equal
  131.         //  Fail zero
  132.         // }
  133.  
  134.    
  135.  
  136.         CGPROGRAM
  137. #pragma vertex vert
  138. #pragma fragment frag
  139. #pragma multi_compile_fwdadd
  140. #include "UnityCG.cginc"
  141. #include "AutoLight.cginc"
  142. #include "Lighting.cginc"
  143.  
  144.                     float4 _MainTex_ST;
  145.  
  146.                     struct v2f
  147.                     {
  148.                         float4 pos : SV_POSITION;
  149.                         half2 uv : TEXCOORD0;
  150.                         float4 screenUV : TEXCOORD1;
  151.                         float3 ray : TEXCOORD2;
  152.                         float3 normal : NORMAL;
  153.                         float3 lightDir : TEXCOORD3;
  154.                         LIGHTING_COORDS(5, 6)
  155.                     };
  156.  
  157.                     v2f vert(appdata_full v)
  158.                     {
  159.                         v2f o;
  160.                         o.pos = UnityObjectToClipPos(v.vertex);
  161.                         o.uv = v.vertex.xz + 0.5;
  162.                         o.normal = v.normal;
  163.                         o.screenUV = ComputeScreenPos(o.pos);
  164.                         o.lightDir = ObjSpaceLightDir(v.vertex).xyz;
  165.                         o.ray = UnityObjectToViewPos(float4(v.vertex.xyz, 1)).xyz * float3(-1, -1, 1);
  166.                         TRANSFER_VERTEX_TO_FRAGMENT(o);
  167.                         return o;
  168.                     }
  169.  
  170.                     float _LightStrength;
  171.                     sampler2D_float _CameraDepthTexture;
  172.                     sampler2D_float _DirectionalShadowMap;
  173.                     SamplerState sampler_DirectionalShadowMap;
  174.  
  175.                     UNITY_INSTANCING_BUFFER_START(Props)
  176.                     UNITY_DEFINE_INSTANCED_PROP(sampler2D, _MainTex)
  177.                     UNITY_DEFINE_INSTANCED_PROP(float4, _Tint)
  178.  
  179.                     UNITY_INSTANCING_BUFFER_END(Props)
  180.  
  181.                     fixed4 frag(v2f i) : SV_Target
  182.                     {
  183.                         i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
  184.                         float2 uv = i.screenUV.xy / i.screenUV.w;
  185.  
  186.                         // read depth and reconstruct world position
  187.                         float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
  188.                         depth = Linear01Depth(depth);
  189.  
  190.                         float4 vpos = float4(i.ray * depth,1);
  191.                         float3 wpos = mul(unity_CameraToWorld, vpos).xyz;
  192.                         float3 opos = mul(unity_WorldToObject, float4(wpos,1)).xyz;
  193.                         // clip off most of the cube
  194.                         clip(float3(0.5,0.5,0.5) - abs(opos.xyz));
  195.  
  196.                         // offset uvs
  197.                         i.uv = opos.xz + 0.5;
  198.                         // add texture from decal script
  199.                         fixed4 col = tex2D(UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex), i.uv);
  200.                         // discard outside of texture alpha
  201.                         clip(col.a - 0.1);
  202.                         col *= col.a;
  203.                         // directional shadows again
  204.                         float shadow = tex2D(_DirectionalShadowMap, uv).r;
  205.                         col.rgb *= saturate(shadow + 0.1);
  206.                         // additional lights attenuation
  207.                         fixed atten = LIGHT_ATTENUATION(i);
  208.  
  209.                         // add in colors and attenuation, multiply with optional light strength
  210.                         col += (_LightColor0 * atten * col.a) * _LightStrength;
  211.                         col *= atten;
  212.                         return col;
  213.                         }
  214.                         ENDCG
  215.                     }
  216.  
  217.     }
  218.  
  219.         Fallback "VertexLit"
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement