Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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,520
    * _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!