我想用渐变色来制作我的场景背景,而不是单一的颜色。这个能做到吗?我在iOS上使用OpenGL ES 2.0。
我想用渐变色来制作我的场景背景,而不是单一的颜色。这个能做到吗?我在iOS上使用OpenGL ES 2.0。
好的,现在作为回答再说一遍。
您可以绘制一个填满整个屏幕的矩形,并将渐变应用于它。为此,请使用矩形顶点的纹理坐标来计算顶点着色器中相应的颜色:
// ...
attribute vec2 textureCoordinate;
varying highp vec4 colorGradient;
// other attributes, uniforms, etc...
void main() {
// This defines the colors of the 4 corners of your rectangle.
// The gradient is then interpolated by the GPU between the vertices.
colorGradient = vec4(textureCoordinate, 0.0f, 1.0f); // adjust as needed
// ... set gl_Position, etc.
}
// ...
varying highp vec4 colorGradient;
// other uniforms, etc...
void main() {
// do additional shading if needed
// colorGradient is already interpolated between the vertices by the GPU
gl_FragColor = colorGradient;
}
varying vec3
传递到片段着色器中(因此由 GPU 自动插值),在其中将颜色设置为gl_FragColor
。 - Nero