大家好,这是我的
     
      OpenGL ES
     
     高级进阶系列文章,在我的
     
      github
     
     上有一个与本系列文章对应的项目,欢迎关注,链接:
     
      github.com/kenneycode/…
     
    
     今天给大家介绍一下
     
      纹理数组
     
     ,它是
     
      OpenGL ES 3.0
     
     引入的一个新特性,它能让我们以数组的方式往
     
      shader
     
     中传递纹理,我们先来看看一个普通的
     
      fragment shader
     
     :
    
#version 300 es
precision mediump float;
layout(location = 0) out vec4 fragColor;
layout(location = 0) uniform sampler2D u_texture;
in vec2 v_textureCoordinate;
void main() {
    fragColor = texture(u_texture, v_textureCoordinate);
}
复制代码
     在这个
     
      shader
     
     中我们声明了一个
     
      uniform sampler2D
     
     变量,即我们只能传递一个纹理,如果要传递多个纹理,就要声明多个
     
      uniform sampler2D
     
     :
    
#version 300 es
precision mediump float;
...
layout(location = 0) uniform sampler2D u_texture0;
layout(location = 1) uniform sampler2D u_texture1;
layout(location = 2) uniform sampler2D u_texture2;
...
复制代码
     这样一方面会占用多个纹理单元,另一方面一旦
     
      shader
     
     定了,里面支持的纹理数量也就定了,不利于各种数量的纹理,除非自己去生成
     
      shader
     
     。 纹理数组的出现让我们可以像传递一个数组一样将纹理传给
     
      shader
     
     ,我们来看一下使用纹理数组时的
     
      shader
     
     :
    
 
