I’m kind of dealing with a similar problem (tring to get an Ambient Occlusion effect).
Either make multiple Variables (Object1pos, Object2pos …)
It’s tedious, but in the end, low end shaders have to unroll any kind of for loop anyway, which means a fixed number of objects anyway.
Just define some “empty” state, so the shader can “ignore” it.
The other thing you can try is doing it with a texture. Write the postion etc data to a texture and sample the texture. (with SetPixel and Apply()). Problem here is, you’ll have to encode Float data into RGB.
Do you think I could use matrix instead to pack them all ?
Like my script make array of all data then send those in some matrix that I can send to shader and the shader take the value when the matrix is filled ?
The texture trick is also interresting but I don’t have any idea how to implement that currently.
I haven’t tried it with Matrix, should be possible aswell.
I would probably just go with several variables. I don’t think there’s a speed gain using Matrix instead of several vectors and it would probably just make the less readable.
With a Texture: (pseudo snippet code) JavaScript
var AOareas = new Texture2D(16, 16); //create a Texture
...
// in Start()
AOareas.anisoLevel = 1;
AOareas.filterMode = FilterMode.Point;
// inside Update()
for(var x = 0; x < AOareas.width; x++)
for(var y = 0; y < AOareas.height; y++)
{
AOareas.SetPixel(x, y, UnitToColor24Sign(YOURFLOATPOSITION.x)); // needs to be etween 0 and 1!
}
AOareas.Apply();
// helper function
function UnitToColor24Sign(unit : float) : Color {
var col : Vector4;
var sign = Mathf.Sign(unit);
col = unit * sign * Vector3(1, 255, 65025);
col.y = col.y % 1;
col.z = col.z % 1;
col.x -= col.y * 0.00390625;
col.y -= col.z * 0.00390625;
col.x = Mathf.Clamp01(col.x);
col.y = Mathf.Clamp01(col.y);
col.z = Mathf.Clamp01(col.z);
col.w = sign;
return col;
}
Then inside the Shader you can Loop over the texture and retrieve the values with:
inline float ColorToUnit24Sign(in fixed4 col) {
float f = dot(col.xyz, float3(1.0, 0.00392156862, 0.0000153787));
if(col.w == 0) f *= -1; // sign is in w
return f;
}
Yeah that’s what I meant… I think I misunderstood what you are trying to achieve…
So, you’re mixing together screen space reflections, image based reflections and billboard reflections? …or are you trying to implement parallax-corrected IBR, which have little to do with billboard reflections?
Anyways, don’t use textures for that purpose, if you care about performance. I haven’t tried it myself, but you CAN use arrays in your shaders, even in SM3, as long as you access them with constant indices, which unrolled loops use. The only issue is with populating it with your script. In the compiled shader, you can see that the array is converted into single constants, like this:
Yeah I’m trying to mix them and at the end I’ll have SSRR, ImageBasedReflection (to me billboard reflection is the same but with some BRDF approach) and cube map with parallax correction.
In fact what I want to achieve with those reflective quad is what UDK have.
You place an ImageReflectionCapture object where you want to have some reflection and capture the image behind, then the shader reflect it (not the object used to capture) in a physical plausible way.