How can I colour things in OpenGL ES 2.0 based on their depth?
This is fairly simple to do if you just want to use the Z position of your geometry. You could have a vertex shader like the following:
attribute vec4 position;
varying float zDepth;
uniform mat4 modelViewProjMatrix;
void main()
{
vec4 newPosition = modelViewProjMatrix * position;
gl_Position = newPosition;
zDepth = (2.0 - (1.0 + newPosition.z))/2.0;
}
and a fragment shader like
varying highp float zDepth;
void main()
{
gl_FragColor = vec4(zDepth, zDepth, 0.0, 1.0);
}
which would produce the following look:
I have an example of this within this iPad sample code I assembled for my OpenGL ES 2.0 class.