I'm having a RWStructuredBuffer
called AffectedVertices give me random data instead of 1 simple value to resultAffectedVertices
, when i get i run SurfaceNets()
a lot by holding space.
The problem lies with my chunkGenerator compute shader's kernel, which, generate the base data into vertices
for the other kernels that only read from vertices
buffer.
this problem disappears if i add a breakpoint before and after running SurfaceNets()
in order to have it only run 1 by 1
This is my code: Chunk.compute
#include "simplex/noiseSimplex.cginc"
int vertMultiplier;
float element;
int octaveNumber;
float noiseFrequency;
float noiseAmplitude;
float noisePersistence;
float lac;
#pragma kernel CSMain
RWStructuredBuffer<float4> vertices;
float4 chunkCenterPos;
float3 sphereCenter;
float sphereRadius;
RWStructuredBuffer<float3> AffectedVertices;
float sNoiseLayered(float3 pos, int numL, float amp, float freq, float prs, float lac)
{
float noiseSum = 0;
for (int i = 0; i < numL; i ++)
{
noiseSum += snoise(pos * freq) * amp;
amp *= prs;
freq *= lac;
}
return noiseSum;
}
[numthreads(1,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
vertices[id.x + vertMultiplier* id.y + vertMultiplier*vertMultiplier*id.z] = float4(id.x,id.y,id.z,sNoiseLayered(float3(id.x + chunkCenterPos.x,id.y + chunkCenterPos.y,id.z + chunkCenterPos.z),octaveNumber,noiseAmplitude,noiseFrequency,noisePersistence,lac) ) ;//distance(id, float3(4.,4,4)) - 3.8 );
if (id.x == 1 && id.z == 1 && id.y == 1)
{
AffectedVertices[AffectedVertices.IncrementCounter()] = id.xyz;
vertices[id.x + vertMultiplier * id.y + vertMultiplier * vertMultiplier * id.z].w = distance(id, sphereCenter - chunkCenterPos) - sphereRadius;
}
}
void SurfaceNetting()
{
meshCollider = GetComponent<MeshCollider>();
chunkGenerator.SetInt("vertMultiplier", vertMultiplier);
NetsSampler.SetInt("vertMultiplier", vertMultiplier);
SurfaceNetter.SetInt("vertMultiplier", vertMultiplier);
SNTriangler.SetInt("vertMultiplier", vertMultiplier);
chunkGenerator.SetInt("octaveNumber", octaveNumber);
chunkGenerator.SetFloat("noiseFrequency", noiseFrequency);
chunkGenerator.SetFloat("noiseAmplitude", noiseAmplitude);
chunkGenerator.SetFloat("noisePersistence", noisePersistence);
chunkGenerator.SetFloat("lac", lac);
chunkVertices = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(float) * 4);
AffectedVertices = new ComputeBuffer((vertMultiplier) * (vertMultiplier) * (vertMultiplier), sizeof(float) * 3, ComputeBufferType.Counter);
netsX = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(float) * 3);
netsY = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(float) * 3);
netsZ = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(float) * 3);
XTriangleFaces = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(uint));
YTriangleFaces = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(uint));
ZTriangleFaces = new ComputeBuffer(vertMultiplier * vertMultiplier * vertMultiplier, sizeof(uint));
FinalVertices = new ComputeBuffer((vertMultiplier - 1) * (vertMultiplier - 1) * (vertMultiplier - 1), sizeof(float) * 3, ComputeBufferType.Counter);
vertMapper = new ComputeBuffer((vertMultiplier - 1) * (vertMultiplier - 1) * (vertMultiplier - 1), sizeof(uint));
FinalTriangles = new ComputeBuffer(21750, sizeof(int) * 3, ComputeBufferType.Counter);
netsX.SetCounterValue(0);
netsY.SetCounterValue(0);
netsZ.SetCounterValue(0);
XTriangleFaces.SetCounterValue(0);
YTriangleFaces.SetCounterValue(0);
ZTriangleFaces.SetCounterValue(0);
FinalVertices.SetCounterValue(0);
AffectedVertices.SetCounterValue(0);
vertMapper.SetCounterValue(0);
FinalTriangles.SetCounterValue(0);
chunkGenerator.SetBuffer(0, "AffectedVertices", AffectedVertices);
chunkGenerator.SetBuffer(0, "vertices", chunkVertices);
chunkGenerator.SetFloats( "chunkCenterPos", transform.position.x, transform.position.y, transform.position.z);
chunkGenerator.Dispatch(0, vertMultiplier, vertMultiplier, vertMultiplier);
//actually when i check in unity i find 512 vertices on the mesh, but when i check ther data inside the buffer i find
NetsSampler.SetBuffer(0, "netsX", netsX);
NetsSampler.SetBuffer(1, "netsZ", netsZ);
NetsSampler.SetBuffer(2, "netsY", netsY);
NetsSampler.SetBuffer(0, "vertices", chunkVertices);
NetsSampler.SetBuffer(1, "vertices", chunkVertices);
NetsSampler.SetBuffer(2, "vertices", chunkVertices);
NetsSampler.SetBuffer(0, "XTriangleFaces", XTriangleFaces);
NetsSampler.SetBuffer(1, "ZTriangleFaces", ZTriangleFaces);
NetsSampler.SetBuffer(2, "YTriangleFaces", YTriangleFaces);
NetsSampler.Dispatch(0, vertMultiplier, vertMultiplier, vertMultiplier);
NetsSampler.Dispatch(1, vertMultiplier, vertMultiplier, vertMultiplier);
NetsSampler.Dispatch(2, vertMultiplier, vertMultiplier, vertMultiplier);
SurfaceNetter.SetBuffer(0, "FinalVertices", FinalVertices);
SurfaceNetter.SetBuffer(0, "netsX", netsX);
SurfaceNetter.SetBuffer(0, "netsY", netsY);
SurfaceNetter.SetBuffer(0, "netsZ", netsZ);
SurfaceNetter.SetBuffer(0, "vertMapper", vertMapper);
SurfaceNetter.SetBuffer(0, "XTriangleFaces", XTriangleFaces);
SurfaceNetter.SetBuffer(0, "ZTriangleFaces", ZTriangleFaces);
SurfaceNetter.SetBuffer(0, "YTriangleFaces", YTriangleFaces);
SurfaceNetter.Dispatch(0, (vertMultiplier - 1), (vertMultiplier - 1), (vertMultiplier - 1));
SNTriangler.SetBuffer(0, "vertices", chunkVertices);
SNTriangler.SetBuffer(0, "XTriangleFaces", XTriangleFaces);
SNTriangler.SetBuffer(0, "ZTriangleFaces", ZTriangleFaces);
SNTriangler.SetBuffer(0, "YTriangleFaces", YTriangleFaces);
SNTriangler.SetBuffer(0, "FinalVertices", FinalVertices);
SNTriangler.SetBuffer(0, "FinalTriangles", FinalTriangles);
SNTriangler.SetBuffer(0, "vertMapper", vertMapper);
//*
SNTriangler.Dispatch(0, vertMultiplier, vertMultiplier, vertMultiplier);
c = new Vector4[chunkVertices.count];
chunkVertices.GetData(c);
resultAffectedVertices = new Vector3[chunkVertices.count];
AffectedVertices.GetData(resultAffectedVertices);
//*/
vertices = new Vector3[FinalVertices.count];
FinalVertices.GetData(vertices);
List<Vector3> vertices3 = new List<Vector3>();
bool poi = false;
for (int i = 0; i < vertices.Length; i++)
if (vertices[i] != Vector3.zero ) { vertices3.Add(vertices[i]); }
else if (!poi) { vertices3.Add(vertices[i]); }
Vector3[] vertices2 = new Vector3[vertices3.Count];
vertices2 = vertices3.ToArray();
tfx = new uint[XTriangleFaces.count];
tfy = new uint[YTriangleFaces.count];
tfz = new uint[ZTriangleFaces.count];
/*XTriangleFaces.GetData(tfx);
YTriangleFaces.GetData(tfy);
ZTriangleFaces.GetData(tfz);*/
triangles = new int[FinalTriangles.count];
FinalTriangles.GetData(triangles);
List<int> triangles3 = new List<int>();
for (int i = 0; i < triangles.Length - 3; i += 3)
if (triangles[i] == 0 && triangles[i + 1] == 0 && triangles[i + 2] == 0) { }
else {triangles3.Add(triangles[i]); triangles3.Add(triangles[i+1]); triangles3.Add(triangles[i+2]); }
int[] triangles2 = new int[triangles3.Count];
triangles2 = triangles3.ToArray();
NX = new Vector3[netsX.count];
NY = new Vector3[netsY.count];
NZ = new Vector3[netsZ.count];
/*netsX.GetData(NX);
netsY.GetData(NY);
netsZ.GetData(NZ);*/
//Debug.Log(vertices[2]);
mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices2;
mesh.triangles = triangles2;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
meshCollider.sharedMesh = mesh;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
//meshCollider.convex = true;
chunkVertices.Release();
netsX.Release();
netsY.Release();
netsZ.Release();
FinalVertices.Release();
FinalTriangles.Release();
XTriangleFaces.Release();
YTriangleFaces.Release();
ZTriangleFaces.Release();
vertMapper.Release();
AffectedVertices.Release();
}
```