[PD] How to dynamically choose which image to use as texture in a shader (or, understanding tex units and texture id's)

Matteo Sisti Sette matteosistisette at gmail.com
Fri Jun 10 21:20:38 CEST 2011


Hi,

Let's first consider an example that does not involve shaders to explain 
what's the thing that I can already do with images and that I would like 
to apply to shaders.

Say I have M images and N squares, and I want to dynamically reassign 
which image to use as a texture on which square. It may be M>N or M<N 
but let's suppose M>N. At any time I want to say: rectangle m uses image 
n. Multiple rectangles may be using the same image at a given time.

For a series of reasons I can't afford using "open" messages because I 
want all the images to be preloaded. So one approach is to use 
[pix_texture]'s right inlets and outlets.

I have M:
   [pix_image]
    |
   [pix_texture]

and N:
   [pix_texture]
    |
   [square]

By using [pix_texture]'s right outlets and inlets (with sends and 
receives) I can "map" the texture I want to the square I want.


Now suppose that for each rectangle I want to use a shader that somewhat 
mixes two (or may be even more) of the M textures, and I want to select 
which ones dinamically.


How do I accomplish that? How do I "pass" texture id's (those coming 
from the right outlets of textures) to the shader and have it correspond 
to a sampler2D variable in the shader?


So far the only way to work with multiple textures in a shader and 
dynamically set which textures to use from the patch, is to use 
texunits. I send a [pix_texture] a "texunit K" message, and I send the 
same number K to a uniform sampler2D variable of the shader.
However, there can only be up to 4 texunits available (maybe 8 on some 
platform)!!!!! So this would only work for M and N <4 which is not my case.

There must be a way to use those texture id's that [pix_texture] uses, 
isn't there?


Thanks
m.



More information about the Pd-list mailing list