我正在为游戏引擎编写以下片段着色器:

#version 330 core 
 
layout (location = 0) out vec4 color; 
 
uniform vec4 colour; 
uniform vec2 light_pos; 
 
in DATA 
{ 
    vec4 position; 
    vec2 uv; 
    float tid; 
    vec4 color; 
} fs_in; 
 
uniform sampler2D textures[32]; 
 
void main() 
{ 
    float intensity = 1.0 / length(fs_in.position.xy - light_pos); 
 
    vec4 texColor = fs_in.color; 
 
    if(fs_in.tid > 0.0){ 
        int tid = int(fs_in.tid + 0.5); 
        texColor = texture(textures[tid], fs_in.uv); 
    } 
 
    color = texColor * intensity; 
} 

texColor =texture(textures[tid], fs_in.uv);导致编译着色器时“GLSL 1.30及更高版本中禁止使用非常量表达式索引的采样器数组”表达式。

顶点着色器(如果需要)是:

#version 330 core 
 
layout (location = 0) in vec4 position; 
layout (location = 1) in vec2 uv; 
layout (location = 2) in float tid; 
layout (location = 3) in vec4 color; 
 
uniform mat4 pr_matrix; 
uniform mat4 vw_matrix = mat4(1.0); 
uniform mat4 ml_matrix = mat4(1.0); 
 
out DATA 
{ 
    vec4 position; 
    vec2 uv; 
    float tid; 
    vec4 color; 
} vs_out; 
 
void main() 
{ 
    gl_Position = pr_matrix * vw_matrix * ml_matrix * position; 
    vs_out.position = ml_matrix * position; 
    vs_out.uv = uv; 
    vs_out.tid = tid; 
    vs_out.color = color; 
} 

请您参考如下方法:

在 GLSL 3.3 中,采样器数组的索引仅允许 integral constant expression (参见GLSL 3.3 Spec, Section 4.1.7)。

在更现代的版本中,从 GLSL 4.0 开始,允许通过dynamic uniform expressions索引采样器数组。 (参见GLSL 4.0 Spec, Section 4.1.7)

您实际上尝试的是通过变量来索引数组,这是根本不可能的。如果这样做绝对不可避免,您可以将 2d 纹理打包到 2d 数组纹理或 3d 纹理中,并使用索引来寻址纹理的图层(或第 3 维)。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!