public void ComputeNoise(Octree node) { int size = (int)node.bounds.size.x; //size = 2; NativeArray <float4> result = new NativeArray <float4>(size * size * size, Allocator.TempJob); NoiseJob jobData = new NoiseJob(); jobData.size = size; jobData.noiseScale = 10f; jobData.minCorner = node.bounds.min; jobData.noiseData = result; JobHandle handle = jobData.Schedule(); handle.Complete(); //Debug.Log(string.Join(",", result.ToArray())); Debug.Log(result.Length); result.Dispose(); }
/// <summary> /// Subdivde a node into 8 octants /// </summary> public void Subdivide() { if (children == null) { children = new Octree[8]; } float3 localCenter; for (int i = 0; i < 8; i++) { localCenter.x = ((i & (1 << 0)) >> 0 == 0 ? -1 : 1); localCenter.y = ((i & (1 << 1)) >> 1 == 0 ? -1 : 1); localCenter.z = ((i & (1 << 2)) >> 2 == 0 ? -1 : 1); if (children[i] == null) { children[i] = new Octree(chunk.size / 2, localCenter * chunk.size / 4 + chunk.center); children[i].level = this.level - 1; children[i].parent = this; } } }
public Mesh[] MarchChunk(object state) { Mesh[] result = new Mesh[2]; Octree node = (Octree)state; //Debug.Log("MARCH: " + node.center); //Debug.Log("MARCH1: " + node.center); int id = Thread.CurrentThread.ManagedThreadId % THREAD_MAX; //Debug.Log("MARCH: " + id); ComputeBuffer noiseDataBuffer = noiseBuffs[id]; ComputeBuffer trianglesBuffer = trigBuffs[id]; ComputeBuffer PPTrianglesBuffer = trigBuffs_PP[id]; ComputeBuffer counterBuffer = countBuffs[id]; int[] bufferCount = bufferCounts[id]; //Debug.Log("MARCH2: " + node.center); perlin_shader.SetBuffer(PNkernel, "output", noiseDataBuffer); //Debug.Log("MARCH3: " + node.center); perlin_shader.SetVector("minCorner", new float4(node.bounds.min, 0)); perlin_shader.SetInt("sampleSize", CHUNK_SIZE + 1); perlin_shader.SetInt("scale", node.level); //Debug.Log("MARCH4: " + node.center); int groupSize = Chunk.CHUNK_SIZE / 8; //Debug.Log("MARCH5: " + node.center); perlin_shader.Dispatch(PNkernel, groupSize + 1, groupSize + 1, groupSize + 1); //data = new float4[(size + 1) * (size + 1) * (size + 1)]; //perlinBuffer.GetData(data); //this.hasMesh = HasMesh(); //AsyncGPUReadbackRequest PVBrequest = AsyncGPUReadback.Request(noiseDataBuffer); //AsyncGPUReadbackRequest PPPVBrequest = AsyncGPUReadback.Request(PPNoiseDataBuffer); //StartCoroutine(GetNoiseDataCoroutine(PVBrequest, PPPVBrequest, node.chunk)); Chunk chunk = node.chunk; //while (!PVBrequest.done || !PPPVBrequest.done) continue; //if (PVBrequest.hasError || PPPVBrequest.hasError) { yield break}; //float4[] data = new float4[(CHUNK_SIZE + 1) * (CHUNK_SIZE + 1) * (CHUNK_SIZE + 1)]; //noiseDataBuffer.GetData(data); //Debug.Log(data[0] + " " + data[data.Length - 1]); ////chunk.PPData = PPPVBrequest.GetData<float4>(); //bool hasMesh = false; ////if (chunk.center.x == -128f && chunk.center.y == -128f && chunk.center.z == -128f) { // float4[] d = data; // for (int i = 0; i < d.Length; i++) { // if (!(d[i].w > 0 && d[0].w > 0 || (d[i].w <= 0 && d[0].w <= 0))) { // hasMesh = true; // break; // } // } // Debug.Log(hasMesh); ////} ////Debug.Log(result.Length + " " + chunk.data.Value.Length); //chunk.hasMesh = hasMesh; //chunk.data = data; //if (!hasMesh) yield break; //if (((Vector3)chunk.center + Vector3.one * chunk.size / 2).y < chunk.data.Value[0].y) Debug.LogError("Data out of NODE"); //if (((Vector3)chunk.center + Vector3.one * chunk.size / 2).y < chunk.PPData.Value[0].y) Debug.LogError("PPData out of NODE"); //Debug.Log("MARCH " + chunk.center); trianglesBuffer.SetCounterValue(0); PPTrianglesBuffer.SetCounterValue(0); mc_shader.SetBuffer(MCkernel, "dataBuffer", noiseDataBuffer); mc_shader.SetBuffer(MCkernel, "trianglesBuffer", trianglesBuffer); mc_shader.SetBuffer(MCkernel, "PPTrianglesBuffer", PPTrianglesBuffer); mc_shader.SetFloat("isolevel", MarchingCube.isolevel); mc_shader.SetInt("dataSize", CHUNK_SIZE + 1); //MCShader.SetInt("scale", node.level + 1); mc_shader.Dispatch(MCkernel, groupSize, groupSize, groupSize); ComputeBuffer.CopyCount(trianglesBuffer, counterBuffer, 0); counterBuffer.GetData(bufferCount); Triangle[] triangles = new Triangle[bufferCount[0]]; trianglesBuffer.GetData(triangles); ComputeBuffer.CopyCount(PPTrianglesBuffer, counterBuffer, 0); counterBuffer.GetData(bufferCount); Triangle[] PPtriangles = new Triangle[bufferCount[0]]; PPTrianglesBuffer.GetData(PPtriangles); result[0] = GenMesh(triangles); if (chunk.mesh != null && ((Vector3)chunk.center + Vector3.one * chunk.size / 2).y < chunk.mesh.vertices[0].y) { Debug.LogError("Mesh out of NODE" + chunk.center + " " + node.level); } result[1] = GenMesh(PPtriangles); if (result[1] == null && result[0] != null || result[0] == null && result[1] != null) { Debug.LogError("!!!!!!!!!!!!!!!!!!!"); } if (result[0] != null && result[1] != null) { chunk.hasMesh = true; } chunk.calculated = true; //Debug.Log("MARCH: " + node.center); return(result); }