Advertisement
noradninja

Godrays

May 5th, 2023
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.58 KB | None | 0 0
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2.  
  3.  
  4. Shader "Lighting/Crepuscular Rays" {
  5.    
  6.     Properties{
  7.         _MainTex("Base (RGB)", 2D) = "white" {}
  8.         _BlurTex("BlurTex (RGB)", 2D) = "white" {}
  9.         _NumSamples("Number of Samples", Range(0, 1024)) = 128
  10.         _Density("Density", Range(0, 1)) = 1.0
  11.         _Weight("Weight", Range(0, 2)) = 1.0
  12.         _Decay("Decay", Range(0, 1)) = 1.0
  13.         _Exposure("Exposure", Range(0, 1)) = 1.0
  14.         _Parameter("Kernel Offset", Range(0, 4)) = 1.0
  15.         _Contrast("Contrast", Range(1, 5)) = 1.0
  16.         _PerpendicularFalloff("Perpendicular Falloff Rate", Range(0.01, 1)) = 1.0
  17.         _CosAngle("Angle", Float) = 1
  18.         [IntRange] _StencilRef ("Stencil Ref", Range(0,255)) = 0
  19.         _TintColor ("Tint Color", Color) = (.5, .5, .5, .5)
  20.         _FrameValue("Frame remainder", Float) = 0
  21.         [IntRange] _Frequency ("Frequency", Range(1,15)) = 1
  22.         _fogInfluence("Fog Influence", Range(0,2)) = 0.5
  23.         _fogSpeed("Fog Speed", Float) = 10.0
  24.     }
  25.         CGINCLUDE
  26.         #include "UnityCG.cginc"
  27.         #pragma target 2.0
  28.  
  29.         uniform sampler2D_half _MainTex;
  30.         uniform sampler2D_half _BlurTex;
  31.         uniform sampler2D _CameraDepthTexture;
  32.         half3 _LightPos;
  33.         half _NumSamples;
  34.         half _Density;
  35.         half _Weight;
  36.         half _Decay;
  37.         half _Exposure;
  38.         half _Contrast;
  39.         uniform half4 _Parameter;
  40.         uniform half4 _MainTex_TexelSize;
  41.         half4 _MainTex_ST;
  42.         half _LightY;
  43.         half _CosAngle;
  44.         half4 _TintColor;
  45.         half _PerpendicularFalloff;
  46.         int _Frequency;
  47.         float _fogInfluence;
  48.         half _fogSpeed;
  49.        
  50.  
  51.        
  52.         struct appdata
  53.         {
  54.             float4 pos : POSITION;
  55.             float2 uv : TEXCOORD0;
  56.         };
  57.  
  58.         struct v2f
  59.         {
  60.             float4 pos : SV_POSITION;
  61.             float2 uv  : TEXCOORD0;
  62.         };
  63.  
  64.         struct v2f_withBlurCoordsSGX
  65.         {
  66.             float4 pos : SV_POSITION;
  67.             half2 uv : TEXCOORD0;
  68.             half4 offs[3] : TEXCOORD1;
  69.         };
  70.  
  71.         static const half curve[7] = { 0.0205, 0.0855, 0.232, 0.324, 0.232, 0.0855, 0.0205 };  // gauss'ish blur weights
  72.  
  73.         half rand(half2 co)
  74.                 {
  75.                     const half a = 2.9898f;
  76.                     const half b = 78.233f;
  77.                     const half c = 28.5453f;
  78.                     const half dt = dot(co.xy ,half2(a,b));
  79.                     const half sn = cos(abs(dt/3.14f));
  80.                     return cos(frac(sin(sn) * c)* ((_Time.w * _fogSpeed) * _fogInfluence));
  81.                 }  
  82. ////////////// accumulator for rays  /////////////////
  83.         v2f vert( appdata v )
  84.             {
  85.                 v2f o;
  86.                 o.pos = UnityObjectToClipPos (v.pos);
  87.                 o.uv = v.uv;
  88.                 return o;
  89.             }
  90.            
  91.            
  92.         half4 frag(v2f i) : COLOR
  93.             {
  94.                 // Calculate floattor from pixel to light source in screen space.
  95.                 half4 light = half4(_LightPos.xyz,1);
  96.                 half2 deltaTexCoord = half2 (0,0);
  97.                 // get our y direction, and swap the direction the coordinates are plotted based on that
  98.                 // so that it looks correct regardless of current camera rotation- we decompose this
  99.                 //because it will not look right if we just add or subtract light.xy to i.uv
  100.                 deltaTexCoord = half2(light.y < 0.0h ? half2(i.uv.x + light.x, i.uv.y + light.y) : half2(i.uv.x - light.x, i.uv.y - light.y));
  101.  
  102.                 // Divide by number of samples and scale by control factor.
  103.                 deltaTexCoord *= 1.0h / _NumSamples * _Density;
  104.                
  105.                 // Store initial sample.
  106.                 half2 uv = i.uv;
  107.                 half3 color = tex2D(_MainTex, uv);
  108.                
  109.                 // Set up illumination decay factor.
  110.                 half illuminationDecay = 1.0h;
  111.                
  112.                 // Evaluate summation from Equation 3 NUM_SAMPLES iterations.
  113.                 float depth;
  114.                 half rate = _Frequency;
  115.                
  116.                 for (int i = 1; i < _NumSamples + 1; i++)
  117.                 {
  118.                     // Step sample location along ray.
  119.                     uv -= deltaTexCoord;
  120.                     // Retrieve sample at new location.
  121.                     float sample = tex2D(_MainTex, uv);
  122.                     half randomFactor = rand(uv.yx)*_fogInfluence * _Contrast;
  123.                     float value = frac(i/rate);
  124.                     float cast = Linear01Depth(tex2D(_CameraDepthTexture, uv)).r;
  125.                     //calc depth value
  126.                     depth = float(value !=0 ? float(cast * 1.5f):float(cast * randomFactor));
  127.                     // Apply sample attenuation scale/decay factors.
  128.                     sample *= illuminationDecay * (_Weight/ _NumSamples*4) * depth;
  129.                     sample *= 2.5h;
  130.                    
  131.                     // Accumulate combined color.
  132.                     color += sample;
  133.                     // Update exponential decay factor.
  134.                     illuminationDecay *= _Decay;
  135.                 }
  136.                 // Output final color with a further scale control factor.
  137.                 return (max(half4(color * _Exposure, 1), 0.15h));
  138.             }
  139. /////////////// SGX Horizontal Blur /////////////////////////////      
  140.         v2f_withBlurCoordsSGX vertBlurHorizontalSGX (appdata_img v)
  141.             {
  142.                 v2f_withBlurCoordsSGX o;
  143.                 o.pos = UnityObjectToClipPos(v.vertex);
  144.                
  145.                 o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST);
  146.  
  147.                 const half offsetMagnitude = _MainTex_TexelSize.x * _Parameter.x;
  148.                 o.offs[0] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(-3.0h, 0.0h, 3.0h, 0.0h), _MainTex_ST);
  149.                 o.offs[1] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(-2.0h, 0.0h, 2.0h, 0.0h), _MainTex_ST);
  150.                 o.offs[2] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(-1.0h, 0.0h, 1.0h, 0.0h), _MainTex_ST);
  151.  
  152.                 return o;
  153.             }
  154. /////////////// SGX Vertical Blur /////////////////////////////    
  155.         v2f_withBlurCoordsSGX vertBlurVerticalSGX (appdata_img v)
  156.             {
  157.                 v2f_withBlurCoordsSGX o;
  158.                 o.pos = UnityObjectToClipPos(v.vertex);
  159.                
  160.                 o.uv = half4(UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST),1,1);
  161.  
  162.                 const half offsetMagnitude = _MainTex_TexelSize.y * _Parameter.x;
  163.                 o.offs[0] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -3.0h, 0.0h, 3.0h), _MainTex_ST);
  164.                 o.offs[1] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -2.0h, 0.0h, 2.0h), _MainTex_ST);
  165.                 o.offs[2] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -1.0h, 0.0h, 1.0h), _MainTex_ST);
  166.  
  167.                 return o;
  168.             }
  169. ///////////// SGX Frag  //////////////////////////////////////
  170.         half4 fragBlurSGX ( v2f_withBlurCoordsSGX i ) : SV_Target
  171.             {
  172.                 half2 uv = i.uv.xy;
  173.                
  174.                 half4 color = tex2D(_MainTex, i.uv) * curve[3];
  175.                
  176.                 for( int l = 0; l < 3; l++ )  
  177.                 {  
  178.                     const half4 tapA = tex2D(_MainTex, i.offs[l].xy);
  179.                     const half4 tapB = tex2D(_MainTex, i.offs[l].zw);
  180.                     color += (tapA + tapB) * curve[l];
  181.                 }
  182.                 return color;
  183.             }
  184. /////////////// Composition /////////////////////////////
  185.         v2f vertFinal(appdata i)
  186.             {
  187.                 v2f o = (v2f)0;
  188.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
  189.                 o.pos = UnityObjectToClipPos(i.pos);
  190.                 o.uv = (i.uv);
  191.                 return o;
  192.             }
  193.            
  194.         half4 fragFinal(v2f i) : SV_Target
  195.             {
  196.                 half4 light = half4(_LightPos.xyz,1);
  197.                 _CosAngle = 1- abs(cos(light.z));
  198.                 const fixed4 col = tex2D(_MainTex, i.uv);
  199.                 fixed4 sample = tex2D(_BlurTex, i.uv);
  200.                 const fixed contrast = _Contrast;
  201.                 //sample = sample.r + sample.g + sample.b;
  202.                 //sample *= 2.0h;
  203.                 const fixed4 finalSample = (((col) + (sample * 0.4h)) - 0.5h) * contrast + 0.445h; //final sampled color
  204.                 const fixed4 finalColor = (col + (col * 0.04h) - 0.01h); //final modulated base color
  205.                 //add our ray greyscale samples at - 25% brightness to the main image
  206.                 fixed4 blitColor = lerp(finalSample, finalColor, (1 - _CosAngle - _PerpendicularFalloff));//lerp (finalColor + 0.25, finalColor + 0.035h, normalizedLightY);
  207.                 return blitColor;
  208.             }
  209.  
  210.         //
  211.         v2f vertStencil(appdata v)
  212.         {
  213.                 v2f o;
  214.                 o.pos = UnityObjectToClipPos (v.pos);
  215.                 o.uv = v.uv;
  216.                 return o;
  217.         }
  218.         half4 fragStencil (v2f i) : SV_Target
  219.         {
  220.             fixed4 col = _TintColor;
  221.             return col;
  222.         }
  223.        
  224.         ENDCG
  225.     ////// Passes /////////////////////////////////////////////////////
  226.     SubShader {
  227.         ZTest Always
  228.         Cull Off
  229.         //0- calculate low resolution rays
  230.        
  231.         Pass {
  232.                 CGPROGRAM
  233.                 #pragma vertex vert
  234.                 #pragma fragment frag
  235.                 #pragma fragmentoption ARB_precision_hint_fastest
  236.                 ENDCG
  237.             }
  238.             //2- vertical blur
  239.         Pass {
  240.                 CGPROGRAM
  241.                 #pragma vertex vertBlurVerticalSGX
  242.                 #pragma fragment fragBlurSGX
  243.                 #pragma fragmentoption ARB_precision_hint_fastest
  244.                 ENDCG
  245.             }  
  246.            
  247.         //3- horizontal Blur
  248.         Pass {     
  249.                 CGPROGRAM  
  250.                 #pragma vertex vertBlurHorizontalSGX
  251.                 #pragma fragment fragBlurSGX
  252.                 #pragma fragmentoption ARB_precision_hint_fastest
  253.                 ENDCG
  254.             }
  255.         Pass //4- composition
  256.         {
  257.             CGPROGRAM
  258.             #pragma vertex vertFinal
  259.             #pragma fragment fragFinal
  260.             #pragma fragmentoption ARB_precision_hint_fastest
  261.             ENDCG
  262.         }
  263.         Pass {
  264.         Stencil{
  265.             Ref [_StencilRef]
  266.             Comp Equal
  267.             Pass Keep
  268.         }
  269.                 CGPROGRAM
  270.                 #pragma vertex vertStencil
  271.                 #pragma fragment fragStencil
  272.                 #pragma fragmentoption ARB_precision_hint_fastest
  273.                 ENDCG
  274.             }
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement