Advertisement
Guest User

StencilDecal.shader

a guest
Dec 3rd, 2023
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Stencil Decal"
  2. {
  3.     Properties
  4.     {
  5.         _Intensity("Intensity", Range(0,10)) = 5.0
  6.    
  7.         [Header(Movement)]
  8.         _SpeedX("Speed X", Range(-5,5)) = 1.0
  9.         _SpeedY("Speed Y", Range(-5,5)) = 1.0
  10.         _RadialScale("Radial Scale", Range(0,10)) = 1.0
  11.         _LengthScale("Length Scale", Range(0,10)) = 1.0
  12.         _MovingTex ("MovingTex", 2D) = "white" {}
  13.         _Multiply("Multiply Moving", Range(0,10)) = 1.0
  14.        
  15.         [Header(Shape)]
  16.         _ShapeTex("Shape Texture", 2D) = "white" {}
  17.         _ShapeTexIntensity("Shape tex intensity", Range(0,6)) = 0.5
  18.        
  19.         [Header(Gradient Coloring)]
  20.         _Gradient("Gradient Texture", 2D) = "white" {}
  21.         _Stretch("Gradient Stretch", Range(-2,10)) = 1.0
  22.         _Offset("Gradient Offset", Range(-2,10)) = 1.0
  23.  
  24.         [Header(Cutoff)]   
  25.         _Cutoff("Outside Cutoff", Range(0,1)) = 1.0
  26.         _Smoothness("Outside Smoothness", Range(0,1)) = 1.0
  27.  
  28.     }  
  29.     SubShader
  30.     {        
  31.         Tags
  32.         {
  33.             "Queue" = "Geometry+1"          
  34.         }
  35.         CGINCLUDE
  36.         #include "UnityCG.cginc"
  37.         #include "AutoLight.cginc"
  38.         #include "Lighting.cginc"
  39.  
  40.         float _Cutoff, _Smoothness;
  41.         sampler2D _MovingTex;
  42.         float _SpeedX, _SpeedY;
  43.         sampler2D _ShapeTex;
  44.         float _ShapeTexIntensity;
  45.         sampler2D _Gradient;
  46.         float _Stretch,_Multiply;
  47.         float _Intensity,_Offset;
  48.         float _RadialScale, _LengthScale;
  49.  
  50.         UNITY_INSTANCING_BUFFER_START(Props)
  51.         UNITY_DEFINE_INSTANCED_PROP(float4, _Tint)
  52.         UNITY_INSTANCING_BUFFER_END(Props)
  53.  
  54.         sampler2D _CameraDepthTexture;
  55.  
  56.         // helper functions
  57.         float2 Unity_PolarCoordinates(float2 UV, float2 Center, float RadialScale, float LengthScale)
  58.         {
  59.             float2 delta = UV - Center;
  60.             float radius = length(delta) * 2.0 * RadialScale;
  61.             float angle = atan2(delta.y, delta.x) * 1.0/6.28318 * LengthScale;
  62.             return float2(radius, angle);
  63.         }
  64.  
  65.         float3 ReconstructObjectPosition(float3 viewPos, float4 screenUV)
  66.         {
  67.        
  68.             viewPos = viewPos * (_ProjectionParams.z / viewPos.z) ;
  69.  
  70.             //Screenspace UV
  71.             float2 uv = screenUV.xy / screenUV.w ;
  72.                
  73.             // read depth
  74.             float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);   
  75.             float depthLinear = Linear01Depth(depth);
  76.  
  77.             // reconstruct world space
  78.             float4 vpos = float4(viewPos * depthLinear, 1);
  79.             float4 wpos = mul(unity_CameraToWorld, vpos);
  80.             // back to object space
  81.             float3 opos = mul(unity_WorldToObject, float4(wpos)).xyz;
  82.             return opos;
  83.         }
  84.  
  85.         float GetFinalDistortion(float2 uvProj, float shapeTex)
  86.         {
  87.             fixed2 polarUV = Unity_PolarCoordinates(uvProj, float2(0.5, 0.5), _RadialScale, _LengthScale);
  88.                
  89.             // move UV
  90.             float2 movingUV = float2(polarUV.x + (_Time.x * _SpeedX), polarUV.y +(_Time.x * _SpeedY));
  91.  
  92.  
  93.             // final moving texture with the distortion
  94.             fixed4 final =  tex2D(_MovingTex, movingUV).r;
  95.  
  96.             shapeTex *= _ShapeTexIntensity;
  97.             final *= shapeTex;
  98.             return final;
  99.         }
  100.        
  101.         struct v2f
  102.         {
  103.             float4 pos : SV_POSITION;
  104.             half3 uv : TEXCOORD0;
  105.             float4 screenUV : TEXCOORD1;
  106.             float3 viewPos : TEXCOORD2;
  107.         };
  108.  
  109.         v2f vert(appdata_full v)
  110.         {
  111.             v2f o;
  112.             o.pos = UnityObjectToClipPos(v.vertex);
  113.             o.uv = v.texcoord;
  114.             float2 uv = v.texcoord.xy;
  115.             o.screenUV = ComputeScreenPos(o.pos);  
  116.             o.viewPos = UnityObjectToViewPos(float4(v.vertex.xyz,0)).xyz * float3(-1,-1,1);
  117.             TRANSFER_VERTEX_TO_FRAGMENT(o);
  118.             return o;
  119.         }
  120.  
  121.         fixed4 fragStencilMask(v2f i) : SV_Target
  122.         {
  123.             float3 opos = ReconstructObjectPosition(i.viewPos, i.screenUV);
  124.                
  125.             // clip outside of the decal cube
  126.             clip(float3(0.5,0.5,0.5)- abs(opos.xyz));
  127.  
  128.             // offset uvs
  129.             float2 uvProj = opos.xz + 0.5; 
  130.             // get the main shape texture for the alpha
  131.             fixed shapeTex = tex2D(_ShapeTex, uvProj).r;   
  132.  
  133.             float vortexEffect = GetFinalDistortion(uvProj, shapeTex);             
  134.                    
  135.             // discard outside of texture alpha
  136.             clip(vortexEffect- 0.1);
  137.             return float4(1,1,1,1);
  138.         }
  139.  
  140.          fixed4 frag(v2f i) : SV_Target
  141.         {
  142.             // get the world position from depth
  143.             float3 opos = ReconstructObjectPosition(i.viewPos, i.screenUV);
  144.  
  145.             clip(float3(0.5,0.5,0.5)- abs(opos.xyz));
  146.  
  147.             // offset uvs
  148.             float2 uvProj = opos.xz + 0.5;
  149.             // get the main shape texture for the alpha
  150.             fixed shapeTex = tex2D(_ShapeTex, uvProj).r;
  151.                
  152.             fixed vortexEffect =  GetFinalDistortion(uvProj, shapeTex);
  153.            
  154.             // add the coloring from the gradient map
  155.             float4 gradientmap = tex2D(_Gradient, (vortexEffect * _Stretch) + _Offset) * _Intensity ;
  156.             gradientmap *= vortexEffect;
  157.             gradientmap *= _Tint;  
  158.                
  159.             // add tinting and transparency
  160.             gradientmap.rgb *= _Tint.rgb;
  161.             gradientmap *= _Tint.a;
  162.             gradientmap *= shapeTex;
  163.  
  164.             // create a cutoff point for the outside of the portal effect
  165.             gradientmap *= smoothstep(_Cutoff-_Smoothness,_Cutoff, vortexEffect * _Multiply);
  166.             // increase intensity
  167.             gradientmap = saturate(gradientmap * 10) * _Intensity;
  168.             return gradientmap;
  169.         }
  170.  
  171.         ENDCG  
  172.  
  173.         Pass
  174.         {
  175.             Name "Decal Mask"
  176.             Ztest Greater
  177.             Zwrite off
  178.             Cull Off
  179.             Colormask 0
  180.             Lighting Off
  181.  
  182.             Tags
  183.             {
  184.                 "RenderType" = "Transparent"            
  185.                 "RenderPipeline" = "UniversalPipeline"
  186.             }
  187.            
  188.             Stencil
  189.             {
  190.                 comp Always
  191.                 ref 1
  192.                 pass replace
  193.             }
  194.  
  195.             CGPROGRAM
  196.             #pragma vertex vert
  197.             #pragma fragment fragStencilMask
  198.            
  199.         ENDCG
  200.         }
  201.  
  202.        
  203.         Pass
  204.         {
  205.             Name "Decal Effect"
  206.             Zwrite off
  207.             Ztest Off
  208.             Cull Front
  209.             Lighting Off
  210.             Blend OneMinusDstColor One
  211.  
  212.             Tags
  213.             {
  214.                 "RenderType" = "Transparent"
  215.                 "Queue" = "Transparent"
  216.                 "RenderPipeline" = "UniversalPipeline"
  217.                 "LightMode" = "UniversalForward"
  218.             }
  219.            
  220.             CGPROGRAM
  221.             #pragma vertex vert
  222.             #pragma fragment frag  
  223.             ENDCG
  224.         }      
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement