I have a bunch of texture IDs that each correspond to an image, I would like an integer
to be passed in as an attribute, and then just get a uniform sampler2D <integer>
You cannot do this in glsl, since sampler2D
has to be a uniform, so I took the approach of just activating a uniform based on a provided integer. Is there a way to directly get a sampler2D
based on a textureId
without if statements?
Code
precision mediump float;
uniform sampler2D bedrock;
uniform sampler2D dirt;
uniform sampler2D grass;
uniform sampler2D gravel;
uniform sampler2D sand;
uniform sampler2D snow;
uniform sampler2D stone;
varying float vTextureId;
varying vec2 vUv;
varying float vBrightness;
int bedrockId = 0;
int stoneId = 1;
int dirtId = 2;
int sandId = 4;
int grassId = 5;
int gravelId = 6;
int snowId = 7;
void main() {
int textureId = int(vTextureId);
vec4 texel;
if (textureId == bedrockId) {
texel = texture2D(bedrock, vUv);
} else if (textureId == stoneId) {
texel = texture2D(stone, vUv);
} else if (textureId == dirtId) {
texel = texture2D(dirt, vUv);
} else if (textureId == sandId) {
texel = texture2D(sand, vUv);
} else if (textureId == grassId) {
texel = texture2D(grass, vUv);
} else if (textureId == gravelId) {
texel = texture2D(gravel, vUv);
} else if (textureId == snowId) {
texel = texture2D(snow, vUv);
};
gl_FragColor = vec4(texel.xyz * vBrightness, 1);
}