Search Unity

CG ddy function returning negated vector in game view. Macro to check?

Discussion in 'Shaders' started by cjddmut, May 30, 2016.

  1. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    179
    I have a pretty simple pixel shader that calculates the face normal by relying on the ddx and ddy CG functions. Pretty straight forward.

    Code (CSharp):
    1. float3 normal = -normalize(cross(ddx(i.position), ddy(i.position)));
    This worked out very well in the scene view but I get the negated normal in game view.

    Pasted image at 2016_05_29 05_06 PM.png

    The normal is inverted! I realized that this was due to the ddy function. If I just output the result of the ddy function (multiplied by 200) I get the following result.

    Pasted image at 2016_05_29 05_30 PM.png

    So I'm pretty sure that's what's causing the problem (the ddx result is consistent for both the game and screen view). My question is if there's a macro or something I can check for? It looks like maybe the screen y is flipped? I tried using UNITY_UV_STARTS_AT_TOP but it didn't solve this issue.

    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    * _ProjectionParams.x
     
    yyylny likes this.
  3. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    179
    I started looking into checking the projection matrix to use that to see if it flips it or not but this is nicer :) Thanks!