So, I have an array in shader:
float3 _PointPositions [10];
I’m tryng to loop through it and set each element with:
material.SetVector("_PointPositions"+i.ToString(), value);
It doesn’t seem to work tho and only mentions of this method I found are ~6 years old.
Note: I know of SetVectorArray, but the input array isn’t always the same size, so it cannot use it as it wants to restart editor every time different size of array is passed into it.
If you want to use arrays, they have to be static size. So you need to use SetVectorArray and fill rest with zeros. If you want to only fill specific parts of the array, you should use buffers instead and then SetBuffer method.
You would need something like this for buffers:
StructuredBuffer<float3> _PointPositions;
and in C# you would use this method:
SetData(Array data, int managedBufferStartIndex, int computeBufferStartIndex, int count);
it should look like this:
var dataArray = new Vector3[numberOfPoints];
positionBuffer = new ComputeBuffer(numberOfPoints, sizeof(float) * 3, ComputeBufferType.Default);
positionBuffer.SetData(dataArray, dataArrayOffset, bufferOffset, numberOfValuesToFill);
material.SetBuffer("_PointPositions", positionBuffer);
So dataArray is your array of Vec3s, dataArrayOffset is integer that determines the start and numberOfValuesToFill determines the end of vector values from array to pass. bufferOffset should be the same as dataArrayOffset if buffer in shader and array in C# are the same size.
It’s a rough code, but this should give you some links to look for buffer stuff. There are StructuredBuffers, RWStructuredBuffers for different Read/Write purposes, so you can read about them as well.