Search Unity

Question Shader Pass not work ?

Discussion in 'Shaders' started by MatheusMarkies, Mar 9, 2021.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    I made a post processing shader that uses more than one pass. First time I work with this. What is happening and that the last pass of the shader overlaps all the others, was this supposed to happen?



    Code (CSharp):
    1. Shader "Hidden/ScreenSpaceLensFlare"
    2. {
    3.     Properties{}
    4.     SubShader
    5.     {
    6.         Tags { "RenderType" = "Opaque" }
    7.  
    8.         ZTest Always Cull Off ZWrite Off
    9.  
    10.         HLSLINCLUDE
    11.         #include "../../ShaderLibrary/Common.hlsl"
    12.         ENDHLSL
    13.  
    14.         Pass{
    15.             HLSLPROGRAM
    16.             #pragma target 3.5
    17.                 #pragma vertex DefaultPassVertex
    18.                 #pragma fragment FragBox
    19.             #include "ScreamSpaceLensFlare.hlsl"
    20.             ENDHLSL
    21.         }
    22.         Pass{
    23.             HLSLPROGRAM
    24.             #pragma target 3.5
    25.                 #pragma vertex DefaultPassVertex
    26.                 #pragma fragment FragHBlur
    27.             #include "ScreamSpaceLensFlare.hlsl"
    28.             ENDHLSL
    29.         }
    30.         Pass{
    31.             HLSLPROGRAM
    32.             #pragma target 3.5
    33.                 #pragma vertex DefaultPassVertex
    34.                 #pragma fragment FragVBlur
    35.             #include "ScreamSpaceLensFlare.hlsl"
    36.             ENDHLSL
    37.         }
    38.         Pass{
    39.             HLSLPROGRAM
    40.             #pragma target 3.5
    41.             #pragma vertex DefaultPassVertex
    42.             #pragma fragment threshold
    43.             #include "ScreamSpaceLensFlare.hlsl"
    44.             ENDHLSL
    45.         }
    46.         Pass{
    47.             HLSLPROGRAM
    48.             #pragma target 3.5
    49.             #pragma vertex DefaultPassVertex
    50.             #pragma fragment streakPass
    51.             #include "ScreamSpaceLensFlare.hlsl"
    52.             ENDHLSL
    53.         }
    54.         Pass{
    55.             HLSLPROGRAM
    56.             #pragma target 3.5
    57.             #pragma vertex DefaultPassVertex
    58.             #pragma fragment ghostPass
    59.             #include "ScreamSpaceLensFlare.hlsl"
    60.             ENDHLSL
    61.         }
    62.     }
    63. }
    Code (CSharp):
    1. #include "../../ShaderLibrary/UnityInput.hlsl"
    2.  
    3. TEXTURE2D(_PostFXSource);
    4. SAMPLER(sampler_PostFXSource);
    5.  
    6. struct appdata
    7. {
    8.     float4 vertex : POSITION;
    9.     float2 uv : TEXCOORD0;
    10. };
    11.  
    12. struct Varyings {
    13.     float4 positionCS : SV_POSITION;
    14.     float2 fxUV : VAR_FX_UV;
    15. };
    16.  
    17. Varyings DefaultPassVertex(uint vertexID: SV_VertexID) {
    18.     Varyings output;
    19.     output.positionCS = float4(
    20.         vertexID <= 1 ? -1.0 : 3.0,
    21.         vertexID == 1 ? 3.0 : -1.0,
    22.         0.0, 1.0
    23.         );
    24.     output.fxUV = float2(
    25.         vertexID <= 1 ? 0.0 : 2.0,
    26.         vertexID == 1 ? 2.0 : 0.0
    27.         );
    28.     if (_ProjectionParams.x < 0.0) {
    29.         output.fxUV.y = 1.0 - output.fxUV.y;
    30.     }
    31.     return output;
    32. }
    33.  
    34. float4 _ScreenParams;
    35. float4 _PostFXSource_TexelSize;
    36. float _Size;
    37. float _Threshold;
    38. float _Gain;
    39. int _Boundary;
    40.  
    41. float _Delta;
    42.  
    43. float4 GetSource(float2 fxUV) {
    44.     return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, fxUV);
    45. }
    46.  
    47. float4 SampleBox(float2 uv, float delta)
    48. {
    49.     float4 o = _PostFXSource_TexelSize.xyxy * float4(-delta, -delta, delta, delta);
    50.     return (GetSource(uv + o.xy)
    51.         + GetSource(uv + o.zy)
    52.         + GetSource(uv + o.xw)
    53.         + GetSource(uv + o.zw)) * 0.25;
    54. }
    55.  
    56. float4 FragBox(Varyings i) : SV_Target
    57. {
    58.     return SampleBox(i.fxUV, _Delta);
    59. }
    60.  
    61. static const float gaussian[7] = {
    62.     0.00598,    0.060626,    0.241843,    0.383103,    0.241843,    0.060626,    0.00598
    63. };
    64.  
    65. float4 FragHBlur(Varyings i) : SV_Target
    66. {
    67.     float4 color;
    68.     float2 o = float2(_PostFXSource_TexelSize.x, 0);
    69.     for (int idx = -3; idx <= 3; idx++) {
    70.         float4 tColor = GetSource(i.fxUV + idx * o);
    71.         color += tColor * gaussian[idx + 3];
    72.     }
    73.     color.rgb = float3(color.r * 4,color.g,color.b);
    74.     return color;
    75. }
    76.  
    77. float4 FragVBlur(Varyings i) : SV_Target
    78. {
    79.     float4 color;
    80.     float2 o = float2(0, _PostFXSource_TexelSize.y);
    81.     for (int idx = -3; idx <= 3; idx++) {
    82.         float4 tColor = GetSource(i.fxUV + idx * o);
    83.         color += tColor * gaussian[idx + 3];
    84.     }
    85.     return color;
    86. }
    87.  
    88. inline float2 ThresholdBounds(float2 uv, int boundary) {
    89.     float2 bounds;
    90.     bounds.x = smoothstep(float2(0, 0), boundary, uv * _ScreenParams.xy);
    91.     bounds.y = smoothstep(float2(0, 0), boundary, _ScreenParams.xy - (uv * _ScreenParams.xy));
    92.     return bounds.x * bounds.y;
    93. }
    94.  
    95. float Luma(float3 color) {
    96.     return dot(color.rgb, 0.333);
    97. }
    98.  
    99. float4 threshold(Varyings i) : SV_Target{
    100.     float4 color = GetSource(i.fxUV);
    101.     float luma = Luma(color.rgb);
    102.  
    103.     if (luma < _Threshold) return 0;
    104.  
    105.     float2 bounds = ThresholdBounds(i.fxUV, _Boundary);
    106.     return float4(color.rgb * color.a * _Gain * bounds.x * bounds.y, 0);
    107. }
    108.  
    109. static const float ghosts[9] = {
    110.     0.05870,
    111.     0.176548,
    112.     -0.0954567,
    113.     0.208574,
    114.     0.303365,
    115.     -0.32452345,
    116.     -0.2345414,
    117.     -0.1677848,
    118.     0.1156776,
    119. };
    120.  
    121. float4 Ghost(float2 fxUV,int ammount) : SV_Target{
    122.     float4 result = float4(0, 0, 0, 0);
    123.     for (int i = 0; i < ammount; i++) {
    124.         float t_p = ghosts[i];
    125.         result += (fxUV * 2 * t_p + 0.5, 1) * (t_p * t_p);
    126.     }
    127.     return result;
    128. }
    129.  
    130. float4 GetSourceThreshold(float2 fxUV, float thr) {
    131.     return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, (fxUV - 0.5) / thr + 0.5);
    132. }
    133.  
    134. float lightIntensity(float2 fxUV, float thr) {
    135.     float4 colorChannel = GetSourceThreshold(fxUV, thr);
    136.     colorChannel = ((+pow(colorChannel, 2)) * 4) / 4;
    137.     float greyscale = (colorChannel.r + colorChannel.g + colorChannel.b) / 3.0;
    138.     if (greyscale > 0.8)
    139.         return greyscale;
    140.     else
    141.         return 0;
    142. }
    143.  
    144. float4 ChromaticAberration(float2 chromaticAberration, float2 uv) {
    145.     float colR = GetSource(float2(uv.x - chromaticAberration.x, uv.y - chromaticAberration.x)).r;
    146.     float colG = GetSource(float2(uv)).g;
    147.     float colB = GetSource(float2(uv.x + chromaticAberration.x, uv.y + chromaticAberration.x)).b;
    148.  
    149.     return float4(lerp(float3(lerp(colR, colG, uv.x * 0.3), lerp(colG, colB, uv.x * 0.3), lerp(colR, colB, uv.x*0.3)), float3(lightIntensity(uv, 1), lightIntensity(uv, 1), lightIntensity(uv, 1)), 0.25), 1);
    150. }
    151.  
    152. #define PI 3.14159265359
    153. float4 ghostPass(Varyings i) : SV_Target{
    154.  
    155.     float4 color = GetSource(i.fxUV);
    156.     float4 colorChannel = GetSource(i.fxUV);
    157.  
    158.     float greyscale = lightIntensity(i.fxUV, 1);
    159.     float2 ghost = (float2(0.5, 0.5) - i.fxUV) * 0.4;
    160.  
    161.     float4 result = float4(0, 0, 0, 0);
    162.     float2 fxUV = i.fxUV;
    163.     float2 uv = i.fxUV - 0.5;
    164.     for (int i = 0; i < 6; i++) {
    165.         float t_p = ghosts[i];
    166.         result.rgb += float3(ChromaticAberration(float2(0.0009, 0.005), uv * _Size * t_p + 0.5).r, ChromaticAberration(float2(0.0009, 0.005), uv * _Size * t_p + 0.5).g, ChromaticAberration(float2(0.0009, 0.005), uv * _Size * t_p + 0.5).b) * (t_p * t_p);
    167.     }
    168.  
    169.     float2 haloUv = normalize(uv) * -0.5;
    170.     result.rgb += (color * 0.005) + max(float3(ChromaticAberration(float2(0.0009, 0.005), fxUV + haloUv).r, ChromaticAberration(float2(0.0009, 0.005), fxUV + haloUv).g, ChromaticAberration(float2(0.0009, 0.005), fxUV + haloUv).b) - 0.0, 0) * length(uv) * 0.0025;
    171.  
    172.     return float4(colorChannel.rgb + result, color.a);
    173.  
    174. }
    175.  
    176. float _Attenuation;
    177. float4 _Direction;
    178. float _Offset;
    179.  
    180. float4 streakPass(Varyings i) : SV_Target
    181. {
    182.         float2 dx = _PostFXSource_TexelSize.xy;
    183.         float4 color = GetSource(i.fxUV);
    184.  
    185.         float attenuationSquared = _Attenuation * _Attenuation;
    186.         float attenuationCubed = _Attenuation * _Attenuation * _Attenuation;
    187.  
    188.         return color + _Attenuation * GetSource(i.fxUV + _Offset * _Direction.xy * dx) + attenuationSquared * GetSource(i.fxUV + 2 * _Offset * _Direction.xy * dx) + attenuationCubed * GetSource(i.fxUV + 3 * _Offset * _Direction.xy * dx);
    189. }
    https://developer.amd.com/wordpress/media/2012/10/Oat-ScenePostprocessing.pdf
    I am using an SRP, is that an error in it?