示例#1
0
    public void UpdateMeshFrom3dBoundedByHeightMap(
        float[,] heightMap, Defines.MapParams mapSize,
        Vector3 samplingRate, float spaceCreationThreshold)
    {
        var spaceMap = HeightMapAssembler.AssembleSpaceMap(mapSize);
        var vertices = new List <Vector3>();
        var colors   = new List <Color>();

        for (int x = 0; x < mapSize.width; x += (int)samplingRate.x)
        {
            for (int y = 0; y < mapSize.height; y += (int)samplingRate.y)
            {
                for (int z = 0; z < mapSize.depth; z += (int)samplingRate.z)
                {
                    //Debug.Log("Vertex " + x + ", " + y + ", " + z + " = " + spaceMap[x, y, z]);
                    if (heightMap[x, y] * mapSize.depth > z && spaceMap[x, y, z] > spaceCreationThreshold)
                    {
                        vertices.Add(new Vector3(x / (int)samplingRate.x, y / (int)samplingRate.y, z));
                        colors.Add(ColorPicker.GetColor(colorParams, heightMap[x, y]));
                    }
                }
            }
        }
        var mesh = VoxelHandler.CreateMulticubeMesh(vertices, colors);

        filter.mesh = mesh;
    }
示例#2
0
    public void UpdateMeshFrom3dBoundedByHeightMapOptimized(
        float[,] heightMap, Defines.MapParams mapSize,
        Vector3 samplingRate, System.Func <float, float, float, bool> spaceProbingFunction)
    {
        cleanupChunks();

        // ToDo: Could pass height generating function instead of height map to
        // free ourselves from array.Length() sanity checks and solve potential rounding problems
        System.Func <float, float, float, bool> spaceProbingFunc =
            (x, y, z) => z == 0 ||
            (spaceProbingFunction(x, y, z) &&
             x >= 0 &&
             y >= 0 &&
             z >= 0 &&
             x < heightMap.GetLength(0) &&
             y < heightMap.GetLength(1) &&
             z < heightMap[(int)x, (int)y] * mapSize.depth);

        int xBot = 0;

        while (xBot < mapSize.width)
        {
            int xTop = Mathf.Min(mapSize.width, xBot + chunkSize);
            int yBot = 0;
            while (yBot < mapSize.height)
            {
                int yTop         = Mathf.Min(mapSize.height, yBot + chunkSize);
                var chunkMapSize = new Defines.MapParams(
                    new Vector3(xTop - xBot, yTop - yBot, mapSize.depth),
                    new Vector3(xBot, yBot, mapSize.offset.z));

                GameObject newChunk = prepareNewChunk();

                var chunkMesh = VoxelHandler.CreateMulticubeMeshOptimized(heightMap, spaceProbingFunc, samplingRate, chunkMapSize, colorParams);
                chunkMesh.Optimize();
                newChunk.GetComponent <MeshFilter>().mesh = chunkMesh;
                yBot += chunkSize;
            }
            xBot += chunkSize;
        }
    }
示例#3
0
    public void Start()
    {
        if (Camera.main != null)
        {
            _cameraTransform = Camera.main.transform;
        }
        if (Camera.main != null)
        {
            _playerTransform = Camera.main.transform.parent.transform;
        }

        VoxelHandler.instance = this;
        gameObject.GetComponent <Atlas>().GenerateAtlas();
        LoadBlockData();
        chunks = new GameObject[xChunkCount, zChunkCount];

        for (var x = 0; x < chunks.GetLength(0); x++)
        {
            for (var z = 0; z < chunks.GetLength(1); z++)
            {
                var chunk = new GameObject();

                chunk.transform.SetParent(this.transform);
                chunk.name = $"Chunk {x}:{z}";
                chunk.transform.position = new Vector3(16 * x, 0, 16 * z);

                var chunkComponent = chunk.AddComponent <Chunk>();
                chunkComponent.x = x;
                chunkComponent.z = z;
                chunkComponent.Generate();

                chunks[x, z] = chunk;
            }
        }

        var turnFraction = (1f + Mathf.Sqrt(5)) / 2f;

        var numberOfTrees = xChunkCount * zChunkCount * treesPerChunk;

        for (var i = 0; i < numberOfTrees; i++)
        {
            var distance = i / (numberOfTrees - 1f);
            var angle    = 2 * Mathf.PI * turnFraction * i;

            var x = 64f + (64f * (distance * Mathf.Cos(angle)));
            var z = 64f + (64f * (distance * Mathf.Sin(angle)));

            PlaceTree((int)x, (int)z, false);
        }

        // This loop is seperate since all Meshes should be generated AFTER the chunks
        for (var x = 0; x < chunks.GetLength(0); x++)
        {
            for (var z = 0; z < chunks.GetLength(1); z++)
            {
                var chunkMesh = chunks[x, z].AddComponent <ChunkMesh>();
            }
        }

        _chunkOffsets = new[]
        {
            new Vector3(0f, 0f, 0f),
            new Vector3(16f, 0f, 0f),
            new Vector3(0f, 32f, 0f),
            new Vector3(16f, 32f, 0f),
            new Vector3(0f, 0f, 16f),
            new Vector3(16f, 0f, 16f),
            new Vector3(0f, 32f, 16f),
            new Vector3(16f, 32f, 16f),
        };
    }