我正在尝试在片段着色器中使用 2 个 mipmap 纹理。我无法在加载之前将它们组合起来,因为我必须从单独的源(即单独的文件)加载纹理。在渲染过程中,我必须根据属性在来自一个或另一个采样器的纹理数据之间进行选择。为了获得更多背景信息,我正在使用 2 个双通道图像(亮度和 Alpha)。
尝试访问非均匀条件块(即无法在编译时评估的条件)内的 mipmap 纹理会给出未定义的值。因此,一种方法是对每个通道的前两个通道进行采样,然后根据条件选择正确的通道(或无分支方法,但这并不重要)。
作为替代方案,我想知道是否可以在初始化期间将这两个双通道图像组合成一个四通道图像(即前两个是图像一,后两个是图像二),并且无论如何都始终获取所有四个通道,消除额外的开销。这可能吗?
这是一个片段着色器示例,显示了方法 1:
#version 100
precision mediump float;
varying vec2 v_objectTexCoord;
varying vec3 v_objectColorRGB;
varying float v_objectType; // 0.0f == first texture, 1.0f == second texture
uniform sampler2D u_tex_sampler_1;
uniform sampler2D u_tex_sampler_2;
// NOTE: this shader may only be compatible with OpenGL ES 2.0 at this time!
void main()
{
// The texture's red channel is luminance, and texture's green channel is alpha.
vec4 object_texture_1 = texture2D(u_tex_sampler_1, v_objectTexCoord);
vec4 object_texture_2 = texture2D(u_tex_sampler_2, v_objectTexCoord);
float luminance = object_texture_1.r * (1.0 - v_objectType) + object_texture_1.r * v_objectType;
float alpha = object_texture_1.a * (1.0 - v_objectType) + object_texture_1.a * v_objectType;
// If the texture is transparent, discard the fragment entirely.
if (alpha < 0.01)
{
discard;
}
// Object's color is based on the color attribute, with luminance and alpha applied.
vec4 color = vec4(v_objectColorRGB * luminance, alpha)
gl_FragColor = color;
}