Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Possible GrabPass projection bug or I'm not doing it right

Discussion in 'Shaders' started by NoxWings, Dec 26, 2015.

  1. NoxWings

    NoxWings

    Joined:
    Nov 28, 2013
    Posts:
    3
    Hi, I'm mostly new to shader programming. I've only implemented 2 simple effects before, an outline shader just like unity's one but with a little bit more flexibility and a simple dissolve shader using a noise texture (you can check both here).

    Now I'm trying to learn how to create a distortion shader. If I'm correct I need a grabpass and project that texture onto my object (a plane).

    I've found that I need to use ComputeGrabScreenPos in the vertex shader and then UNITY_PROJ_COORD to make the texture lookup.

    I've only implemeted a simple shader that acts as a "red glass" layer.

    Actually everything is working fine BUT it only works as expected in editor mode and in game mode after building the game but it doesn't work in the editor's game mode.

    *This bug only occurs in forward rendering, deferred is fine.*

    It does a weird thing in editors game mode where Y axis of the projected texture is off.
    As you can see in the image below it works fine in the editor mode and even in the tiny camera preview (on the LEFT), but it is off in the game view (on the RIGHT).




    This is the shader code I'm using:
    Code (CSharp):
    1.  
    2. Shader "GrabPassSimpleLayer"
    3. {
    4.     Properties
    5.     {
    6.         [Enum(UnityEngine.Rendering.CullMode)] _CullMode ("Culling", Float) = 0
    7.     }
    8.  
    9.  
    10.     SubShader
    11.     {
    12.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"}
    13.  
    14.         Blend One Zero
    15.         Lighting Off
    16.         Fog { Mode Off }
    17.         ZWrite On
    18.         Cull [_CullMode]
    19.  
    20.         // Grab the screen behind the object into _GrabTexture
    21.         GrabPass { "_GrabTexture" }
    22.  
    23.         Pass {
    24.             CGPROGRAM
    25.             #pragma target 3.0
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.  
    29.             #include "UnityCG.cginc"
    30.  
    31.             sampler2D _GrabTexture;
    32.  
    33.             struct v2f {
    34.                 float4 pos : POSITION;
    35.                 float4 uvgrab : TEXCOORD1;
    36.             };
    37.  
    38.             v2f vert(appdata_base v)
    39.             {
    40.                 v2f o;
    41.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    42.                 o.uvgrab = ComputeGrabScreenPos(o.pos);
    43.                 return o;
    44.             }
    45.  
    46.             float4 frag(v2f i) : COLOR
    47.             {
    48.                 float4 c = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    49.                 return float4(c.r,0,0,1);
    50.             }
    51.             ENDCG
    52.         }
    53.  
    54.     }
    55. }
    56.  
    EDIT: Btw I'm using it in forward rendering mode. It works fine in deferred rendering.
     

    Attached Files:

    Last edited: Dec 26, 2015
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,202
    I believe I ran into this issue with Unity 5.2, which turned out to be a bug in Unity, but should be fixed by now. Do you use 5.2 or a newer version?

    A while ago, I posted a distortion shader example, which does not have this problem to my knowledge. Maybe you want to take a look at it. A far as I remember, the example does not use tex2Dproj. You can find the download link to the project at the bottom of the following post:
    http://forum.unity3d.com/threads/horizontal-wave-distortion.295769/#post-1950999

    If you download the .zip, the interesting code is located in "Assets/GrabPassDistortion/GrabPassDistortion.cginc".

    Hope it helps.
     
  3. NoxWings

    NoxWings

    Joined:
    Nov 28, 2013
    Posts:
    3
    Thank you Peter77, what a coincidence that after I run into this bug I started searching for distortion examples and found exactly yours ^^ (except I found it in a blog post)

    I'm using unity 5.3.1 and If I recall correctly I think that bug was also happening with your code too!

    Btw your example is really nice, I've learned a few shaderlab things from it so thanks for sharing it.

    PS: I'll re-check it again tomorrow
     
  4. NoxWings

    NoxWings

    Joined:
    Nov 28, 2013
    Posts:
    3
    I tested it, and even though it doesn't use tex2Dproj the same bug is happening with it.
    I'm up to date with unity 5.3.1f1. They may have reintroduced the bug in 5.3 if you say it was already patched in 5.2.

    Edit: How can I report the bug again? I see there is an option in the editor but if there is already a bug report it could be better if we could try to reopen it.
     
  5. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Happens in forward rendering + HDR, I reported this bug, will be fixed in 5.4
     
    Peter77 likes this.
  6. Xix-Interactive

    Xix-Interactive

    Joined:
    Aug 26, 2015
    Posts:
    423
    It was supposedly fixed with 5.3.4, but I'm still seeing it, at least with a complicated shader. Is it fixed with the test case you submitted?
     
unityunity