コード例 #1
0
    public static Vector2 GetUV(int index, BlockDatabase.BlockData data)
    {
        Vector2 uv = UVs[index];

        if (index <= 5)
        {
            uv += data.frontOffset;
        }
        else if (index <= 11)
        {
            uv += data.topoffset;
        }
        else if (index <= 17)
        {
            uv += data.rightOffset;
        }
        else if (index <= 23)
        {
            uv += data.leftOffset;
        }
        else if (index <= 29)
        {
            uv += data.backOffset;
        }
        else if (index <= 35)
        {
            uv += data.bottomOffset;
        }

        return(uv);
    }
コード例 #2
0
ファイル: Chunk.cs プロジェクト: takaaptech/VoxelTerrainGen
    public void GenerateChunk()
    {
        if (!belowChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(0, -chunkSize, 0), out belowChunk);
        }
        if (!topChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(0, chunkSize, 0), out topChunk);
        }
        if (!frontChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(0, 0, chunkSize), out frontChunk);
        }
        if (!behindChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(0, 0, -chunkSize), out behindChunk);
        }
        if (!leftChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(-chunkSize, 0, 0), out leftChunk);
        }
        if (!rightChunk)
        {
            World.instance.chunks.TryGetValue(transform.localPosition + new Vector3(chunkSize, 0, 0), out rightChunk);
        }



        List <Vector2> uvs                = new List <Vector2>();
        List <Vector3> vertices           = new List <Vector3>();
        List <int>     mainMeshTriangles  = new List <int>();
        List <int>     waterMeshTriangles = new List <int>();

        foreach (KeyValuePair <Vector3Int, BlockDatabase.BlockType> pair in blockPositions)
        {
            List <int> neededVerts            = CheckAdjacentPositions(pair.Key);
            BlockDatabase.BlockData blockData = null;

            foreach (BlockDatabase.BlockData data in BlockDatabase.instance.blockData)
            {
                if (data.type == pair.Value)
                {
                    blockData = data;
                }
            }

            for (int i = 0; i < neededVerts.Count; i += 2)
            {
                for (int t = neededVerts[i]; t <= neededVerts[i + 1]; t++)
                {
                    uvs.Add(Block.GetUV(t, blockData));
                    vertices.Add(Block.vertices[Block.triangles[t]] + pair.Key);

                    if (pair.Value != BlockDatabase.BlockType.WATER)
                    {
                        mainMeshTriangles.Add(vertices.Count - 1);
                    }
                    else
                    {
                        waterMeshTriangles.Add(vertices.Count - 1);
                    }
                }
            }
        }

        //Build the mesh
        mesh.Clear();
        mesh.subMeshCount = 2;
        mesh.vertices     = vertices.ToArray();
        mesh.SetTriangles(mainMeshTriangles.ToArray(), 0);
        mesh.SetTriangles(waterMeshTriangles.ToArray(), 1);
        mesh.uv = uvs.ToArray();
        mesh.RecalculateNormals();
        //collider.sharedMesh = mesh;
    }