i am trying to figure out how to access specific geo instances, and assign to them specific glsl mats.
so- i have an instanced array of geo objects. i have figured out how to use renderpick chop to determine which instance id(s) i’d like to manipulate. i have a glsl mat for my base / original object, and a glsl mat i’d like to apply to selected instances of the base object.
what is the best way to go about accomplishing this? if i have 600 instances, and i want to apply this mat to (theoretically) instances numbered 1,5,8,300 - will i use gl_InstanceID in in some code in the vertex shader to “tag” those instances, and assign the mat to them only? in the pixel shader? both?
once i have figured out how to assign simple mats to individual instances, i plan on making more complex glsl shaders to deform these instances separately.
i’m slowly wrapping my brain around glsl coding, and i have a good grasp of the fundamentals- but definitely still a newbie.
You’ll need to do it all in one GLSL MAT. You can’t change shaders mid-render.
So in your vertex shader you do
if (gl_InstanceID == an id I want the first mat to be applied to)
{
vMatIndex = 0.5;
}
else // material 2
{
vMatIndex = 1.5;
}
Where vMatIndex is a varying. I use 0.5 offsets here to deal with 1.0 getting changed to 0.99999 or 1.00001, during the interpolation across the polygon.
Then in the pixel shader you do
if (vMatIndex > 1.0)
{
// Do material 2 calculations here
}
else
{
// Do material 1 calculations here
}
Actually an alternative is this:
You have two GLSL MATs, each one knowing which IDs they should be applied to (via a uniform or mathematical formula etc.)
In the vertex shader if the gl_instanceID is something that you don’t want to be applied to, write
gl_Position = vec4(0.0);
This will throw the polgyon offscreen, and it’ll get culled out quickly.
So you two do renders of your full set of instances, one with each mat.
If “accessing a different material for each geometry instance” means you simply want each instance to have a different texture map, then you can put your textures in a Texture 3D TOP, and use the W texture coordinate in each instance to access the different slices of textures. No need to write your own shader.