GLSL:片段着色器什么时候退出/返回

时间:2018-04-30 22:05:22

标签: opengl-es glsl

假设我们有以下片段着色器:

varying vec2 vUv; // uv coordinates
varying float vTexture; // texture index in array of textures

uniform sampler2D textures[2]; // number of textures

void main() {
  int textureIndex = int(floor(vTexture)); // convert texture index to int for equality checks

  if (textureIndex == 0) {
    gl_FragColor = texture2D(textures[0], vUv);
  }

  if (textureIndex == 1) {
    gl_FragColor = texture2D(textures[1], vUv);
  } 
}

如果通过此着色器的给定值具有textureIndex = 0,那么该值是否会检查第二个if条件,还是gl_FragColor的建立会导致main()函数“返回”还是退出?有没有办法强制片段着色器在给定点退出或返回?

1 个答案:

答案 0 :(得分:0)

新答案(特定于问题)

void main() {
  int textureIndex = int(floor(vTexture)); // convert texture index to int for equality checks

  if (textureIndex == 0) {
    gl_FragColor = texture2D(textures[0], vUv);
  }

  if (textureIndex == 1) {
    gl_FragColor = texture2D(textures[1], vUv);
  } 

  // <--- HERE (ONCE IT RUNS THE CODE ABOVE, IT REACHES THE "END" AND EXITS)

}