public void DebugByDebuger() { for (int i = 0; i < pathNodes.Count; i++) { Debuger_K.AddDot(pathNodes[i], Color.red); Debuger_K.AddLabelFormat(pathNodes[i], "{0} {1}", i, pathNodes[i]); } for (int i = 0; i < pathNodes.Count - 1; i++) { Debuger_K.AddLine(pathNodes[i], pathNodes[i + 1], Color.red); } }
public CSRasterization2DResult Rasterize(Vector3[] verts, int[] tris, int sizeX, int sizeZ, float chunkPosX, float chunkPosZ, float voxelSize, float maxSlopeCos, bool debug = false) { if (tris.Length == 0) { return(null); } int dsLength = tris.Length / 3; int dsTargetLength = Mathf.Max(Mathf.CeilToInt((float)dsLength / (float)cellSizeX), 1) * cellSizeX; DataSegment2D[] ds = new DataSegment2D[dsTargetLength]; int sizeTotal = sizeX * sizeZ; int offsetX = Mathf.RoundToInt(chunkPosX / voxelSize); int offsetZ = Mathf.RoundToInt(chunkPosZ / voxelSize); for (int i = 0; i < tris.Length; i += 3) { Vector3 A = verts[tris[i]]; Vector3 B = verts[tris[i + 1]]; Vector3 C = verts[tris[i + 2]]; //making data segments //data segment represent: //triangle index //expected passablity based on inclanation //min and max indexes in grid to check it presence ds[i / 3] = new DataSegment2D( i, (CalculateWalk(A, B, C, maxSlopeCos) ? 3 : 1), //if true then walkable else slope; Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX), //minX Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX), //maxX Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ), //minZ Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ) //maxZ ); } int kernel = CS.FindKernel("Rasterize"); ComputeBuffer vertsBuffer = new ComputeBuffer(verts.Length, sizeof(float) * 3); ComputeBuffer trisBuffer = new ComputeBuffer(tris.Length, sizeof(int)); ComputeBuffer voxelBuffer = new ComputeBuffer(sizeTotal, Voxel2D.stride); ComputeBuffer dataSegmentBuffer = new ComputeBuffer(dsTargetLength, DataSegment2D.stride); CS.SetInt("SizeX", sizeX); CS.SetInt("SizeZ", sizeZ); CS.SetVector("ChunkPos", new Vector4(chunkPosX, 0, chunkPosZ, 0)); CS.SetFloat("VoxelSize", voxelSize); CS.SetBuffer(kernel, "CurTris", trisBuffer); CS.SetBuffer(kernel, "CurVerts", vertsBuffer); CS.SetBuffer(kernel, "Result", voxelBuffer); CS.SetBuffer(kernel, "TargetSegments", dataSegmentBuffer); vertsBuffer.SetData(verts); trisBuffer.SetData(tris); Voxel2D[] voxels = new Voxel2D[sizeTotal]; for (int i = 0; i < sizeTotal; i++) { voxels[i].passability = -1; } voxelBuffer.SetData(voxels); dataSegmentBuffer.SetData(ds); CS.Dispatch(kernel, dsTargetLength / cellSizeX, 1, 1); voxelBuffer.GetData(voxels); CSRasterization2DResult result = new CSRasterization2DResult(voxels); //debug #if UNITY_EDITOR if (debug) { Debuger_K.AddMesh(verts, tris, new Color(1, 0, 1, 0.1f)); //implementation of things goint on in compute shader foreach (var dataSet in ds) { Vector3 A = verts[tris[dataSet.index]]; Vector3 B = verts[tris[dataSet.index + 1]]; Vector3 C = verts[tris[dataSet.index + 2]]; for (int x = dataSet.minX; x < dataSet.maxX; x++) { for (int z = dataSet.minZ; z < dataSet.maxZ; z++) { float pointX = (x * voxelSize) + chunkPosX; float pointZ = (z * voxelSize) + chunkPosZ; //if (SomeMath.LineSide(B.x, B.z, A.x, A.z, pointX, pointZ) <= 0.001 & // SomeMath.LineSide(A.x, A.z, C.x, C.z, pointX, pointZ) <= 0.001 & // SomeMath.LineSide(C.x, C.z, B.x, B.z, pointX, pointZ) <= 0.001) { // float height = SomeMath.CalculateHeight(A, B, C, pointX, pointZ); // Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, height, (z * voxelSize) + chunkPosZ)); // Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); // Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); // Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); //} //else { // Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); // Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); // Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); //} if (SomeMath.PointInTriangleSimple(A, B, C, pointX, pointZ)) { float height = SomeMath.CalculateHeight(A, B, C, pointX, pointZ); Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, height, (z * voxelSize) + chunkPosZ)); //Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); //Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); //Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green); } else { //Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); //Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); //Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red); } } } } //for (int x = 0; x < sizeX; x++) { // for (int z = 0; z < sizeZ; z++) { // var curVoxel = voxels[x + (z * sizeX)]; // if (curVoxel.exist) { // Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, curVoxel.height, (z * voxelSize) + chunkPosZ)); // } // } //} } #endif vertsBuffer.Dispose(); trisBuffer.Dispose(); voxelBuffer.Dispose(); dataSegmentBuffer.Dispose(); return(result); }
public void DebugMe(bool drawMax = true, bool drawMin = true, bool drawMaxMinSize = true) { Vector3 realChunkPos = template.realOffsetedPosition; Vector3 offset = template.halfVoxelOffset; for (int x = 0; x < sizeX; x++) { for (int z = 0; z < sizeZ; z++) { int gen = 0; int index = (z * sizeX) + x; var current = arrayData[index]; if (current.next == -2) { continue; } while (true) { Vector3 p1 = realChunkPos + offset + new Vector3((x * template.voxelSize), current.max, (z * template.voxelSize)); Vector3 p2 = realChunkPos + offset + new Vector3((x * template.voxelSize), current.min, (z * template.voxelSize)); Color dColor; switch (current.pass) { case -1: dColor = Color.black; break; case 0: dColor = Color.red; break; case 1: dColor = Color.magenta; break; case 2: dColor = new Color(0f, 0.5f, 0f, 1f); break; case 3: dColor = Color.green; break; default: dColor = Color.white; break; } //Debuger_K.AddLabelFormat(p2 + (new Vector3(0.005f, 0.005f, 0.005f) * gen), "{0}", gen); //Debuger_K.AddLabelFormat(p2 + (new Vector3(0.005f, 0.005f, 0.005f) * gen), "{0}\n{1}\n{2}", gen, curVal.max, curVal.min); if (drawMax) { Debuger_K.AddDot(p1, dColor, 0.02f); } if (drawMin) { Debuger_K.AddDot(p2, Color.gray, 0.02f); } if (drawMaxMinSize) { Debuger_K.AddLine(p1, p2); } gen++; if (current.next == -1) { break; } else { current = arrayData[current.next]; } } } } }