예제 #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);
            }
        }
예제 #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);
                }
            }
        }
예제 #3
0
        private void coverVoxelsWithTriangleEdge(float3 A, float3 B)
        {
            int3   aVoxelIndex  = VoxelUtility.PosToIndex3(A);
            int3   bVoxelIndex  = VoxelUtility.PosToIndex3(B);
            int    aVoxelIndex1 = VoxelUtility.Index3ToIndex1(aVoxelIndex);
            int    bVoxeklndex1 = VoxelUtility.Index3ToIndex1(bVoxelIndex);
            float3 aVoxelCenter = VoxelUtility.Index3ToPos(aVoxelIndex);
            float3 bVoxelCenter = VoxelUtility.Index3ToPos(bVoxelIndex);

            if (aVoxelIndex.x == bVoxelIndex.x && aVoxelIndex.y == bVoxelIndex.y ||
                aVoxelIndex.x == bVoxelIndex.x && aVoxelIndex.z == bVoxelIndex.z ||
                aVoxelIndex.y == bVoxelIndex.y && aVoxelIndex.z == bVoxelIndex.z)
            {
                /*Same Grid or 6-neighbor*/
                voxels.TryAdd(aVoxelIndex1, true);
                voxels.TryAdd(bVoxeklndex1, true);
                return;
            }

            if (aVoxelIndex.x == bVoxelIndex.x)
            {
                /*yz plane neighbor */
                float2 xy = LinePlaneIntersect(A, B, 'z', 0.5f * (aVoxelCenter.z + bVoxelCenter.z));
                float  x  = xy.x;
                float  y  = xy.y;

                //if (!(A.x <= x && x <= B.x || B.x <= x && x <= A.x)){
                //    Debug.Log(A + "<>" + B + "<>" + aVoxelCenter + "<>" + bVoxelCenter + " -> " + x);
                //}
                //Debug.Assert(A.x <= x && x <= B.x || B.x <= x && x <= A.x);
                int connector;
                if (math.abs(y - A.y) < math.abs(y - B.y))
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, aVoxelIndex.y, bVoxelIndex.z));
                }
                else
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, bVoxelIndex.y, aVoxelIndex.z));
                }
                voxels.TryAdd(connector, true);
                return;
            }

            if (aVoxelIndex.y == bVoxelIndex.y)
            {
                /*xz plane neighbor */
                float2 yz = LinePlaneIntersect(A, B, 'x', 0.5f * (aVoxelCenter.x + bVoxelCenter.x));
                float  y  = yz.x;
                float  z  = yz.y;
                //Debug.Assert(A.y <= y && y <= B.y || B.y <= y && y <= A.y);
                int connector;
                if (math.abs(z - A.z) < math.abs(z - B.z))
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(bVoxelIndex.x, aVoxelIndex.y, aVoxelIndex.z));
                }
                else
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, aVoxelIndex.y, bVoxelIndex.z));
                }
                voxels.TryAdd(connector, true);
                return;
            }

            if (aVoxelIndex.z == bVoxelIndex.z)
            {
                /*xy plane neighbor */
                float2 xz = LinePlaneIntersect(A, B, 'y', 0.5f * (aVoxelCenter.y + bVoxelCenter.y));
                float  x  = xz.x;
                float  z  = xz.y;
                //Debug.Assert(A.z <= z && z <= B.z || B.z <= z && z <= A.z);
                int connector;
                if (math.abs(x - A.x) < math.abs(x - B.x))
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, bVoxelIndex.y, aVoxelIndex.z));
                }
                else
                {
                    connector = VoxelUtility.Index3ToIndex1(new int3(bVoxelIndex.x, aVoxelIndex.y, aVoxelIndex.z));
                }
                voxels.TryAdd(connector, true);
                return;
            }

            /*3D diagonal */
            float2 _yz = LinePlaneIntersect(A, B, 'x', 0.5f * (aVoxelCenter.x + bVoxelCenter.x));

            if (RectContains(new float2(aVoxelCenter.y, aVoxelCenter.z), _yz))
            {
                /*baa */
                voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(bVoxelIndex.x, aVoxelIndex.y, aVoxelIndex.z)), true);
            }
            else
            {
                if (RectContains(new float2(bVoxelCenter.y, bVoxelCenter.z), _yz))
                {
                    /*abb */
                    voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, bVoxelIndex.y, bVoxelIndex.z)), true);
                }
            }

            float2 _xz = LinePlaneIntersect(A, B, 'y', 0.5f * (aVoxelCenter.y + bVoxelCenter.y));

            if (RectContains(new float2(aVoxelCenter.x, aVoxelCenter.z), _xz))
            {
                /*aba */
                voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, bVoxelIndex.y, aVoxelIndex.z)), true);
            }
            else
            {
                if (RectContains(new float2(bVoxelCenter.x, bVoxelCenter.z), _xz))
                {
                    /*bab */
                    voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(bVoxelIndex.x, aVoxelIndex.y, bVoxelIndex.z)), true);
                }
            }

            float2 _xy = LinePlaneIntersect(A, B, 'z', 0.5f * (aVoxelCenter.z + bVoxelCenter.z));

            if (RectContains(new float2(aVoxelCenter.x, aVoxelCenter.y), _xy))
            {
                /*aab */
                voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(aVoxelIndex.x, aVoxelIndex.y, bVoxelIndex.z)), true);
            }
            else
            {
                if (RectContains(new float2(bVoxelCenter.x, bVoxelCenter.y), _xy))
                {
                    /*bba */
                    voxels.TryAdd(VoxelUtility.Index3ToIndex1(new int3(bVoxelIndex.x, bVoxelIndex.y, aVoxelIndex.z)), true);
                }
            }
        }