Search Unity

World Space Normals

Discussion in 'Shaders' started by Frostbite23, Jan 9, 2016.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Im experimenting with image effects like bloom and ssao, but I searched alot of places and I cannot seem to find world space normals or how to convert screen space normals into world space. I looked everywhere in the forum and though people have been getting world space normals for there objects, I cant get it for the scene. Hopefully you know what I mean but how can I get world space normals in an image effect.
     
  2. LoK4t

    LoK4t

    Joined:
    May 27, 2015
    Posts:
    9
    Not sure what you're looking for exactly, but maybe the _Object2World matrix could be helpful in this case?
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    The ones around the forum I came across were world space normals on only one object, or an object that has that specific shader showing the normals of the object. Im looking for the world normals of every object in the scene. Hard to describe but the ones im currently using and I dont want is the view space normals, which the view space normals change when i move the camera, Im looking for normals that dont change when i move the camera. Which is what the world space normals does (does not change when camera is moving or rotating)

    Or if I try doing a simple ndotl with the view space normals, the light direction is not correct and it changes when i rotate the camera and or move. World space normals is again what im looking for but I am not sure how to get it.
     
  4. LoK4t

    LoK4t

    Joined:
    May 27, 2015
    Posts:
    9
    hmm, well maybe you could do some inverse transformation using the view space matrix (assuming there is one) to transform the viewspace normals to worldspace?
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    I tried doing inverse projection but it does not actually have much of an affect, still changes when moving the camera.
     
  6. LoK4t

    LoK4t

    Joined:
    May 27, 2015
    Posts:
    9
    What is it you're trying to achieve with this? Maybe there is a way to do whatever it is you're trying to do without world space normals?
     
  7. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    I want to avoid using unitys skybox or gradient ambient lighting as it uses the ol dot(n, l) but I want to instead create my own simple one that is a half Lambert term ( dot(n, l) * 0.5 + 0.5 ) and not the standered dot(n, l) so the lighting wraps around nicely. I can do this in the deffered shader pass but I want to do it as a post effect and it is possible.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Well, using the 3 color ambient you can do a top, middle, and bottom color that you can get a half lambert look to.

    Also it doesn't really matter that the view space normals change, you just need to know what direction up is in view space. If you're already writing the effect as a post process you just need to pass the world up in camera space to the shader and do a dot product against that.
     
  9. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    I want to avoid using unitys skybox or gradient ambient lighting as it uses the ol dot(n, l) but I want to instead create my own simple one that is a half Lambert term ( dot(n, l) * 0.5 + 0.5 ) and not the standered dot(n, l) so the lighting wraps around nicely.
     
    Last edited: Mar 10, 2019
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Gradient ambient source with a white sky, 127 middle grey equator, and black ground is identical to a half lambert. Unless you're looking to map a different curve to that half lambert (like using the half lambert with a texture look up or applying a pow) there's no good reason to not use the gradient ambient.

    If you want to do something more advanced your better off writing your own shader as proper ambient lighting shouldn't be done as a post process. The Vertex and Fragment Shader Examples page shows how to setup a shader to do lighting, the key change would be to modify the line that uses ShadeSH9() with your own function.

    If you still want to do it as a post process, I did give you the answer to that. You need to pass the camera space world up vector to the shader and use that when doing the dot product against the normals.

    Alternatively if you're using the deferred renderer those normals are already in world space and you could write your own light type that does a half lambert ambient.
     
  11. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    My bad, missed what you said there " you just need to pass the world up in camera space to the shader and do a dot product against that.", Ive tried that and it works now, thank you