Esempio n. 1
0
    public VoxelPoint GetVoxelPoint(int x, int y, int z)
    {
        Chunk chunk = GetChunk(x, y, z);

        VoxelPoint p = chunk.GetVoxelPoint(x.Mod(chunkSize),
                                           y.Mod(chunkSize),
                                           z.Mod(chunkSize));

        return(p);
    }
Esempio n. 2
0
    private VoxelPoint[] GetVoxelPoints(int x, int y, int z, VoxelPoint[,,] points)
    {
        for (int i = 0; i < 8; i++)
        {
            VoxelPoint p = points[x + CubePointsX[i], y + CubePointsY[i], z + CubePointsZ[i]];
            _initPoints[i] = p;
        }

        return(_initPoints);
    }
Esempio n. 3
0
        public static void GenerateMesh(VoxelGrid voxels, Action <ushort, VoxelSide, VoxelSide, Quad, Vector2i> quadProcessor)
        {
            var stopwatch = Stopwatch.StartNew();
            var plane     = new VoxelPoint[voxels.GridSize, voxels.GridSize];

            MeshPosZ(voxels, plane, quadProcessor);
            MeshNegZ(voxels, plane, quadProcessor);
            MeshPosX(voxels, plane, quadProcessor);
            MeshNegX(voxels, plane, quadProcessor);
            MeshPosY(voxels, plane, quadProcessor);
            MeshNegY(voxels, plane, quadProcessor);
            Times.Add(stopwatch.Elapsed.TotalMilliseconds);
        }
Esempio n. 4
0
    private Vector3[] GenerateVertexList(VoxelPoint[] points, int edgeIndex)
    {
        for (int i = 0; i < 12; i++)
        {
            if ((edgeIndex & (1 << i)) != 0)
            {
                int[] edgePair = LookupTables.EdgeIndexTable[i];
                int   edge1    = edgePair[0];
                int   edge2    = edgePair[1];

                VoxelPoint point1 = points[edge1];
                VoxelPoint point2 = points[edge2];

                _vertexList[i] = VertexInterpolate(point1.localPosition, point2.localPosition, point1.density, point2.density);
            }
        }

        return(_vertexList);
    }
Esempio n. 5
0
        private static void MeshNegZ(VoxelGrid voxels, VoxelPoint[,] plane, Action <ushort, VoxelSide, VoxelSide, Quad, Vector2i> quadProcessor)
        {
            var gridSize = voxels.GridSize;

            for (int z = 0; z < gridSize; z++)
            {
                bool someFacesExist = false;
                for (int x = 0; x < gridSize; x++)
                {
                    for (int y = 0; y < gridSize; y++)
                    {
                        var open  = z == 0 || !voxels[x, y, z - 1].Exists;
                        var voxel = voxels[x, y, z];
                        someFacesExist = (open && voxel.Exists) || someFacesExist;
                        plane[x, y]    = new VoxelPoint()
                        {
                            Voxel = voxels[x, y, z], Processed = !open
                        };
                    }
                }
                if (someFacesExist)
                {
                    var zPos = z;
                    FindPlaneRects(plane, (typeNum, orientation, rect) =>
                    {
                        var quad = new Quad(
                            new Vector3(rect.Right, rect.Top, zPos) * voxels.VoxelSize,
                            new Vector3(rect.Right, rect.Bottom, zPos) * voxels.VoxelSize,
                            new Vector3(rect.Left, rect.Bottom, zPos) * voxels.VoxelSize,
                            new Vector3(rect.Left, rect.Top, zPos) * voxels.VoxelSize,
                            -Vector3.UnitZ);
                        quadProcessor(typeNum, orientation, VoxelSide.NORTH, quad, new Vector2i(rect.Width, rect.Height));
                    });
                }
            }
        }
Esempio n. 6
0
        private static void MeshPosY(VoxelGrid voxels, VoxelPoint[,] plane, Action <ushort, VoxelSide, VoxelSide, Quad, Vector2i> quadProcessor)
        {
            var gridSize = voxels.GridSize;

            for (int y = 0; y < gridSize; y++)
            {
                bool someFacesExist = false;
                for (int x = 0; x < gridSize; x++)
                {
                    for (int z = 0; z < gridSize; z++)
                    {
                        var open  = y + 1 == gridSize || !voxels[x, y + 1, z].Exists;
                        var voxel = voxels[x, y, z];
                        someFacesExist = (open && voxel.Exists) || someFacesExist;
                        plane[x, z]    = new VoxelPoint()
                        {
                            Voxel = voxels[x, y, z], Processed = !open
                        };
                    }
                }
                var yPos = (y + 1);
                if (someFacesExist)
                {
                    FindPlaneRects(plane, (typeNum, orientation, rect) =>
                    {
                        var quad = new Quad(
                            new Vector3(rect.Left, yPos, rect.Top) * voxels.VoxelSize,
                            new Vector3(rect.Right, yPos, rect.Top) * voxels.VoxelSize,
                            new Vector3(rect.Right, yPos, rect.Bottom) * voxels.VoxelSize,
                            new Vector3(rect.Left, yPos, rect.Bottom) * voxels.VoxelSize,
                            Vector3.UnitY);
                        quadProcessor(typeNum, orientation, VoxelSide.TOP, quad, new Vector2i(rect.Width, rect.Height));
                    });
                }
            }
        }
Esempio n. 7
0
    public float GetDensity(int x, int y, int z)
    {
        VoxelPoint p = GetVoxelPoint(x, y, z);

        return(p.density);
    }
Esempio n. 8
0
    public void Initialize(World world, int chunkSize, Vector3Int position)
    {
        asteroidGeneration = world.gameObject.GetComponent <AsteroidGeneration>();

        this.chunkSize = chunkSize;
        this.position  = position;
        _isolevel      = world.isolevel;

        _densityGenerator = world.densityGenerator;

        int worldPosX = position.x;
        int worldPosY = position.y;
        int worldPosZ = position.z;

        midLength = chunkSize * asteroidGeneration.worldLength / 2;
        midWidth  = chunkSize * asteroidGeneration.worldWidth / 2;
        midHeight = chunkSize * asteroidGeneration.worldHeight / 2;

        points = new VoxelPoint[chunkSize + 1, chunkSize + 1, chunkSize + 1];

        asteroidGeneration = world.asteroidGeneration;
        _seed          = asteroidGeneration.seed;
        _marchingCubes = new MarchingCubes(points, _isolevel, _seed);

        var asteroidLength = asteroidGeneration.radius * 2;
        var asteroidWidth  = asteroidGeneration.radius * 2;
        var asteroidHeight = asteroidGeneration.radius * 2;

        // Fill array with density and material ID of 0
        for (int x = 0; x < points.GetLength(0); x++)
        {
            for (int y = 0; y < points.GetLength(1); y++)
            {
                for (int z = 0; z < points.GetLength(2); z++)
                {
                    points[x, y, z] = new VoxelPoint(
                        new Vector3Int(x, y, z),
                        0.0f,
                        0
                        );
                }
            }
        }

        for (int x = 0; x < points.GetLength(0); x++)
        {
            for (int y = 0; y < points.GetLength(1); y++)
            {
                for (int z = 0; z < points.GetLength(2); z++)
                {
                    int currX = x + worldPosX;
                    int currY = y + worldPosY;
                    int currZ = z + worldPosZ;

                    float density = GetVoxelDensity(world.asteroidData, currX, currY, currZ);
                    SetDensity(density, x, y, z);

                    int material = GetVoxelMaterial(world.asteroidData, currX, currY, currZ);
                    SetMaterial(material, x, y, z);
                }
            }
        }
    }