How to get pixel location inside a fragment shader?

Hi,
Now I have created my first super simple shader (see Could need some advice on adapting the SHADER BURN example - #27 by yaustar thread), I would like to try a step further.

I would like to adapt the shader such that I can dissolve depending on the position of a pixel (relative to model origin).

So the question is:
How can I get access to the position information of the pixel that is being rendered? I would like to have the x,y,z position relative to model origin.

I hope that it is not to difficult, because I have very little experience with shader code. But I like to learn.

Hi @simmania,

To get the vertex world position for the rendered pixel, if you are using shader chunks then it’s available on this varying:

vPositionW

You could pass the world position of the origin as uniform, then subtract the above to get the local relative position.

If you’d like even more precision like the actual world position of each pixel then you will need to reconstruct the world position from the depth. I don’t have an example in mind for that, but there is this older post that is quite relevant:

Hope that helps.

1 Like