Hello!
I am writing a shader that creates holes in objects (to illustrate drillings), by using the clip() function in a surface shader.
The shader itself is pretty straight forward; I supply a hole position and hole radius as shader properties, then do the calculation.
What I am asking about is if there is any way of accessing separate components of a float3 (for example) using an int for indexing?
Something like this:
... IN.vertexPos[_Direction] ...
I can write IN.vertexPos[0], but that is essentially the same as just writing “.x”, but it would make my life a lot easier if I could use a non-constant integer for this indexing. When I try it, I get this error:
Shader error in 'Reflective_holes: Program 'SurfShaderInternalFunc', can't find __getVectorIndex function in stdlib at line 1554
This makes me think that this “should” be possible somehow.
Anyone got any ideas?
off-hand i’m going to say probably not as you could be changing the referenced index value at runtime and shader code didn’t use to like that sort of thing. Maybe someone else will correct me if i’m wrong?
However you also can’t pass in ints to shaders AFAIK and so IN.vertexPos[float] wouldn’t make any sense either.
Depending on what you are trying to achieve there might be workarounds such as using basic math equation to extract the desired component.
e.g.
vertComponent = lerp(IN.vertexPos.x, IN.vertexPos.y, Direction);
or better
float vertComponent = (Direction > 0.5) ? IN.vertexPos.x : IN.vertexPos.y;
Where direction is a float of either 0 or 1 so you get either x or y.
Though again depending upon what you are trying to achieve they may still be better methods of achieving the end result.
Thank you for your reply!
The second solution would be fine it it wasn’t for the fact that I am right at the edge of the instruction limit. This is a reflective bumped shader that also has the capability of showing a hole, and it is for the Flash player export, which means a low instruction limit.
Right now I have separate shaders for each hole direction (just orthagonal directions), which certainly isn’t a pretty solution; but at least a functioning one.
/S
Well if you are at the instruction limit there isn’t anything I can do about that 
How about passing in a orthogonal vector instead of a float and multiplying it against IN.VertexPos to zero out two axis?
That might actually work. I will give that a try!
Thanks! I’ll get back to you regarding this 