Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bumped Distort shader Unity 5.6.1 VR Single pass stereo rendering

Discussion in 'General Graphics' started by Konst, May 23, 2017.

  1. Konst

    Konst

    Joined:
    Aug 26, 2013
    Posts:
    78
    Seems like shader does not work as intended even with
    #if UNITY_SINGLE_PASS_STEREO
    i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
    #endif

    Unity 5.6.1 VR Single pass stereo rendering Win 10 Oculus VC1

    Thanks
     
  2. Konst

    Konst

    Joined:
    Aug 26, 2013
    Posts:
    78
    Gentlemans, what should i fix in GlassStainedBumpDistort.shader
    Code (CSharp):
    1. // Per pixel bumped refraction.
    2. // Uses a normal map to distort the image behind, and
    3. // an additional texture to tint the color.
    4.  
    5. Shader "FX/Glass/Stained BumpDistort" {
    6. Properties {
    7.     _BumpAmt  ("Distortion", range (0,128)) = 10
    8.     _MainTex ("Tint Color (RGB)", 2D) = "white" {}
    9.     _BumpMap ("Normalmap", 2D) = "bump" {}
    10. }
    11.  
    12. Category {
    13.  
    14.     // We must be transparent, so other objects are drawn before this one.
    15.     Tags { "Queue"="Transparent" "RenderType"="Opaque" }
    16.  
    17.  
    18.     SubShader {
    19.  
    20.         // This pass grabs the screen behind the object into a texture.
    21.         // We can access the result in the next pass as _GrabTexture
    22.         GrabPass {
    23.             Name "BASE"
    24.             Tags { "LightMode" = "Always" }
    25.         }
    26.        
    27.         // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
    28.         // on to the screen
    29.         Pass {
    30.             Name "BASE"
    31.             Tags { "LightMode" = "Always" }
    32.            
    33. CGPROGRAM
    34. #pragma vertex vert
    35. #pragma fragment frag
    36. #pragma multi_compile_fog
    37. #include "UnityCG.cginc"
    38.  
    39. struct appdata_t {
    40.     float4 vertex : POSITION;
    41.     float2 texcoord: TEXCOORD0;
    42. };
    43.  
    44. struct v2f {
    45.     float4 vertex : SV_POSITION;
    46.     float4 uvgrab : TEXCOORD0;
    47.     float2 uvbump : TEXCOORD1;
    48.     float2 uvmain : TEXCOORD2;
    49.     UNITY_FOG_COORDS(3)
    50. };
    51.  
    52. float _BumpAmt;
    53. float4 _BumpMap_ST;
    54. float4 _MainTex_ST;
    55.  
    56. v2f vert (appdata_t v)
    57. {
    58.     v2f o;
    59.     o.vertex = UnityObjectToClipPos(v.vertex);
    60.     o.uvgrab = ComputeGrabScreenPos(o.vertex);
    61.     o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
    62.     o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    63.     UNITY_TRANSFER_FOG(o,o.vertex);
    64.     return o;
    65. }
    66.  
    67. sampler2D _GrabTexture;
    68. float4 _GrabTexture_TexelSize;
    69. sampler2D _BumpMap;
    70. sampler2D _MainTex;
    71.  
    72. half4 frag (v2f i) : SV_Target
    73. {
    74.     #if UNITY_SINGLE_PASS_STEREO
    75.     i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
    76.     #endif
    77.  
    78.     // calculate perturbed coordinates
    79.     half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
    80.     float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    81.     #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
    82.         i.uvgrab.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(i.uvgrab.z) + i.uvgrab.xy;
    83.     #else
    84.         i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    85.     #endif
    86.  
    87.     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    88.     half4 tint = tex2D(_MainTex, i.uvmain);
    89.     col *= tint;
    90.     UNITY_APPLY_FOG(i.fogCoord, col);
    91.     return col;
    92. }
    93. ENDCG
    94.         }
    95.     }
    96.  
    97.     // ------------------------------------------------------------------
    98.     // Fallback for older cards and Unity non-Pro
    99.  
    100.     SubShader {
    101.         Blend DstColor Zero
    102.         Pass {
    103.             Name "BASE"
    104.             SetTexture [_MainTex] {    combine texture }
    105.         }
    106.     }
    107. }
    108.  
    109. }
    110.  
     
  3. sunyifeng

    sunyifeng

    Joined:
    Nov 4, 2014
    Posts:
    28
    We have the same problem.
    Unity 5.6.1 VR Single pass stereo rendering broken everything...
     
  4. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    Yep. There are definite shader problems in Unity 5.6.1p3 and single pass stereo rendering (and the Vive).

    I've opened:
    (Case 921280) Single Pass renderinng not working with Particle Systems that have been upgraded
     
  5. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    The QA team is looking at the bug report I submitted. Sunyifeng, have you filed your own bug report for this? Can you provide any more specific information about what problem you are having (are you using the Vive or the Rift?). Was the issue you are having working in any previous versions of unity? 5.6? 5.5?
     
  6. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,876
    VR SPSR in Unity 5.5 works fine.
     
  7. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Having the same issue in 2017.1.0p5; GlassStainedBumpDistort.shader from the standard assets is not working in SPSR (inversely rotates the reflections/refractions along with the camera) despite the #IF UNITY_SINGLE_PASS_STEREO fragment. @Konst, did you manage to resolve this?
     
  8. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    OK. If you simply remove these three lines from the shader, it works in SPSR. It seems the "fix" is no longer applicable.

    Code (csharp):
    1.  
    2. #if UNITY_SINGLE_PASS_STEREO
    3.    i.uvgrab.xy = TransformStereoScreenSpaceTex(i.uvgrab.xy, i.uvgrab.w);
    4. #endif
    5.  

    I've opened case 946318 specifically for the glass stained bump distort shader so hopefully it will be fixed in future standard asset releases.
     
    robot-ink and Konst like this.