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

Normalized Viewport Rect causing Image effect weirdness

Discussion in 'Shaders' started by metervara, Mar 5, 2010.

  1. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Having a bit of trouble with an effect I'm putting together. It's basically the same as the underwater distortion in blush and the heat refraction particles Forrest did a while back, I'm basically trying to recreat it to fit our needs.

    It works by rendering a normal-map using RenderWithShader in OnPreRender(), then compositing that with the standard image from the camera in a custom shader. Image shows standard image, normal-map (particles w replacement shader) and composit.



    Problem is that if I set the Normalized Viewport Rect to anything else than full-screen, the effect gets mangled up:



    I'm guessing something goes wrong in the composit shader (the heat effect has a bunch of stuff in it that I excluded, maybe I'm cutting too many corners?)

    Code (csharp):
    1. Shader "Hidden/Refract 01" {
    2. Properties {
    3.     _MainTex ("", RECT) = "white" {}
    4.     _Bump ("", RECT) = "white" {}
    5. }
    6. Category {
    7.  
    8.     SubShader{
    9.         Pass{
    10.        
    11.         ZTest Always Cull Off ZWrite Off Fog { Mode off }
    12.        
    13. CGPROGRAM
    14. #pragma fragment frag
    15. #pragma fragmentoption ARB_precision_hint_fastest
    16. #pragma fragmentoption ARB_fog_exp2
    17.  
    18. uniform samplerRECT _MainTex : register(s0);
    19. uniform samplerRECT _Bump : register(s1);
    20.  
    21. struct v2f {
    22.     float2 uv[2] : TEXCOORD0;
    23. };
    24.  
    25. uniform float _BumpAmt;
    26.  
    27. half4 frag( v2f i ) : COLOR
    28. {
    29.    
    30.     half2 bump = texRECT( _Bump, i.uv[1] ).rg * 2 - 1;
    31.    
    32.     i.uv[0] -= bump * _BumpAmt;
    33.    
    34.     half4 col = texRECT( _MainTex, i.uv[0] );
    35.    
    36.    
    37.     return col;
    38. }
    39.  
    40. ENDCG
    41.         }
    42.     }
    43.  
    44. }
    45. }
    Any shader wizards out there that can tell me what's going on?

    thanks - Patrik
     
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Here's the Blush Distortion Composite shader for reference. I used pretty much the same technique. Though never tested it with a different normalized viewport rect, so I imagine mine doesn't work either. Don't have time to look into it deeper at the moment, getting ready for my flight out to GDC tomorrow! Hope this helps though!

    _MainTex = Screen Render
    _DistortMap = Rendered Distortion
    _BumpMap = "Perlin Noise"-ish Normals map

    Code (csharp):
    1. Shader "Squiddy/Post Processing/Water Distortion" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", RECT) = "white" {}
    4.         _DistortMap ("Base (RGB)", RECT) = "bump" {}
    5.         _BumpMap ("Bump (RGB)", 2D) = "bump" {}
    6.     }
    7.     SubShader {
    8.         Pass {
    9.             ZTest Always Cull Off ZWrite Off
    10.             Fog { Mode off }
    11. CGPROGRAM
    12. #pragma vertex vert
    13. #pragma fragment frag
    14. #pragma fragmentoption ARB_precision_hint_fastest
    15. #include "UnityCG.cginc"
    16.  
    17. samplerRECT _MainTex : register(s0);
    18. float4 _MainTex_TexelSize;
    19. samplerRECT _DistortMap : register(s1);
    20. sampler2D _BumpMap : register(s2);
    21.  
    22. struct v2f {
    23.     float4 pos : POSITION;
    24.     float2 uvmain : TEXCOORD0;
    25.     float2 uvdistort : TEXCOORD1;
    26.     float2 uvbump : TEXCOORD2;
    27. };
    28.  
    29. v2f vert( appdata_img v )
    30. {
    31.     v2f o;
    32.     o.pos = mul (glstate.matrix.mvp, v.vertex);
    33.     o.uvmain = MultiplyUV( glstate.matrix.texture[0], v.texcoord);
    34.     o.uvdistort = MultiplyUV( glstate.matrix.texture[1], v.texcoord);
    35.     o.uvbump = MultiplyUV( glstate.matrix.texture[2], v.texcoord);
    36.     return o;
    37. }
    38.  
    39. uniform float4 _Data; // (_Time * _Speed, _Time * _Speed * 2, _BumpAmt, _BumpAmtParticles)
    40.  
    41. half4 frag (v2f i) : COLOR
    42. {
    43.     // General Distortion Overlay
    44.     i.uvbump.x += _Data.x + 0.02;
    45.     i.uvbump.y -= _Data.x + 0.44;
    46.     float2 bump = tex2D(_BumpMap, i.uvbump).rg * 2 - 1;
    47.     i.uvbump.x -= _Data.y + 0.21;
    48.     i.uvbump.y += _Data.y + 0.87;
    49.     bump += tex2D(_BumpMap, i.uvbump).rg * 2 -1;
    50.     bump *= _Data.z;
    51.    
    52.     // Material Distortion Addition
    53.     float2 addBump = texRECT(_DistortMap, i.uvdistort.xy).rg * 2 - 1;
    54.     addBump *= _Data.w;
    55.    
    56.     // Composite Distortion
    57.     float2 offset = bump + addBump;
    58.     #ifdef SHADER_API_D3D9
    59.     offset *= _MainTex_TexelSize.xy;
    60.     #endif
    61.     i.uvmain.xy = offset + i.uvmain.xy;
    62.    
    63.     // Sample maintexture
    64.     half4 outCol = texRECT(_MainTex, i.uvmain.xy);
    65.    
    66.     // Debug
    67.     //outCol = half4(addBump.x, addBump.y, 0.0, 1.0);
    68.     //outCol = half4(offset.x, offset.y, 0.0, 1.0);
    69.     //outCol = half4(i.uvmain.x, i.uvmain.y, 0.0, 1.0);
    70.     //outCol = half4(i.uvmain.z, 0.0, 0.0, 1.0);
    71.    
    72.     // Final color
    73.     return outCol;
    74. }
    75. ENDCG
    76.  
    77.             // Fixed function Vertex Program
    78.             SetTexture [_MainTex] {}    // Source
    79.             SetTexture [_DistortMap] {} // Distort
    80.             SetTexture [_BumpMap] {}        // Bump
    81.         }
    82.     }
    83.     FallBack Off
    84. }
    85.  
     
  3. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Great, thanks Shawn. Some of the things I blatantly ignored in my version are present in yours so there's hope :)

    Have fun at GDC,

    /Patrik
     
  4. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    No luck.

    Also tried the ShaderReplacement examples and found that the issue is present there too, in the 'depth of field' and 'glowing things' examples. Filed a bug-report (335199).

    I'm interested in a workaround and will look at that, suggestions/solutions are very welcome :)

    thanks,
    Patrik
     
  5. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Well, it was no bug and no shader issue...

    I'm using Camera.CopyFrom(...) which causes the shaderreplacement-camera to render using whatever rect was setup in the main camera. So after Camera.CopyFrom(...) I reset the rect of the shaderreplacement-camera to 0,0,1,1

    Problem solved :)

    /P