Exemplo n.º 1
0
        public void Execute(int index)
        {
            int  position  = surfaceVoxelsIndex[index];
            int3 position3 = VoxelUtility.Index1ToIndex3(position);

            Debug.Assert(surfaceVoxels.TryGetValue(position, out bool v));

            int noNeighborCount_Debug = 0;

            int3[] neighborPosition3 = new int3[] {
                position3 + new int3(1, 0, 0),
                position3 + new int3(-1, 0, 0),
                position3 + new int3(0, 1, 0),
                position3 + new int3(0, -1, 0),
                position3 + new int3(0, 0, 1),
                position3 + new int3(0, 0, -1)
            };

            foreach (var neighborPos in neighborPosition3)
            {
                if (!surfaceVoxels.TryGetValue(VoxelUtility.Index3ToIndex1(neighborPos), out bool v1))
                {
                    int surface = VoxelUtility.GetSurfaceIndex(position3, neighborPos);
                    surfaceFaces.TryAdd(surface, true);
                    noNeighborCount_Debug++;
                }
            }

            if (noNeighborCount_Debug == 6)
            {
                Debug.LogError("No neighboring voxel for " + position3);
            }
        }
Exemplo n.º 2
0
        public void Execute(int index)
        {
            int xGrid = index / totalGridCount.y; //[0,totalGridCount.x)
            int yGrid = index % totalGridCount.y;

            bool inside = false;

            for (int z = 0; z != totalGridCount.z - 1; ++z)
            {
                int faceIndex = VoxelUtility.GetSurfaceIndex(new int3(xGrid, yGrid, z), new int3(xGrid, yGrid, z + 1));
                if (surfaceFaces.TryGetValue(faceIndex, out bool v))
                {
                    inside = !inside;
                }
                if (inside)
                {
                    volumeVoxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(xGrid, yGrid, z + 1)), true);
                }
            }
        }
Exemplo n.º 3
0
    void RemoveOneConsecutiveSurface(ref NativeHashMap <int, bool> hashmap)
    {
        NativeHashMap <int, bool> removedSurface = new NativeHashMap <int, bool>(hashmap.Capacity, Allocator.TempJob);

        var keys   = hashmap.GetKeyArray(Allocator.TempJob);
        int index1 = keys[0];

        keys.Dispose();

        Queue <int> toBeRemoved = new Queue <int>();

        toBeRemoved.Enqueue(index1);
        //Debug.Log("Removal starts from " + VoxelUtility.getFacePosition(index1));

        while (toBeRemoved.Count > 0)
        {
            int workingIndex = toBeRemoved.Dequeue();
            hashmap.Remove(workingIndex);
            removedSurface.TryAdd(workingIndex, true);
            int3 voxel1, voxel2;
            (voxel1, voxel2) = VoxelUtility.SurfaceIndexToVoxels(workingIndex);

            int3[] neighborInc = new int3[4];
            if (voxel1.x != voxel2.x)
            {
                neighborInc[0] = new int3(0, 1, 0);
                neighborInc[1] = new int3(0, -1, 0);
                neighborInc[2] = new int3(0, 0, 1);
                neighborInc[3] = new int3(0, 0, -1);
            }
            if (voxel1.y != voxel2.y)
            {
                neighborInc[0] = new int3(1, 0, 0);
                neighborInc[1] = new int3(-1, 0, 0);
                neighborInc[2] = new int3(0, 0, 1);
                neighborInc[3] = new int3(0, 0, -1);
            }
            if (voxel1.z != voxel2.z)
            {
                neighborInc[0] = new int3(1, 0, 0);
                neighborInc[1] = new int3(-1, 0, 0);
                neighborInc[2] = new int3(0, 1, 0);
                neighborInc[3] = new int3(0, -1, 0);
            }
            foreach (int3 voxel in new int3[] { voxel1, voxel2 })
            {
                foreach (int3 inc in neighborInc)
                {
                    int3 candidateNeighbor = voxel + inc;
                    int  candidateFace     = VoxelUtility.GetSurfaceIndex(voxel, candidateNeighbor);
                    if (hashmap.TryGetValue(candidateFace, out bool v))
                    {
                        if (v)
                        {
                            // only enqueue newly discovered neighbor
                            toBeRemoved.Enqueue(candidateFace);

                            // set face's flag to False: it has already be put into queue
                            hashmap.Remove(candidateFace);
                            hashmap.TryAdd(candidateFace, false);
                        }
                    }
                }
            }
            foreach (int3 inc in neighborInc)
            {
                int3 voxel1Offseted = voxel1 + inc;
                int3 voxel2Offseted = voxel2 + inc;
                int  candidateFace  = VoxelUtility.GetSurfaceIndex(voxel1Offseted, voxel2Offseted);
                if (hashmap.TryGetValue(candidateFace, out bool v))
                {
                    if (v)
                    {
                        // only enqueue newly discovered neighbor
                        toBeRemoved.Enqueue(candidateFace);

                        // set face's flag to False: it has already be put into queue
                        hashmap.Remove(candidateFace);
                        hashmap.TryAdd(candidateFace, false);
                    }
                }
            }
        }
        if (hashmap.Length < removedSurface.Length)
        {
            hashmap.Dispose();
            hashmap = removedSurface;
        }
        else
        {
            removedSurface.Dispose();
        }
    }