Ejemplo n.º 1
0
    public virtual void BuildFace(Block.ID id, Block.Face face, Vector3 corner, Vector3 up, Vector3 right, bool reversed, List <Vector3> verts, List <Vector2> uvs, List <int> tris)
    {
        int index = verts.Count;

        verts.Add(corner);
        verts.Add(corner + up);
        verts.Add(corner + up + right);
        verts.Add(corner + right);


        Vector2 uvWidth = new Vector2(1f / 16f, 1f / 16f);
        // Vector2 uvCorner = new Vector2(0.0001f,0.77f);

        Vector2 uvCorner = Texture.getTexture(id, face);

        //uvCorner.x += (float)(1 - 1) / 16;

        uvs.Add(uvCorner);
        uvs.Add(new Vector2(uvCorner.x, uvCorner.y + uvWidth.y));
        uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y + uvWidth.y));
        uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y));


        if (reversed)
        {
            tris.Add(index + 0);
            tris.Add(index + 1);
            tris.Add(index + 2);
            tris.Add(index + 2);
            tris.Add(index + 3);
            tris.Add(index + 0);
        }
        else
        {
            tris.Add(index + 1);
            tris.Add(index + 0);
            tris.Add(index + 2);
            tris.Add(index + 3);
            tris.Add(index + 2);
            tris.Add(index + 0);
        }
    }
Ejemplo n.º 2
0
    public virtual IEnumerator CreateVisualMesh()
    {
        List <Vector3> verts = new List <Vector3>();
        List <Vector2> uvs   = new List <Vector2>();
        List <int>     tris  = new List <int>();


        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                for (int z = 0; z < width; z++)
                {
                    if (map[x, y, z] == 0)
                    {
                        continue;
                    }

                    Block.ID block = (Block.ID)map[x, y, z];
                    // Left wall
                    if (IsTransparent(x - 1, y, z))
                    {
                        BuildFace(block, Block.Face.WEST, new Vector3(x, y, z), Vector3.up, Vector3.forward, false, verts, uvs, tris);
                    }
                    // Right wall
                    if (IsTransparent(x + 1, y, z))
                    {
                        BuildFace(block, Block.Face.EAST, new Vector3(x + 1, y, z), Vector3.up, Vector3.forward, true, verts, uvs, tris);
                    }

                    // Bottom wall
                    if (IsTransparent(x, y - 1, z))
                    {
                        BuildFace(block, Block.Face.BOTTOM, new Vector3(x, y, z), Vector3.forward, Vector3.right, false, verts, uvs, tris);
                    }
                    // Top wall
                    if (IsTransparent(x, y + 1, z))
                    {
                        BuildFace(block, Block.Face.TOP, new Vector3(x, y + 1, z), Vector3.forward, Vector3.right, true, verts, uvs, tris);
                    }

                    // Back
                    if (IsTransparent(x, y, z - 1))
                    {
                        BuildFace(block, Block.Face.NORTH, new Vector3(x, y, z), Vector3.up, Vector3.right, true, verts, uvs, tris);
                    }
                    // Front
                    if (IsTransparent(x, y, z + 1))
                    {
                        BuildFace(block, Block.Face.SOUTH, new Vector3(x, y, z + 1), Vector3.up, Vector3.right, false, verts, uvs, tris);
                    }
                }
            }
        }

        Mesh mesh = meshFilter.mesh;

        mesh.Clear();
        mesh.vertices           = verts.ToArray();
        mesh.triangles          = tris.ToArray();
        meshCollider.sharedMesh = mesh;
        mesh.SetUVs(0, uvs);
        //MeshUtility.Optimize(mesh);
        mesh.RecalculateNormals();



        yield return(0);
    }
Ejemplo n.º 3
0
    public static Vector2 getTexture(Block.ID id, Block.Face face)
    {
        Vector2 texture = new Vector2();
        int     index   = (int)face;

        switch (id)
        {
        case Block.ID.AIR:
            return(texture);

        case Block.ID.GRASS:
            return(grass[index]);

        case Block.ID.DIRT:
            return(dirt[0]);

        case Block.ID.STONE:
            return(stone[0]);

        case Block.ID.BEDROCK:
            return(bedrock[0]);

        case Block.ID.COBBLESTONE:
            return(cobblestone[0]);

        case Block.ID.GLASS:
            return(glass[0]);

        case Block.ID.OAK_LOG:
            return(oak_log[index]);

        case Block.ID.OAK_PLANKS:
            return(oak_planks[0]);

        case Block.ID.BRICKS:
            return(bricks[0]);

        case Block.ID.OAK_LEAVES:
            return(oak_leaves[0]);

        case Block.ID.MOSS_STONE:
            return(moss_stone[0]);

        case Block.ID.MOSS_BRICK:
            return(moss_brick[0]);

        case Block.ID.CRACKED_BRICK:
            return(cracked_brick[0]);

        case Block.ID.STONE_BRICK:
            return(stone_bricks[0]);

        case Block.ID.CLAY:
            return(clay[0]);

        case Block.ID.SANDSTONE:
            return(sandstone[index]);

        case Block.ID.SAND:
            return(sand[0]);

        case Block.ID.GRAVEL:
            return(gravel[0]);

        case Block.ID.BOOKCASE:
            return(bookcase[index]);

        case Block.ID.CURSOR_1:
            return(cursor[0]);
        }


        return(texture);
    }