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

[SOLVED] Raycasting shader used to work on 5.4, buggy in 5.5

Discussion in 'Shaders' started by N3zix, Feb 1, 2017.

  1. N3zix

    N3zix

    Joined:
    Oct 22, 2014
    Posts:
    23
    I am working on a raycasted sphere shader in Unity, that used to work fine on Unity 5.4 and before in DX11 but behaves differently on Unity 5.5 DX11.

    In Unity 5.4:


    The same scene in Unity 5.5:


    When changing the far plane to a small value, the sphere goes back to normal.
    This bug is not observed with OpenGL or in DX9.

    My guess is that Unity 5.5 changed something in the projection matrix for DirectX 11 but I cannot see any related change in the Unity 5.5 changelog.

    Am I missing something obvious ?
     

    Attached Files:

  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    It would help to see your shader code if possible. Are you doing any MATRIX_MVP mul()'ing?
     
  3. N3zix

    N3zix

    Joined:
    Oct 22, 2014
    Posts:
    23
    Yeah, I do :
    Code (CSharp):
    1. o.p = UnityObjectToClipPos(spaceposition);//mul(UNITY_MATRIX_MVP, spaceposition);
    In the vertex shader :

    Code (CSharp):
    1.  
    2.             // vertex input: position
    3.             struct appdata {
    4.                 float4 vertex : POSITION;
    5.             };
    6.  
    7.  
    8.             struct v2p {
    9.                 float4 p                : POSITION;
    10.                 float4 i_near           : TEXCOORD1;
    11.                 float4 i_far            : TEXCOORD2;
    12.                 float4 colonne1         : TEXCOORD3;
    13.                 float4 colonne2         : TEXCOORD4;
    14.                 float4 colonne3         : TEXCOORD5;
    15.                 float4 colonne4         : TEXCOORD6;
    16.             };
    17.  
    18.             struct fragment_out
    19.             {
    20.               float4 color : SV_Target;
    21.               float depth  : SV_Depth;
    22.             };
    23.       // VERTEX SHADER IMPLEMENTATION =============================
    24.  
    25.             v2p ballimproved_v (appdata v) {
    26.  
    27.  
    28.                 float4x4 ModelViewProjI = mat_inverse(UNITY_MATRIX_MVP);
    29.  
    30.                 v2p o; // Shader output
    31.  
    32.                 float4 spaceposition;
    33.                 spaceposition.xyz = _TexPos.xyz;  
    34.                 spaceposition.w =1.0;
    35.  
    36.                 spaceposition.xyz += v.vertex.xyz * (2.0 * _Rayon);
    37.              
    38.                 o.p = UnityObjectToClipPos(spaceposition);//mul(UNITY_MATRIX_MVP, spaceposition);
    39.                 v.vertex = o.p;
    40.                          
    41.  
    42.                 float4 near = o.p ;
    43.                 near.z = 0.0 ;
    44.                 near = mul(ModelViewProjI, near) ;
    45.  
    46.                 float4 far = o.p ;
    47.                 far.z = far.w ;
    48.                 o.i_far = mul(ModelViewProjI,far) ;
    49.                 o.i_near = near;
    50.              
    51.                 ...
    52.                 return o;  
    53.             }
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    You're using the UnityObjectToClipPos, so that's fine. Uhh, I notice you're using depth as well, are you using UNITY_REVERSED_Z shader define macro to check if your depth value should be inverted?
     
  5. N3zix

    N3zix

    Joined:
    Oct 22, 2014
    Posts:
    23
    I just tried and it does not fix the issue. Still, I think I have to do it.
     
  6. N3zix

    N3zix

    Joined:
    Oct 22, 2014
    Posts:
    23
    As expected, there was a problem with the calculation of the near and the far plane used to throw rays in the fragment shader.