- Home /
Shader float4[]
I have to use array in the shader (float4[]) but I can't get it to work:
float4[] spheres;
spheres[0] = float4(10, 1, 10, 1);
spheres[1] = float4(8, 1, 10, 1);
spheres[2] = float4(6, 1, 10, 1);
spheres[3] = float4(12, 1, 10, 1);
float Sphere1 = sdSphere(p - spheres[0].xyz, spheres[0].w);
float Sphere2 = sdSphere(p - spheres[1].xyz, spheres[1].w);
return opSmoothUnion(Sphere1, Sphere2, 1);
When I use separate variables, everything works perfectly, but there is a problem with array.
How should I define and use them in a shader?
Answer by MichaI · Sep 28, 2019 at 12:55 PM
Declaring array in shader you have to specify its length like this
float4 spheres[4];
Also great tutorial about arrays in shader you can find here: https://www.alanzucconi.com/2016/10/24/arrays-shaders-unity-5-4/
Still, it doesn't work as it should.
It works well in this way:
C#:
public Vector4 _sphere1, _sphere2;
material.SetVector("_sphere1", _sphere1);
material.SetVector("_sphere2", _sphere2);
Shader:
uniform float4 _sphere1, _sphere2;
float distanceField(float3 p)
{
float Sphere1 = sdSphere(p - _sphere1.xyz, _sphere1.w);
float Sphere2 = sdSphere(p - _sphere2.xyz, _sphere2.w);
return opSmoothUnion(Sphere1, Sphere2, 1);
}
And when I change it to array it looks like this:
C#:
public Vector4[] _blobs;
material.SetVectorArray("_blobs", _blobs);
Shader:
uniform float4 _blobs[2];
float distanceField(float3 p)
{
float Sphere1 = sdSphere(p - _blobs[0].xyz, _blobs[0].w);
float Sphere2 = sdSphere(p - _blobs[1].xyz, _blobs[1].w);
return opSmoothUnion(Sphere1, Sphere2, 1);
}
And it doesn't matter how I define this array:
-uniform float4[] _blobs;
-uniform float4[2] _blobs;
-uniform float4 _blobs[];
-uniform float4 _blobs[2];
And I have already seen this tutorial.
In C# when setting Vector array you have to set array that is already initialized to its desired length:
public Vector4[] _blobs = new Vector4[2];
material.SetVectorArray("_blobs", _blobs);
according to Step 3. Limitations in Alan Zucconi's tutorial
Nothing - no matter if I set Vector4 in the inspector or in the script itself. I have probably tried all possible combinations. Due to the fact that C# refers to a variable in Shader using a string, propably I'm gonna hard-encode 500 individual variables and use something like this:
for(int i=0 ; i < _blobs.Length ; i++){
material.SetVector("_sphere"+i, _blobs[i]);
}
This is probably the worst way to do it, but I'm running out of patience and ideas on how to do it... I've never had such a problem with arrays...
Sometimes when I set uninitialized array or wanted to make array longer, after changes in script I had to restart whole Unity for it to properly assigned my changes.
I restarted it few times and still nothing. It can also be a problem in my shader but I don't think so because if I manually set up to 20 vectors4 it works fine. As far as I know, debugging shader doesn't work the same way as in the case of an ordinary script - Debug.Log showed that Vector4[] which is sent to the shader is correct (regardless of whether the data is set in the script or inspector and whether I am in play/edit mod). I don't know if the shader receives these data correctly and what they actually look like. float4 works, and float4[] does not - the editor does not show any errors and I have no way of checking what the variables in the shader look like.
Could you post your shader and c# script to see if there isn't any mistake?
Got it! This s**t causes all the problems:
// Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers d3d11 gles
How did it appear in my code?
Now it is working!
$$anonymous$$ichaI, my master, thank you very much for your help :)
Your answer

Follow this Question
Related Questions
Split a number? 1 Answer
Pick between two floats 2 Answers
How to use array in cg shader 1 Answer
NullRefenceException Using Arrays 2 Answers
Why cant I do this? Incorrect number of values(terrain and array related). 1 Answer