Hi all,
I am using a computeshader to populate a unityterrain with trees. However, I am facing issues with the AppendStructuredBuffer. What I want to do, is let a probability check determine if a tree should be spawned or not. If not, then I return, otherwise I append to the buffer.
If I don’t filter (i.e. probability is 100%), everything goes well, all trees look correct. However, if I filter, I get artifacts (in this case on the height). Upon closer inspection it seems that the count reflects the maximum amount of elements I expect (allocated in the C# ComputeBuffer), however this should not be the case…
I use the SetCounterValue(0) to properly set the counter for the buffer.
- Am I wrong to assume that the count of the ComputeBuffer should be the amount of appended elements instead of the total allocated elements?
- If so, how can I find the latter?
- If I am correct, does anyone see why I would get the incorrect count?
Compute Shader:
AppendStructuredBuffer Result;
struct TreeInstance
{
float3 position;
float widthScale;
float heightScale;
float rotation;
int color;
int lightmapColor;
int prototypeIndex;
float temporaryDistance;
};
[NUMTHREADS]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// random function is functioning as it should.
if (random(id.xy) > globalProbability)
{ return; }
TreeInstance tree;
// some code that initializes the TreeInstance values.
Result.Append(tree);
}
relevant C# code:
ComputeBuffer treeInstances =
new ComputeBuffer(total, Marshal.SizeOf<TreeInstance>(), ComputeBufferType.Append);
treeInstances.SetCounterValue(0);
treeInstances.GetData(someInitializedArray,0,0,treeInstances.count);
treeInstances.Release();
Thanks for your time