In a shader, how to pass texture in a variable?

In my shader, based on a certain value, I want to set a variable to point to either the first or second texture. Something like this:

	      if (foundIdx==2.0){
	         tex2use=texTile2Bump;
	      }	   
	      else {
	         tex2use=texTile3Bump;
	      }
              vec4 newColor=texture2D( tex2use, vUv  );

Is this possible, and what type of variable should I use here? Sampler2D appears to be only for uniforms…

Hi!
Maybe better to do it this way:

if ( floor(foundIdx + 0.5) == 2.0){...}

In this case you’ll avoid problems with precision.

1 Like

The foundIdx was not a problem - the value is assigned not calculated, thanks though. I am looking for a type for the variable ‘tex2use’…

I was thinking, either a tiles texture (WebGl2.0 Sampler2DArray) or passing the textures as arrays could be directions to head to. To me the Array Texture feature in WebGL2.0 seems te be the nicest option. How can I use that with THREEJS?

vec4 tex2use; 
if (whatever){
  tex2use = texture2D( texTile2Bump, vUv  );
}	   
else {
  tex2use = texture2D( texTile3Bump, vUv  );
}

Although you could try this

vec4 foo = texture2D( (bar ? map0 : map1) , vUv);
1 Like

…that way I have just one pixel in a variable. I need the whole texture to perform a bit more operations - and later on I even would like to pass it to a function if possible.

I don’t think what you’re trying to do is possible.

edit says it can be passed as a function parameter but im trying to find an example of that

But i still don’t understand what do you mean by the one pixel in a varialbe, at some point you have to lookup a pixel, like you do in your code.

I am aware I will have to lookup the pixel at some point, but I prefer to use that part of the code just once. It also does lookup neighbouring pixels.

I’m afraid it will not possible too, but using an array texture and an index also is an option. But how to doe that in ThreeJS?