Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Shader overlights the scene

Discussion in 'Shaders' started by Sonatai, May 18, 2020.

  1. Sonatai

    Sonatai

    Joined:
    Jan 14, 2018
    Posts:
    2
    Hello,

    I wrote a bloom shader with the help of a tutorial. And the bloom is okay, but if I drag the scene than it lights up till the scene is white. If I run the application, than the scene also lights up till it is white. I try to figure out, why it is lighting up, but I can not find the error. I would be glad, if someone can help me.

    I added the scene as a .unitypackage and also insert the two scripts:

    C# Script
    Code (CSharp):
    1. private void OnRenderImage(RenderTexture src, RenderTexture dest)
    2.    {
    3.       if (bloom == null)
    4.       {
    5.          bloom = new Material(bloomShader);
    6.          bloom.hideFlags = HideFlags.HideAndDontSave;
    7.       }
    8.  
    9.       float knee = treshold * softTreshold;
    10.       Vector4 filter;
    11.       filter.x = treshold;
    12.       filter.y = filter.x - knee;
    13.       filter.z = 2f * knee;
    14.       filter.w = 0.25f / (knee + 0.00001f);
    15.       bloom.SetVector("_Filter",filter);
    16.       bloom.SetFloat("_Intensity", Mathf.GammaToLinearSpace(intensity));
    17.      
    18.       int width = src.width;
    19.       int height = src.height;
    20.       RenderTextureFormat format = src.format;
    21.       RenderTexture[] textures = new RenderTexture[16];
    22.      
    23.       //Temp Texture without depth map
    24.       RenderTexture currentDest = textures[0] = RenderTexture.GetTemporary(width,height,0, format);
    25.      
    26.       Graphics.Blit(src, currentDest, bloom, BoxDownPrefilterPass);
    27.      
    28.       //multiple sampling
    29.       RenderTexture currentSrc = currentDest;
    30.       int i = 1;
    31.       for (; i < iterations; i++)
    32.       {
    33.          width /= 2;
    34.          height /= 2;
    35.          if(height < 2)
    36.             break;
    37.          currentDest = textures[i] = RenderTexture.GetTemporary(width, height, 0, format);
    38.          Graphics.Blit(currentSrc,currentDest, bloom, BoxDownPass);
    39.          currentSrc = currentDest;
    40.       }
    41.  
    42.       for (i -= 2; i >= 0; i--)
    43.       {
    44.          currentDest = textures[i];
    45.          textures[i] = null;
    46.          Graphics.Blit(currentSrc,currentDest,bloom, BoxUpPass);
    47.          RenderTexture.ReleaseTemporary(currentSrc);
    48.          currentSrc = currentDest;
    49.       }
    50.  
    51.       if (debug)
    52.       {
    53.          Graphics.Blit(currentSrc,dest,bloom,DebugBloomPass);
    54.       }
    55.       else
    56.       {
    57.          bloom.SetTexture("_SourceTex", src);
    58.          Graphics.Blit(currentSrc, dest, bloom, ApplyBloomPass);
    59.       }
    60.       RenderTexture.ReleaseTemporary(currentSrc);
    61.    }
    Shader
    Code (CSharp):
    1. Shader "Custom/Bloom"
    2. {
    3.     Properties{
    4.            _MainTex ("Texture", 2D) = "white" {}
    5.     }
    6.    
    7.     CGINCLUDE
    8.         #include "UnityCG.cginc"
    9.        
    10.         sampler2D _MainTex, _SourceTex;
    11.         float4 _MainTex_TexelSize;
    12.         half4 _Filter;
    13.         half _Intensity;
    14.        
    15.         //* calc the new color for the pixel
    16.         half3 Sample (float2 uv){
    17.             return tex2D(_MainTex,uv).rgb;
    18.         }
    19.        
    20.         half3 SampleBox(float2 uv, float delta){
    21.             float4 o = _MainTex_TexelSize.xyxy * float2(-delta,delta).xxyy;
    22.             half3 s =
    23.                 Sample(uv + o.xy) + Sample(uv + o.zy) +
    24.                 Sample(uv + o.xw) + Sample(uv + o.zw);
    25.             return s * 0.25f;
    26.         }
    27.         //*
    28.        
    29.         //Calc brigthness
    30.         half3 Prefilter(half3 c){
    31.             half brightness = max(c.r,max(c.g,c.b));
    32.             half soft = brightness - _Filter.y;
    33.             soft = clamp(soft, 0, _Filter.z);
    34.             soft = soft * soft * _Filter.w;
    35.             half contribution = max(soft, brightness- _Filter.x);
    36.             contribution /= max(brightness, 0.00001);
    37.             return c * contribution;
    38.         }
    39.        
    40.         struct VertexData{
    41.             float4 vertex : POSITION;
    42.             float2 uv : TEXCOORD0;
    43.         };
    44.        
    45.         struct Interpolators{
    46.             float4 pos : SV_POSITION;
    47.             float2 uv : TEXCOORD0;
    48.         };
    49.        
    50.         Interpolators VertexProgram(VertexData v) {
    51.             Interpolators i;
    52.             i.pos = UnityObjectToClipPos(v.vertex);
    53.             i.uv = v.uv;
    54.             return i;
    55.         }
    56.     ENDCG
    57.    
    58.     SubShader{
    59.         Cull Off
    60.         ZTest Always
    61.         ZWrite Off
    62.        
    63.         Pass{ //0
    64.             CGPROGRAM
    65.                 #pragma vertex VertexProgram
    66.                 #pragma fragment FragmentProgram
    67.                
    68.                 half4 FragmentProgram (Interpolators i) : SV_Target {
    69.                     return half4(Prefilter(SampleBox(i.uv,1)),1);
    70.                 }
    71.             ENDCG
    72.         }
    73.        
    74.         Pass{ //1
    75.             CGPROGRAM
    76.                 #pragma vertex VertexProgram
    77.                 #pragma fragment FragmentProgram
    78.                
    79.                 half4 FragmentProgram (Interpolators i) : SV_Target {
    80.                     return half4(SampleBox(i.uv,1),1);
    81.                 }
    82.             ENDCG
    83.         }
    84.        
    85.         Pass{ //2
    86.             Blend One One
    87.            
    88.             CGPROGRAM
    89.                 #pragma vertex VertexProgram
    90.                 #pragma fragment FragmentProgram
    91.                
    92.                 half4 FragmentProgram (Interpolators i) : SV_Target {
    93.                     return half4(SampleBox(i.uv,0.5),1);
    94.                 }
    95.             ENDCG
    96.         }
    97.        
    98.         Pass{ //3
    99.             Blend One One
    100.            
    101.             CGPROGRAM
    102.                 #pragma vertex VertexProgram
    103.                 #pragma fragment FragmentProgram
    104.                
    105.                 half4 FragmentProgram (Interpolators i) : SV_Target {
    106.                     half4 c = tex2D(_SourceTex,i.uv);
    107.                     c.rgb += _Intensity * SampleBox(i.uv,0.5);
    108.                     return c;
    109.                 }
    110.             ENDCG
    111.         }
    112.        
    113.         Pass{ //4
    114.             CGPROGRAM
    115.                 #pragma vertex VertexProgram
    116.                 #pragma fragment FragmentProgram
    117.                
    118.                 half4 FragmentProgram (Interpolators i) : SV_Target {
    119.                     return half4(_Intensity * SampleBox(i.uv,0.5),1);
    120.                 }
    121.             ENDCG
    122.         }
    123.     }
    124. }
    125.  
     

    Attached Files: