Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    84
    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:
    4,883
    * _ProjectionParams.x
     
  3. cjddmut

    cjddmut

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