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

Depth of Field with render textures

Discussion in 'Image Effects' started by jsjdev91, Dec 17, 2019.

  1. jsjdev91

    jsjdev91

    Joined:
    Dec 17, 2019
    Posts:
    4
    Applying effects (I specifically looked at DoF) doesn't work as I thought it would with render textures. When I apply DoF to a camera rendering to a render texture placed on canvas, it causes strange behavior and makes the RT transparent rather than making the background blurred. Any ideas what might be causing this? I couldn't find any solutions online. Perhaps I'm just not searching the right things. Below is perhaps a simplified version of what I see in my project.

     
  2. jsjdev91

    jsjdev91

    Joined:
    Dec 17, 2019
    Posts:
    4
    For a quick-ish workaround, I just make a shader which forced alpha to be one and slapped the material onto the RawImage component with the RT. Since I've seen at least of couple of other individuals with this issue, I wanted to just provide some sort of solution to this.
     
  3. SceneForgeStudio

    SceneForgeStudio

    Joined:
    Feb 28, 2017
    Posts:
    471
    I'm having this issue too! Do you mind sharing your code?
    Thanks!
     
  4. SomeHumbleOnion

    SomeHumbleOnion

    Joined:
    Jan 18, 2021
    Posts:
    28
    I had this same issue but I was able to fix it with jsdev91's solution, thank you jsdev! I've pasted a modified version of Unity's default UI Shader that forces the alpha to be 1. Hope this helps someone!


    Shader "Custom/AlphaForce1"
    {
    Properties
    {
    [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    _Color("Tint", Color) = (1,1,1,1)

    _StencilComp("Stencil Comparison", Float) = 8
    _Stencil("Stencil ID", Float) = 0
    _StencilOp("Stencil Operation", Float) = 0
    _StencilWriteMask("Stencil Write Mask", Float) = 255
    _StencilReadMask("Stencil Read Mask", Float) = 255

    _ColorMask("Color Mask", Float) = 15

    [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
    }

    SubShader
    {
    Tags
    {
    "Queue" = "Transparent"
    "IgnoreProjector" = "True"
    "RenderType" = "Transparent"
    "PreviewType" = "Plane"
    "CanUseSpriteAtlas" = "True"
    }

    Stencil
    {
    Ref[_Stencil]
    Comp[_StencilComp]
    Pass[_StencilOp]
    ReadMask[_StencilReadMask]
    WriteMask[_StencilWriteMask]
    }

    Cull Off
    Lighting Off
    ZWrite Off
    ZTest[unity_GUIZTestMode]
    Blend SrcAlpha OneMinusSrcAlpha
    ColorMask[_ColorMask]

    Pass
    {
    Name "Default"
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 2.0

    #include "UnityCG.cginc"
    #include "UnityUI.cginc"

    #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
    #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

    struct appdata_t
    {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
    float4 vertex : SV_POSITION;
    fixed4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    float4 worldPosition : TEXCOORD1;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    sampler2D _MainTex;
    fixed4 _Color;
    fixed4 _TextureSampleAdd;
    float4 _ClipRect;
    float4 _MainTex_ST;

    v2f vert(appdata_t v)
    {
    v2f OUT;
    UNITY_SETUP_INSTANCE_ID(v);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    OUT.worldPosition = v.vertex;
    OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

    OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);

    OUT.color = v.color * _Color;
    return OUT;
    }

    fixed4 frag(v2f IN) : SV_Target
    {
    half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;

    color.a = 1;

    //#ifdef UNITY_UI_CLIP_RECT
    //color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    //#endif

    //#ifdef UNITY_UI_ALPHACLIP
    //clip(color.a - 0.001);
    //#endif

    return color;
    }
    ENDCG
    }
    }
    }
     
    chriscode likes this.
  5. chriscode

    chriscode

    Joined:
    Mar 2, 2015
    Posts:
    48
    Ah I had this problem - thanks for writing about it: DOF effect on camera rendered to texture, big blurry mess on anything not in focus. Why DOF messes with alpha of render texture? I found right-clicking and creating an unlit shader was okay too. ( commented out fog )

    Code (CSharp):
    1. Shader "Custom/RenderTextureShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             // make fog work
    17.             //#pragma multi_compile_fog
    18.             #include "UnityCG.cginc"
    19.             struct appdata
    20.             {
    21.                 float4 vertex : POSITION;
    22.                 float2 uv : TEXCOORD0;
    23.             };
    24.             struct v2f
    25.             {
    26.                 float2 uv : TEXCOORD0;
    27.                 //UNITY_FOG_COORDS(1)
    28.                 float4 vertex : SV_POSITION;
    29.             };
    30.             sampler2D _MainTex;
    31.             float4 _MainTex_ST;
    32.             v2f vert (appdata v)
    33.             {
    34.                 v2f o;
    35.                 o.vertex = UnityObjectToClipPos(v.vertex);
    36.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    37.                 //UNITY_TRANSFER_FOG(o,o.vertex);
    38.                 return o;
    39.             }
    40.             fixed4 frag (v2f i) : SV_Target
    41.             {
    42.                 // sample the texture
    43.                 fixed4 col = tex2D(_MainTex, i.uv);
    44.                 // apply fog
    45.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    46.                 return col;
    47.             }
    48.             ENDCG
    49.         }
    50.     }
    51. }