Reconstruct normal from position

Started by
4 comments, last by Digitalfragment 14 years, 6 months ago
Is it possible to reconstruct approximate worldspace normals from worldspace position in a pixel shader?
Advertisement
Yes, but by approximate its pretty much going to be at half-res

Use the derivative functions to find the worldspace vector pointing right and down, then normalize and cross-product for your answer
Quote:Original post by Exorcist
Yes, but by approximate its pretty much going to be at half-res

Use the derivative functions to find the worldspace vector pointing right and down, then normalize and cross-product for your answer


This is great, where did you learn it ?

Does it hold because the derivate of the position is the speed, so the direction ?
Knowing what ddx() and ddy() do i took a guess and it worked :)

Basically the pixel shader works on 2x2 blocks for the sake of mipmapping etc, and the derivative functions work on these blocks by giving you the derivative along x and y of what ever variable you pass in. If you pass in a world position, then the derivatives are going to be the difference in position along the screen x and y - i.e. directions in worldspace :)
Quote:Original post by Exorcist
Knowing what ddx() and ddy() do i took a guess and it worked :)

Basically the pixel shader works on 2x2 blocks for the sake of mipmapping etc, and the derivative functions work on these blocks by giving you the derivative along x and y of what ever variable you pass in. If you pass in a world position, then the derivatives are going to be the difference in position along the screen x and y - i.e. directions in worldspace :)


Yes, it makes perfectly sense :D

So, since ddx() and ddy() work on a 2x2 block, they are gonna give you the difference of the variable in the current fragment and the preceeding fragment, divided by one, so just giving you the difference.

if the current fragment has coordinate (x, y):

ddx(k) = k(x, y) - k(x-1,y)
ddy(k) = k(x, y) - k(x,y-1)

is it right ?

---

Going back to the topic start question, it should not work along edges, does it ?
As for your equation, yeah that's correct. One thing to remember though, is that X and Y here in this case are always the same corner of every 2x2 block (cant remember if its bottom-right or top-right, depends on the direction of rasterization i guess)

If its at the time of drawing the geometry, then the edges wont be a problem (the entire 2x2 block is solved by the shader regardless of if it falls partially on a polygon edge)

If you are doing it after converting a z-buffer depth into a world position, then you will run into continuity problems along edges, simply because of the severe change in Z.

This topic is closed to new replies.

Advertisement