예제 #1
0
    private Mesh CreateQuads(Cubeside side, BlockType bType)
    {
        Mesh mesh = new Mesh();

        mesh.name = "ScriptedMesh" + side.ToString();

        Vector3[] vertices = new Vector3[4];
        Vector3[] normals  = new Vector3[4];
        Vector2[] uvs      = new Vector2[4];

        int[] triangles = new int[6];

        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        if (bType == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if (bType == BlockType.GRASS && side == Cubeside.BOTTOM)
        {
            uv00 = blockUVs[(int)(BlockType.DIRT + 1), 0];
            uv10 = blockUVs[(int)(BlockType.DIRT + 1), 1];
            uv01 = blockUVs[(int)(BlockType.DIRT + 1), 2];
            uv11 = blockUVs[(int)(BlockType.DIRT + 1), 3];
        }
        else
        {
            uv00 = blockUVs[(int)(bType + 1), 0];
            uv10 = blockUVs[(int)(bType + 1), 1];
            uv01 = blockUVs[(int)(bType + 1), 2];
            uv11 = blockUVs[(int)(bType + 1), 3];
        }

        //All vertices
        Vector3 p0 = new Vector3(-.5f, -.5f, .5f) + position;
        Vector3 p1 = new Vector3(.5f, -.5f, .5f) + position;
        Vector3 p2 = new Vector3(.5f, -.5f, -.5f) + position;
        Vector3 p3 = new Vector3(-.5f, -.5f, -.5f) + position;
        Vector3 p4 = new Vector3(-.5f, .5f, .5f) + position;
        Vector3 p5 = new Vector3(.5f, .5f, .5f) + position;
        Vector3 p6 = new Vector3(.5f, .5f, -.5f) + position;
        Vector3 p7 = new Vector3(-.5f, .5f, -.5f) + position;

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[]
            {
                Vector3.down,
                Vector3.down,
                Vector3.down,
                Vector3.down
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[]
            {
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[]
            {
                Vector3.left,
                Vector3.left,
                Vector3.left,
                Vector3.left
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[]
            {
                Vector3.right,
                Vector3.right,
                Vector3.right,
                Vector3.right
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[]
            {
                Vector3.forward,
                Vector3.forward,
                Vector3.forward,
                Vector3.forward
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[]
            {
                Vector3.back,
                Vector3.back,
                Vector3.back,
                Vector3.back
            };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        return(mesh);
    }
예제 #2
0
    void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = "ScriptedMesh" + side.ToString();

        Vector3[] vertices  = new Vector3[4];
        Vector3[] normals   = new Vector3[4];
        Vector2[] uvs       = new Vector2[4];
        int[]     triangles = new int[6];

        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        if (bType == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if (bType == BlockType.GRASS && side == Cubeside.BOTTOM)
        {
            uv00 = blockUVs[(int)(BlockType.DIRT + 1), 0];
            uv10 = blockUVs[(int)(BlockType.DIRT + 1), 1];
            uv01 = blockUVs[(int)(BlockType.DIRT + 1), 2];
            uv11 = blockUVs[(int)(BlockType.DIRT + 1), 3];
        }
        else
        {
            uv00 = blockUVs[(int)(bType + 1), 0];
            uv10 = blockUVs[(int)(bType + 1), 1];
            uv01 = blockUVs[(int)(bType + 1), 2];
            uv11 = blockUVs[(int)(bType + 1), 3];
        }

        //all possible vertices
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);


        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] { Vector3.down, Vector3.down,
                                       Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] { Vector3.up, Vector3.up,
                                       Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] { Vector3.left, Vector3.left,
                                       Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] { Vector3.right, Vector3.right,
                                       Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] { Vector3.forward, Vector3.forward,
                                       Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] { Vector3.back, Vector3.back,
                                       Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("Quad");

        quad.transform.parent = this.gameObject.transform;
        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = mesh;
        MeshRenderer renderer = quad.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

        renderer.material = cubeMaterial;
    }
예제 #3
0
    void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = side.ToString();

        Vector3[] vertices  = new Vector3[4];
        Vector3[] normals   = new Vector3[4];
        Vector2[] uvs       = new Vector2[4];
        int[]     triangles = new int[6];

        //all possible UVS
        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        if (bType == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if ((bType == BlockType.GRASS && side == Cubeside.BOTTOM) || bType == BlockType.DIRT)
        {
            uv00 = blockUVs[2, 0];
            uv10 = blockUVs[2, 1];
            uv01 = blockUVs[2, 2];
            uv11 = blockUVs[2, 3];
        }
        else if (bType == BlockType.GRASS)
        {
            uv00 = blockUVs[1, 0];
            uv10 = blockUVs[1, 1];
            uv01 = blockUVs[1, 2];
            uv11 = blockUVs[1, 3];
        }
        else
        {
            uv00 = blockUVs[3, 0];
            uv10 = blockUVs[3, 1];
            uv01 = blockUVs[3, 2];
            uv11 = blockUVs[3, 3];
        }

        //all possible vertices
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices  = new Vector3[] { p0, p1, p2, p3 };
            normals   = new Vector3[] { Vector3.down, Vector3.down, Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.TOP:
            vertices  = new Vector3[] { p7, p6, p5, p4 };
            normals   = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.LEFT:
            vertices  = new Vector3[] { p7, p4, p0, p3 };
            normals   = new Vector3[] { Vector3.left, Vector3.left, Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.RIGHT:
            vertices  = new Vector3[] { p5, p6, p2, p1 };
            normals   = new Vector3[] { Vector3.right, Vector3.right, Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.FRONT:
            vertices  = new Vector3[] { p4, p5, p1, p0 };
            normals   = new Vector3[] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.BACK:
            vertices  = new Vector3[] { p6, p7, p3, p2 };
            normals   = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("quad");

        quad.transform.position = position;
        quad.transform.parent   = parent.transform;

        MeshFilter meshFilter = quad.AddComponent <MeshFilter>();

        meshFilter.mesh = mesh;
    }
예제 #4
0
    void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = side.ToString();

        Vector3[] vertices  = new Vector3[4];
        Vector3[] normals   = new Vector3[4];
        Vector2[] uvs       = new Vector2[4];
        int[]     triangles = new int[6];

        //all possible UVS
        Vector2 uv00 = new Vector2(0f, 0f);
        Vector2 uv10 = new Vector2(1f, 0f);
        Vector2 uv01 = new Vector2(0f, 1f);
        Vector2 uv11 = new Vector2(1f, 1f);

        //all possible vertices
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices  = new Vector3[] { p0, p1, p2, p3 };
            normals   = new Vector3[] { Vector3.down, Vector3.down, Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.TOP:
            vertices  = new Vector3[] { p7, p6, p5, p4 };
            normals   = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.LEFT:
            vertices  = new Vector3[] { p7, p4, p0, p3 };
            normals   = new Vector3[] { Vector3.left, Vector3.left, Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.RIGHT:
            vertices  = new Vector3[] { p5, p6, p2, p1 };
            normals   = new Vector3[] { Vector3.right, Vector3.right, Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.FRONT:
            vertices  = new Vector3[] { p4, p5, p1, p0 };
            normals   = new Vector3[] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;

        case Cubeside.BACK:
            vertices  = new Vector3[] { p6, p7, p3, p2 };
            normals   = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 0, 2, 1, 0, 3, 2 };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("quad");

        quad.transform.parent = transform;
        MeshFilter meshFilter = quad.AddComponent <MeshFilter>();

        meshFilter.mesh = mesh;
    }
예제 #5
0
    private void CreateQuad(Cubeside side)
    {
        // Create a new mesh and give it a name
        Mesh mesh = new Mesh
        {
            name = "CubeMesh" + side.ToString()
        };

        // Declare the needed vectors for the mesh
        Vector3[] vertices = null, normals = null;

        // Intitialize the triangles vector
        int[] triangles = new int[] { 3, 1, 0, 3, 2, 1 };

        // Initialize all the possible verticies
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        // Set the value of the vectors based on the cubeside
        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] { Vector3.down, Vector3.down, Vector3.down, Vector3.down };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] { Vector3.left, Vector3.left, Vector3.left, Vector3.left };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] { Vector3.right, Vector3.right, Vector3.right, Vector3.right };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back };
            break;
        }

        // Save the created vectors in the mesh and recalculate bounding volume
        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.triangles = triangles;

        mesh.uv = new Vector2[] { blockUVs[3], blockUVs[2], blockUVs[0], blockUVs[1] };;
        mesh.RecalculateBounds();

        // Create the new GameObject for the Quad
        GameObject quad = new GameObject("Quad");

        // Set the position, parent object
        quad.transform.position = position;
        quad.transform.parent   = parent.transform;

        // Add MeshFilter Component to the GameObject
        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = mesh;
    }
예제 #6
0
    void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = "ScriptedMesh" + side.ToString();

        Vector3[]      vertices = new Vector3[4];
        Vector3[]      normals  = new Vector3[4];
        Vector2[]      uvs      = new Vector2[4];
        List <Vector2> suvs     = new List <Vector2>();

        int[] triangles = new int[6];

        //all possible UVS
        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        if (bType == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if (bType == BlockType.GRASS && side == Cubeside.BOTTOM)
        {
            uv00 = blockUVs[(int)(BlockType.DIRT + 1), 0];
            uv10 = blockUVs[(int)(BlockType.DIRT + 1), 1];
            uv01 = blockUVs[(int)(BlockType.DIRT + 1), 2];
            uv11 = blockUVs[(int)(BlockType.DIRT + 1), 3];
        }
        else
        {
            uv00 = blockUVs[(int)(bType + 1), 0];
            uv10 = blockUVs[(int)(bType + 1), 1];
            uv01 = blockUVs[(int)(bType + 1), 2];
            uv11 = blockUVs[(int)(bType + 1), 3];
        }
        //all possible vertices

        suvs.Add(blockUVs[(int)(health + 1), 3]);
        suvs.Add(blockUVs[(int)(health + 1), 2]);
        suvs.Add(blockUVs[(int)(health + 1), 0]);
        suvs.Add(blockUVs[(int)(health + 1), 1]);

        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        Vector2[] uvsref = new Vector2[] { (uv11), (uv01), (uv00), (uv10) };

        //Vector2[] uvsref = new Vector2[] { (uv11/16.00f), (uv01/16.00f), (uv00/16.00f), (uv10/16.00f)};
        //
        //for(int i = 0; i < uvsref.Length; i++){
        //	uvsref[i].y += 15f/16f;
        //}

        int[] trianglesref = new int[] { 3, 1, 0, 3, 2, 1 };

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices  = new Vector3[] { p0, p1, p2, p3 };
            normals   = new Vector3[] { Vector3.down, Vector3.down, Vector3.down, Vector3.down };
            uvs       = uvsref;
            triangles = trianglesref;
            break;

        case Cubeside.TOP:
            vertices  = new Vector3[] { p7, p6, p5, p4 };
            normals   = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
            uvs       = uvsref;
            triangles = trianglesref;
            break;

        case Cubeside.LEFT:
            vertices  = new Vector3[] { p7, p4, p0, p3 };
            normals   = new Vector3[] { Vector3.left, Vector3.left, Vector3.left, Vector3.left };
            uvs       = uvsref;
            triangles = trianglesref;
            break;

        case Cubeside.RIGHT:
            vertices  = new Vector3[] { p5, p6, p2, p1 };
            normals   = new Vector3[] { Vector3.right, Vector3.right, Vector3.right, Vector3.right };
            uvs       = uvsref;
            triangles = trianglesref;
            break;

        case Cubeside.FRONT:
            vertices  = new Vector3[] { p4, p5, p1, p0 };
            normals   = new Vector3[] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
            uvs       = uvsref;
            triangles = trianglesref;
            break;

        case Cubeside.BACK:
            vertices  = new Vector3[] { p6, p7, p3, p2 };
            normals   = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back };
            uvs       = uvsref;
            triangles = trianglesref;
            break;
        }

        mesh.vertices = vertices;
        mesh.normals  = normals;
        mesh.uv       = uvs;
        mesh.SetUVs(1, suvs);
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("Quad");

        quad.transform.position = position;
        quad.transform.parent   = parent.transform;

        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = mesh;
    }
예제 #7
0
파일: Block.cs 프로젝트: lsroth/DET-SS2019
    /// <summary>
    /// Assembles one side of a cube's mesh by selecting the UVs, defining the vertices and calculating the normals.
    /// </summary>
    /// <param name="side">Quad to be created for this side</param>
    private void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = "ScriptedMesh" + side.ToString();

        Vector3[]      vertices = new Vector3[4];
        Vector3[]      normals  = new Vector3[4];
        Vector2[]      uvs      = new Vector2[4];
        List <Vector2> suvs     = new List <Vector2>();

        int[] triangles = new int[6];

        // All possible UVs
        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        if (blockType == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if (blockType == BlockType.GRASS)
        {
            uv00 = blockUVs[1, 0];
            uv10 = blockUVs[1, 1];
            uv01 = blockUVs[1, 2];
            uv11 = blockUVs[1, 3];
        }
        // TODO: grass bottom = sand??
        // else if(blockType == BlockType.GRASS && side == Cubeside.BOTTOM)
        // {
        //  uv00 = blockUVs[(int)(BlockType.DIRT+2),0];
        //  uv10 = blockUVs[(int)(BlockType.DIRT+2),1];
        //  uv01 = blockUVs[(int)(BlockType.DIRT+2),2];
        //  uv11 = blockUVs[(int)(BlockType.DIRT+2),3];
        // }
        else if (blockType == BlockType.CACTUS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[2, 0];
            uv10 = blockUVs[2, 1];
            uv01 = blockUVs[2, 2];
            uv11 = blockUVs[2, 3];
        }
        else if (blockType == BlockType.PUMPKIN && side == Cubeside.FRONT)
        {
            uv00 = blockUVs[(int)(blockType + 1), 0];
            uv10 = blockUVs[(int)(blockType + 1), 1];
            uv01 = blockUVs[(int)(blockType + 1), 2];
            uv11 = blockUVs[(int)(blockType + 1), 3];
        }
        else if (blockType == BlockType.PUMPKIN && side == Cubeside.TOP)
        {
            uv00 = blockUVs[(int)(blockType + 3), 0];
            uv10 = blockUVs[(int)(blockType + 3), 1];
            uv01 = blockUVs[(int)(blockType + 3), 2];
            uv11 = blockUVs[(int)(blockType + 3), 3];
        }
        else if (blockType == BlockType.LISA && side == Cubeside.TOP)
        {
            uv00 = blockUVs[(int)(blockType + 3), 0];
            uv10 = blockUVs[(int)(blockType + 3), 1];
            uv01 = blockUVs[(int)(blockType + 3), 2];
            uv11 = blockUVs[(int)(blockType + 3), 3];
        }
        else if (blockType == BlockType.LISA)
        {
            uv00 = blockUVs[(int)(blockType + 4), 0];
            uv10 = blockUVs[(int)(blockType + 4), 1];
            uv01 = blockUVs[(int)(blockType + 4), 2];
            uv11 = blockUVs[(int)(blockType + 4), 3];
        }
        else if (blockType == BlockType.JULIA && side == Cubeside.TOP)
        {
            uv00 = blockUVs[(int)(blockType + 4), 0];
            uv10 = blockUVs[(int)(blockType + 4), 1];
            uv01 = blockUVs[(int)(blockType + 4), 2];
            uv11 = blockUVs[(int)(blockType + 4), 3];
        }
        else if (blockType == BlockType.JULIA || blockType == BlockType.SIGNBASE)
        {
            uv00 = blockUVs[(int)(blockType + 5), 0];
            uv10 = blockUVs[(int)(blockType + 5), 1];
            uv01 = blockUVs[(int)(blockType + 5), 2];
            uv11 = blockUVs[(int)(blockType + 5), 3];
        }
        else if (blockType == BlockType.HELGE)
        {
            uv00 = blockUVs[25, 0];
            uv10 = blockUVs[25, 1];
            uv01 = blockUVs[25, 2];
            uv11 = blockUVs[25, 3];
        }
        else if (blockType == BlockType.CACTUSBASE)
        {
            uv00 = blockUVs[6, 0];
            uv10 = blockUVs[6, 1];
            uv01 = blockUVs[6, 2];
            uv11 = blockUVs[6, 3];
        }
        else if (blockType == BlockType.SIGNLEFT)
        {
            if (side == Cubeside.BACK)
            {
                uv00 = blockUVs[21, 0];
                uv10 = blockUVs[21, 1];
                uv01 = blockUVs[21, 2];
                uv11 = blockUVs[21, 3];
            }
            else if (side == Cubeside.FRONT)
            {
                uv00 = blockUVs[23, 0];
                uv10 = blockUVs[23, 1];
                uv01 = blockUVs[23, 2];
                uv11 = blockUVs[23, 3];
            }
            else
            {
                uv00 = blockUVs[24, 0];
                uv10 = blockUVs[24, 1];
                uv01 = blockUVs[24, 2];
                uv11 = blockUVs[24, 3];
            }
        }
        else if (blockType == BlockType.SIGNRIGHT)
        {
            if (side == Cubeside.FRONT)
            {
                uv00 = blockUVs[21, 0];
                uv10 = blockUVs[21, 1];
                uv01 = blockUVs[21, 2];
                uv11 = blockUVs[21, 3];
            }
            else if (side == Cubeside.BACK)
            {
                uv00 = blockUVs[23, 0];
                uv10 = blockUVs[23, 1];
                uv01 = blockUVs[23, 2];
                uv11 = blockUVs[23, 3];
            }
            else
            {
                uv00 = blockUVs[24, 0];
                uv10 = blockUVs[24, 1];
                uv01 = blockUVs[24, 2];
                uv11 = blockUVs[24, 3];
            }
        }
        else if (blockType == BlockType.SIGNMIDDLE)
        {
            if (side == Cubeside.BACK || side == Cubeside.FRONT)
            {
                uv00 = blockUVs[22, 0];
                uv10 = blockUVs[22, 1];
                uv01 = blockUVs[22, 2];
                uv11 = blockUVs[22, 3];
            }
            else
            {
                uv00 = blockUVs[24, 0];
                uv10 = blockUVs[24, 1];
                uv01 = blockUVs[24, 2];
                uv11 = blockUVs[24, 3];
            }
        }
        else
        {
            uv00 = blockUVs[(int)(blockType + 2), 0];
            uv10 = blockUVs[(int)(blockType + 2), 1];
            uv01 = blockUVs[(int)(blockType + 2), 2];
            uv11 = blockUVs[(int)(blockType + 2), 3];
        }

        // Set cracks
        suvs.Add(blockUVs[(int)(health + 2), 3]);
        suvs.Add(blockUVs[(int)(health + 2), 2]);
        suvs.Add(blockUVs[(int)(health + 2), 0]);
        suvs.Add(blockUVs[(int)(health + 2), 1]);

        //{uv11, uv01, uv00, uv10};

        // All possible vertices
        // Top vertices
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        // Bottom vertices
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] { Vector3.down, Vector3.down,
                                       Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] { Vector3.up, Vector3.up,
                                       Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] { Vector3.left, Vector3.left,
                                       Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] { Vector3.right, Vector3.right,
                                       Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] { Vector3.forward, Vector3.forward,
                                       Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] { Vector3.back, Vector3.back,
                                       Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;
        }

        mesh.vertices = vertices;
        mesh.normals  = normals;
        mesh.uv       = uvs;
        mesh.SetUVs(1, suvs);
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("Quad");

        quad.transform.position = position;
        quad.transform.parent   = this.parent.transform;

        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = mesh;
    }
예제 #8
0
    /// <summary>
    /// Assembles one side of a cube's mesh by selecting the UVs, defining the vertices and calculating the normals.
    /// </summary>
    /// <param name="side">Quad to be created for this side</param>
    private void CreateQuad(Cubeside side)
    {
        Mesh mesh = new Mesh();

        mesh.name = "ScriptedMesh" + side.ToString();

        Vector3[]      vertices = new Vector3[4];
        Vector3[]      normals  = new Vector3[4];
        Vector2[]      uvs      = new Vector2[4];
        List <Vector2> suvs     = new List <Vector2>();

        int[] triangles = new int[6];

        // All possible UVs
        Vector2 uv00;
        Vector2 uv10;
        Vector2 uv01;
        Vector2 uv11;

        {
            uv00 = blockUVs[(int)(blockType), 0];
            uv10 = blockUVs[(int)(blockType), 1];
            uv01 = blockUVs[(int)(blockType), 2];
            uv11 = blockUVs[(int)(blockType), 3];
        }


        //{uv11, uv01, uv00, uv10};

        // All possible vertices
        // Top vertices
        Vector3 p0 = new Vector3(-0.5f, -0.5f, 0.5f);
        Vector3 p1 = new Vector3(0.5f, -0.5f, 0.5f);
        Vector3 p2 = new Vector3(0.5f, -0.5f, -0.5f);
        Vector3 p3 = new Vector3(-0.5f, -0.5f, -0.5f);
        // Bottom vertices
        Vector3 p4 = new Vector3(-0.5f, 0.5f, 0.5f);
        Vector3 p5 = new Vector3(0.5f, 0.5f, 0.5f);
        Vector3 p6 = new Vector3(0.5f, 0.5f, -0.5f);
        Vector3 p7 = new Vector3(-0.5f, 0.5f, -0.5f);

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] { Vector3.down, Vector3.down,
                                       Vector3.down, Vector3.down };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] { Vector3.up, Vector3.up,
                                       Vector3.up, Vector3.up };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] { Vector3.left, Vector3.left,
                                       Vector3.left, Vector3.left };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] { Vector3.right, Vector3.right,
                                       Vector3.right, Vector3.right };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] { Vector3.forward, Vector3.forward,
                                       Vector3.forward, Vector3.forward };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] { Vector3.back, Vector3.back,
                                       Vector3.back, Vector3.back };
            uvs       = new Vector2[] { uv11, uv01, uv00, uv10 };
            triangles = new int[] { 3, 1, 0, 3, 2, 1 };
            break;
        }

        mesh.vertices = vertices;
        mesh.normals  = normals;
        mesh.uv       = uvs;
        mesh.SetUVs(1, suvs);
        mesh.triangles = triangles;

        mesh.RecalculateBounds();

        GameObject quad = new GameObject("Quad");

        quad.transform.position = position;
        quad.transform.parent   = this.parent.transform;

        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        meshFilter.mesh = mesh;
    }
