Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    86
    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:
    5,746
    * _ProjectionParams.x
     
  3. cjddmut

    cjddmut

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