在OpenGL ES 2.0中绘制渐变背景

3

我想用渐变色来制作我的场景背景,而不是单一的颜色。这个能做到吗?我在iOS上使用OpenGL ES 2.0。


您可以绘制一个填满整个屏幕的矩形,并将渐变应用于它。 - Nero
1
您可以使用矩形四个顶点的纹理坐标,在顶点着色器中计算各自的颜色,将其作为 varying vec3 传递到片段着色器中(因此由 GPU 自动插值),在其中将颜色设置为 gl_FragColor - Nero
1个回答

4

好的,现在作为回答再说一遍。

您可以绘制一个填满整个屏幕的矩形,并将渐变应用于它。为此,请使用矩形顶点的纹理坐标来计算顶点着色器中相应的颜色:

// ...
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”后期添加Alpha通道,但使用“vec4”会更清晰一些。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接