[英]How can I colour things in OpenGL ES 2.0 based on their depth?
I'm writing an OpenGL ES 2.0 game (on iOS). 我正在编写一个OpenGL ES 2.0游戏(在iOS上)。 How can I create a shader (since I assume this would be simpler to do in a shader) so that geometry further from the origin (on the Z axis) appears darker? 如何创建着色器(因为我假设在着色器中这样做更简单),因此距离原点(在Z轴上)的几何体看起来更暗?
The water in this image illustrates the effect I have in mind 这张照片中的水说明了我的想法
(source: bytecellar.com ) (来源: bytecellar.com )
This is fairly simple to do if you just want to use the Z position of your geometry. 如果您只想使用几何体的Z位置,这很简单。 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. 我在为我的OpenGL ES 2.0类组装的这个iPad示例代码中有一个这样的例子。
I did a rather interesting test with what @brad provide. 我用@brad提供了一个相当有趣的测试。 I just change the line: 我只是改变了这条线:
gl_FragColor = vec4(1.0, 1.0, 0.0, zDepth);
Nothing happen to the alpha. 阿尔法没有任何事情发生。 Is it me or is there something else is missing as for the Alpha
? 这是我还是Alpha
缺少其他东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.