I’m trying to write a shader with logic dependent on fragment’s screen position.
Basically that’s what gl_Position is from vertex shader, which is usually a result of matrix_viewProjection * matrix_model * vertex_position.
In pixel shader screen position is expected to be calculated with something like this: vec4 pos = matrix_viewProjection * vec4 (vPositionW, 1.0);
But that doesn’t produce the intended result. When testing it with code like this: vec3 emission = pos.x > 0.5 ? vec3 (0.0, 1.0, 0.0) : vec3 (1.0, 0.0, 0.0);
I get colors split not in screen space, but with some virtual plane of sort:
i have a solution. I only keep the gl_Position from vert to frag shader and make the “simple” calculation first in frag shader. getScreenPosition() return the screen position between vec2(0.0, 0.0) and vec2(1.0, 1.0), when you apply display width and height you could calculate exact screen pixel.