# Reconstruct normal from position

This topic is 3349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is it possible to reconstruct approximate worldspace normals from worldspace position in a pixel shader?

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by ExorcistYes, but by approximate its pretty much going to be at half-resUse 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 ?

##### Share on other sites
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 :)

##### Share on other sites
Quote:
 Original post by ExorcistKnowing 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 ?

##### Share on other sites
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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013608
×