Esempio n. 1
0
    public void Generate(int height)
    {
        int chunkSizeX = SmoothVoxelSettings.ChunkSizeX;
        int chunkSizeY = SmoothVoxelSettings.ChunkSizeY;
        int chunkSizeZ = SmoothVoxelSettings.ChunkSizeZ;

        int meterSizeX = SmoothVoxelSettings.MeterSizeX;
        int meterSizeY = SmoothVoxelSettings.MeterSizeY;
        int meterSizeZ = SmoothVoxelSettings.MeterSizeZ;

        Sampler.SetChunkSettings(SmoothVoxelSettings.voxelsPerMeter,
                                 new Vector3Int(chunkSizeX, chunkSizeY, chunkSizeZ),
                                 new Vector3Int(meterSizeX, meterSizeY, meterSizeZ),
                                 Mathf.RoundToInt(1 / (float)SmoothVoxelSettings.voxelsPerMeter),
                                 ((1.0f / (float)SmoothVoxelSettings.voxelsPerMeter) / 2.0f),
                                 new Vector3(meterSizeX / (float)chunkSizeX, meterSizeY / (float)chunkSizeY, meterSizeZ / (float)chunkSizeZ));

        Vector3Int topVoxel    = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMax(), 0));
        Vector3Int bottomVoxel = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMin(), 0));

        int topChunk    = VoxelConversions.VoxelToChunk(topVoxel).y;
        int bottomChunk = VoxelConversions.VoxelToChunk(bottomVoxel).y;

        if (NetworkMode)
        {
            for (int y = 0; y <= topChunk; y++)
            {
                SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller);
            }
        }
        else
        {
            Vector2Int bottomLeft = new Vector2(Location.x * chunkSizeX, Location.y * chunkSizeZ);
            Vector2Int topRight   = new Vector2(Location.x * chunkSizeX + chunkSizeX, Location.y * chunkSizeZ + chunkSizeZ);
            Sampler.SetSurfaceData(bottomLeft, topRight);

            for (int y = 0; y <= topChunk; y++)
            {
                SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller);
            }
            Loom.QueueOnMainThread(() =>
            {
                Debug.Log("Spawning grass...");
                SpawnGrass();
            });
        }
    }
    public MeshData Render(bool renderOnly)
    {
        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();
        List <Vector3> Normals   = new List <Vector3>();

        if (empty)
        {
            SafeDebug.Log("Chunk is empty... skipping.");
            return(new MeshData(vertices.ToArray(), triangles.ToArray(), null, Normals.ToArray()));
        }

        if (Initialized)
        {
            SetSurroundingChunks();
            int cx = location.x;
            int cy = location.y;
            int cz = location.z;

            int xStart = cx * ChunkSizeX;
            int xEnd   = cx * ChunkSizeX + ChunkSizeX;

            int yStart = cy * ChunkSizeY;
            int yEnd   = cy * ChunkSizeY + ChunkSizeY;

            int zStart = cz * ChunkSizeZ;
            int zEnd   = cz * ChunkSizeZ + ChunkSizeZ;

            int x = 0;
            int y = 0;
            int z = 0;

            int globalLocX = 0;
            int globalLocY = 0;
            int globalLocZ = 0;
            //try
            //{
            watch.Reset();
            watch.Start();
            //
            for (globalLocY = yStart, y = 0; y < ChunkSizeY; globalLocY++, y++)
            {
                if (globalLocY < Sampler.GetMin() - 20 || globalLocY > Sampler.GetMax())
                {
                    continue;
                }

                for (globalLocZ = zStart, z = 0; z < ChunkSizeZ; globalLocZ++, z++)
                {
                    for (globalLocX = xStart, x = 0; x < ChunkSizeX; globalLocX++, x++)
                    {
                        if (deactivated)
                        {
                            break;
                        }

                        Vector3     worldPos  = new Vector3(x * xSideLength, y * ySideLength, z * zSideLength);
                        Vector3Int  globalPos = new Vector3Int(globalLocX * skipDist, globalLocY * skipDist, globalLocZ * skipDist);
                        Vector3Int  localPos  = new Vector3Int(x, y, z);
                        GridPoint[] grid      = new GridPoint[8];
                        //miscWatch.Start();
                        for (int i = 0; i < grid.Length; i++)
                        {
                            grid[i] = GetVector4(worldPos + locOffset[i], localPos + directionOffsets[i], globalPos + globalOffsets[i], !renderOnly);
                        }
                        //miscWatch.Stop();

                        RenderBlock(grid, 0, vertices, triangles, Normals, null);
                    }
                }
            }
            ResetPageNeighbors();
            watch.Stop();
            renderTime = watch.Elapsed.ToString();

            //Debug.Log("Chunk generated in: " + renderTime);

            miscTime = miscWatch.Elapsed.ToString();
            miscTime = globalLocZ.ToString();

            /*}
             * catch (Exception e)
             * {
             *  SafeDebug.LogError(e.GetType().ToString() + ": " + e.Message + "\n " + e.StackTrace);
             * }*/
        }
        return(new MeshData(vertices.ToArray(), triangles.ToArray(), null, Normals.ToArray()));
    }