예제 #9
0
    void CreateQuad(Cubeside side)
    {
        GameObject quad = new GameObject("quad");

        quad.transform.position = position;
        quad.transform.parent   = owner.chunk.gameObject.transform;

        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));

        Mesh mesh = meshFilter.mesh = new Mesh();

        mesh.name = prefix + side.ToString();

        Vector3[] vertices = new Vector3[4];
        Vector3[] normals  = new Vector3[4];

        // All Possible UV's
        Vector2 uv00 = new Vector2(0f, 0f);
        Vector2 uv10 = new Vector2(1f, 0f);
        Vector2 uv01 = new Vector2(0f, 1f);
        Vector2 uv11 = new Vector2(1f, 1f);

        if (element == BlockType.GRASS && side == Cubeside.TOP)
        {
            uv00 = blockUVs[0, 0];
            uv10 = blockUVs[0, 1];
            uv01 = blockUVs[0, 2];
            uv11 = blockUVs[0, 3];
        }
        else if (element == BlockType.GRASS && side == Cubeside.BOTTOM)
        {
            uv00 = blockUVs[(int)(BlockType.DIRT + 1), 0];
            uv10 = blockUVs[(int)(BlockType.DIRT + 1), 1];
            uv01 = blockUVs[(int)(BlockType.DIRT + 1), 2];
            uv11 = blockUVs[(int)(BlockType.DIRT + 1), 3];
        }
        else
        {
            uv00 = blockUVs[(int)(element + 1), 0];
            uv10 = blockUVs[(int)(element + 1), 1];
            uv01 = blockUVs[(int)(element + 1), 2];
            uv11 = blockUVs[(int)(element + 1), 3];
        }

        switch (side)
        {
        case Cubeside.BOTTOM:
            vertices = new Vector3[] { p0, p1, p2, p3 };
            normals  = new Vector3[] {
                Vector3.down,
                Vector3.down,
                Vector3.down,
                Vector3.down
            };
            break;

        case Cubeside.TOP:
            vertices = new Vector3[] { p7, p6, p5, p4 };
            normals  = new Vector3[] {
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up
            };
            break;

        case Cubeside.LEFT:
            vertices = new Vector3[] { p7, p4, p0, p3 };
            normals  = new Vector3[] {
                Vector3.left,
                Vector3.left,
                Vector3.left,
                Vector3.left
            };
            break;

        case Cubeside.RIGHT:
            vertices = new Vector3[] { p5, p6, p2, p1 };
            normals  = new Vector3[] {
                Vector3.right,
                Vector3.right,
                Vector3.right,
                Vector3.right
            };
            break;

        case Cubeside.FRONT:
            vertices = new Vector3[] { p4, p5, p1, p0 };
            normals  = new Vector3[] {
                Vector3.forward,
                Vector3.forward,
                Vector3.forward,
                Vector3.forward
            };
            break;

        case Cubeside.BACK:
            vertices = new Vector3[] { p6, p7, p3, p2 };
            normals  = new Vector3[] {
                Vector3.back,
                Vector3.back,
                Vector3.back,
                Vector3.back
            };
            break;
        }

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.triangles = triangles;

        mesh.uv = new Vector2[] { uv11, uv01, uv00, uv10 };

        //set cracks if necessary
        if (healthrating[(int)element] != (int)element)
        {
            List <Vector2> suvs = new List <Vector2>();

            suvs.Add(decalUVs[(int)(health + 1), 3]);
            suvs.Add(decalUVs[(int)(health + 1), 2]);
            suvs.Add(decalUVs[(int)(health + 1), 1]);
            suvs.Add(decalUVs[(int)(health + 1), 0]);

            mesh.SetUVs(1, suvs);
        }

        mesh.RecalculateBounds();
    }