Advertisement
Guest User

StencilDecalPortalOrtho.shader

a guest
Mar 24th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Custom/URPDecalOrtho"
  3. {
  4.     Properties
  5.     {
  6.     _Intensity("Intensity", Range(0,10)) = 5.0
  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.     // The SubShader block containing the Shader code.
  29.     SubShader
  30.     {
  31.         // SubShader Tags define when and under which conditions a SubShader block or
  32.         // a pass is executed.
  33.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry +1"}
  34.  
  35.        
  36.  
  37.         HLSLINCLUDE
  38.         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  39.  
  40.         // The DeclareDepthTexture.hlsl file contains utilities for sampling the
  41.         // Camera depth texture.
  42.         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"  
  43.      
  44.         float _Cutoff, _Smoothness;
  45.         sampler2D _MovingTex;
  46.         float _SpeedX, _SpeedY;
  47.         sampler2D _ShapeTex;
  48.         float _ShapeTexIntensity;
  49.         sampler2D _Gradient;
  50.         float _Stretch, _Multiply;
  51.         float _Intensity, _Offset;
  52.         float _RadialScale, _LengthScale;
  53.         float4 _Tint;
  54.        
  55.  
  56.        
  57.        
  58.      
  59.  
  60.          // helper functions
  61.          float2 Unity_PolarCoordinates(float2 UV, float2 Center, float RadialScale, float LengthScale)
  62.          {
  63.              float2 delta = UV - Center;
  64.              float radius = length(delta) * 2.0 * RadialScale;
  65.              float angle = atan2(delta.y, delta.x) * 1.0 / 6.28318 * LengthScale;
  66.              return float2(radius, angle);
  67.          }
  68.  
  69.          
  70.         float GetFinalDistortion(float2 uvProj, float shapeTex)
  71.         {
  72.             float2 polarUV = Unity_PolarCoordinates(uvProj, float2(0.5, 0.5), _RadialScale, _LengthScale);
  73.  
  74.             // Move UV
  75.             float2 movingUV = float2(polarUV.x + (_Time.x * _SpeedX), polarUV.y + (_Time.x * _SpeedY));
  76.  
  77.             // Final moving texture with the distortion
  78.             float final = tex2D(_MovingTex, movingUV).r;
  79.  
  80.             shapeTex *= _ShapeTexIntensity;
  81.             final *= shapeTex;
  82.             return final;
  83.         }
  84.  
  85.             // This example uses the Attributes structure as an input structure in
  86.             // the vertex shader.
  87.             struct Attributes
  88.             {
  89.                 // The positionOS variable contains the vertex positions in object
  90.                 // space.
  91.                 float4 positionOS   : POSITION;
  92.             };
  93.  
  94.             struct Varyings
  95.             {
  96.                 // The positions in this struct must have the SV_POSITION semantic.
  97.                 float4 positionHCS  : SV_POSITION;
  98.             };
  99.  
  100.             // The vertex shader definition with properties defined in the Varyings
  101.             // structure. The type of the vert function must match the type (struct)
  102.             // that it returns.
  103.             Varyings vert(Attributes IN)
  104.             {
  105.                 // Declaring the output object (OUT) with the Varyings struct.
  106.                 Varyings OUT;
  107.                 // The TransformObjectToHClip function transforms vertex positions
  108.                 // from object space to homogenous clip space.
  109.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
  110.                 // Returning the output.
  111.                 return OUT;
  112.             }
  113.  
  114.             half4 fragStencilMask(Varyings IN) : SV_Target{
  115. // To calculate the UV coordinates for sampling the depth buffer,
  116.                 // divide the pixel location by the render target resolution
  117.                 // _ScaledScreenParams.
  118.                 float2 UV = IN.positionHCS.xy / _ScaledScreenParams.xy;
  119.  
  120.                 // Sample the depth from the Camera depth texture.
  121.                 #if UNITY_REVERSED_Z
  122.                     real depth = SampleSceneDepth(UV);
  123.                 #else
  124.                     // Adjust Z to match NDC for OpenGL ([-1, 1])
  125.                     real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(UV));
  126.                 #endif
  127.  
  128.                 // Reconstruct the world space positions.
  129.                 float3 worldPos = ComputeWorldSpacePosition(UV, depth, UNITY_MATRIX_I_VP);
  130.  
  131.                 float3 opos = mul(unity_WorldToObject, float4(worldPos,1)).xyz;
  132.  
  133.                 clip(float3(0.5,0.5,0.5) - abs(opos.xyz));
  134.  
  135.                 float2 uvProj = opos.xz + 0.5; 
  136.             // get the main shape texture for the alpha
  137.             float shapeTex = tex2D(_ShapeTex, uvProj).r;   
  138.  
  139.             float vortexEffect = GetFinalDistortion(uvProj, shapeTex);             
  140.                    
  141.             // discard outside of texture alpha
  142.             clip(vortexEffect- 0.1);
  143.             return float4(1,1,1,1);
  144.  
  145.             }
  146.  
  147.             // The fragment shader definition.
  148.             // The Varyings input structure contains interpolated values from the
  149.             // vertex shader. The fragment shader uses the `positionHCS` property
  150.             // from the `Varyings` struct to get locations of pixels.
  151.             half4 frag(Varyings IN) : SV_Target
  152.             {
  153.                 // To calculate the UV coordinates for sampling the depth buffer,
  154.                 // divide the pixel location by the render target resolution
  155.                 // _ScaledScreenParams.
  156.                 float2 UV = IN.positionHCS.xy / _ScaledScreenParams.xy;
  157.  
  158.                 // Sample the depth from the Camera depth texture.
  159.                 #if UNITY_REVERSED_Z
  160.                     real depth = SampleSceneDepth(UV);
  161.                 #else
  162.                     // Adjust Z to match NDC for OpenGL ([-1, 1])
  163.                     real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(UV));
  164.                 #endif
  165.  
  166.                 // Reconstruct the world space positions.
  167.                 float3 worldPos = ComputeWorldSpacePosition(UV, depth, UNITY_MATRIX_I_VP);
  168.  
  169.                 float3 opos = mul(unity_WorldToObject, float4(worldPos,1)).xyz;
  170.  
  171.                 clip(float3(0.5,0.5,0.5) - abs(opos.xyz));
  172.  
  173.                 float2 uvProj = opos.xz + 0.5;
  174.                 // Get the main shape texture for the alpha
  175.                 float shapeTex = tex2D(_ShapeTex, uvProj).r;
  176.  
  177.                 float vortexEffect = GetFinalDistortion(uvProj, shapeTex);
  178.  
  179.                 // Add the coloring from the gradient map
  180.                 float4 gradientmap = tex2D(_Gradient, (vortexEffect * _Stretch) + _Offset) * _Intensity;
  181.                 gradientmap *= vortexEffect;
  182.                gradientmap *= _Tint;
  183.    
  184.                 // Add tinting and transparency
  185.                gradientmap.rgb *= _Tint.rgb;
  186.                gradientmap *= _Tint.a;
  187.                 gradientmap *= shapeTex;
  188.    
  189.                 // Create a cutoff point for the outside of the portal effect
  190.                 gradientmap *= smoothstep(_Cutoff - _Smoothness, _Cutoff, vortexEffect * _Multiply);
  191.                 // Increase intensity
  192.                 gradientmap = saturate(gradientmap * 10) * _Intensity;
  193.                 return gradientmap;
  194.             }
  195.         ENDHLSL
  196.         Pass
  197.         {
  198.             Name "Decal Mask"
  199.             Ztest Greater
  200.             Zwrite off
  201.             Cull Off
  202.             Colormask 0
  203.             Lighting Off
  204.  
  205.             Tags
  206.             {
  207.                 "RenderType" = "Transparent"            
  208.                 "RenderPipeline" = "UniversalPipeline"
  209.             }
  210.            
  211.             Stencil
  212.             {
  213.                 comp Always
  214.                 ref 1
  215.                 pass replace
  216.             }
  217.  
  218.             HLSLPROGRAM
  219.             #pragma vertex vert
  220.             #pragma fragment fragStencilMask
  221.  
  222.             ENDHLSL
  223.         }
  224.  
  225.         Pass {
  226.             Name "Decal Outside"
  227.             Zwrite off
  228.             ZTest off
  229.             Cull Back
  230.             Lighting Off
  231.             Blend OneMinusDstColor One
  232.            
  233.             Tags
  234.             {
  235.                 "RenderType" = "Transparent"              
  236.                 "RenderPipeline" = "UniversalPipeline"          
  237.             }
  238.             HLSLPROGRAM
  239.             #pragma vertex vert
  240.             #pragma fragment frag        
  241.             ENDHLSL
  242.         }
  243.        
  244.         Pass
  245.         {
  246.             Name "Decal Inside"
  247.             Zwrite off
  248.             Ztest Off
  249.             Cull Front
  250.             Lighting Off
  251.             Blend OneMinusDstColor One
  252.  
  253.             Tags
  254.             {
  255.                 "RenderType" = "Transparent"
  256.                 "Queue" = "Transparent"
  257.                 "RenderPipeline" = "UniversalPipeline"
  258.                 "LightMode" = "UniversalForward"
  259.             }
  260.            
  261.             HLSLPROGRAM
  262.             #pragma vertex vert
  263.             #pragma fragment frag  
  264.             ENDHLSL
  265.         }      
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement