How can i manipulate vertices in plane mesh ?

In this script below i can create meshes. But it’s not so easy to understand all the positions and faces.
My goal is to create a plane out of meshes and then manipulate vertices in the meshes that create the plane.

What i want to do is to create some enum states or enum parameters to create meshes more easy with this script. For example to make enum parameter called: Plane. And This Plane will have some properties for example: Width and Height and maybe also Vertices.

Then i want somehow more easy to be able to change the plane vertices on the Z to push a vertice/s down/deep.
Like: z - 20 or maybe: - z not sure. But the idea is to take the script and to add to it some enums so ican use it more easy with another script to create meshes like plane and others.

This script manipulate vertices on the meshes but i’m not sure how to create a plane and how to manipulate vertices on it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class buildMesh : MonoBehaviour
{
    public Vector3 vertLeftTopFront = new Vector3(-1, 1, 1);
    public Vector3 vertRightTopFront = new Vector3(1, 1, 1);
    public Vector3 vertRightTopBack = new Vector3(1, 1, -1);
    public Vector3 vertLeftTopBack = new Vector3(-1, 1, -1);

    private float waitN = 3f;
    private float waitD = 3f;
    public int shapeN = 0;

    // Use this for initialization
    void Start()
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        Mesh mesh = mf.mesh;

        //Vertices

        Vector3[] vertices = new Vector3[]
        {
            //Front face
            vertLeftTopFront, // left top front, 0
            vertRightTopFront, // right top front, 1
            new Vector3(-1,-1,1), // left bottom front, 2
            new Vector3(1,-1,1), // right bottom front, 3

            //Back face
            vertRightTopBack, //right top back, 4
            vertLeftTopBack, //left top back, 5
            new Vector3(1,-1,-1), //right bottom back, 6
            new Vector3(-1,-1,-1), //left bottom back, 7

            //Left face
            vertLeftTopBack, //left top back, 8
            vertLeftTopFront, //left top front, 9
            new Vector3(-1,-1,-1), //left bottom back, 10
            new Vector3(-1,-1,1), //left bottom front, 11

            //Right face
            vertRightTopFront, //right top front, 12
            vertRightTopBack, //right top back, 13
            new Vector3(1,-1,1), //right bottom front, 14
            new Vector3(1,-1,-1), //right bottom back, 15

            //Top face
            vertLeftTopBack, //left top back, 16
            vertRightTopBack, //right top back, 17
            vertLeftTopFront, //left top front, 18
            vertRightTopFront, //right top front, 19

            //Bottom face
            new Vector3(-1,-1,1), //left bottom front, 20
            new Vector3(1,-1,1), //right bottom front, 21
            new Vector3(-1,-1,-1), //left bottom back, 22
            new Vector3(1,-1,-1), //right bottom back, 23
        };

        //Triangles // 3 points, clockwise determines wich side is visible

        int[] triangles = new int[]
        {
            //Front face//
            0,2,3,//first triangle
            3,1,0,//second triangle

            //Back face//
            4,6,7,//first triangle
            7,5,4,//second triangle

            //Left face//
            8,10,11,//first triangle
            11,9,8,//second triangle

            //Right face//
            12,14,15,//first triangle
            15,13,12,//second triangle

            //Top face//
            16,18,19,//first triangle
            19,17,16,//second triangle

            //Bottom face//
            20,22,23,//first triangle
            23,21,20//second triangle
        };

        //UVs

        List<Vector2> uvs = new List<Vector2>();
        for (int i = 0; i < vertices.Length / 4; i++)
        {
            uvs.Add(new Vector2(0, 1));
            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(1, 1));
            uvs.Add(new Vector2(1, 0));
        }
        Vector2[] uv = uvs.ToArray();

        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uv;
        mesh.RecalculateNormals();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            if (waitN > 0f)
            {
                waitN -= Time.deltaTime;
            }
            else
            {
                waitN = waitD;
                shapeN++;
                if (shapeN > 3)
                {
                    shapeN = 0;
                }
            }

            //morph to cube
            if (shapeN == 0)
            {
                vertLeftTopFront = Vector3.Lerp(vertLeftTopFront, new Vector3(-1, 1, 1), Time.deltaTime);
                vertRightTopFront = Vector3.Lerp(vertRightTopFront, new Vector3(1, 1, 1), Time.deltaTime);
                vertRightTopBack = Vector3.Lerp(vertRightTopBack, new Vector3(1, 1, -1), Time.deltaTime);
                vertLeftTopBack = Vector3.Lerp(vertLeftTopBack, new Vector3(-1, 1, -1), Time.deltaTime);
            }

            //morph to pyramid
            if (shapeN == 1)
            {
                vertLeftTopFront = Vector3.Lerp(vertLeftTopFront, new Vector3(0, 1, 0), Time.deltaTime);
                vertRightTopFront = Vector3.Lerp(vertRightTopFront, new Vector3(0, 1, 0), Time.deltaTime);
                vertRightTopBack = Vector3.Lerp(vertRightTopBack, new Vector3(0, 1, 0), Time.deltaTime);
                vertLeftTopBack = Vector3.Lerp(vertLeftTopBack, new Vector3(0, 1, 0), Time.deltaTime);
            }

            //morph to ramp
            if (shapeN == 2)
            {
                vertLeftTopFront = Vector3.Lerp(vertLeftTopFront, new Vector3(-1, -1, 2), Time.deltaTime);
                vertRightTopFront = Vector3.Lerp(vertRightTopFront, new Vector3(1, -1, 2), Time.deltaTime);
                vertRightTopBack = Vector3.Lerp(vertRightTopBack, new Vector3(1, 0.5f, -1), Time.deltaTime);
                vertLeftTopBack = Vector3.Lerp(vertLeftTopBack, new Vector3(-1, 0.5f, -1), Time.deltaTime);
            }

            //morph to roof
            if (shapeN == 3)
            {
                vertLeftTopFront = Vector3.Lerp(vertLeftTopFront, new Vector3(-1, 0.2f, 0), Time.deltaTime);
                vertRightTopFront = Vector3.Lerp(vertRightTopFront, new Vector3(1, 0.2f, 0), Time.deltaTime);
                vertRightTopBack = Vector3.Lerp(vertRightTopBack, new Vector3(1, 0.2f, 0), Time.deltaTime);
                vertLeftTopBack = Vector3.Lerp(vertLeftTopBack, new Vector3(-1, 0.2f, 0), Time.deltaTime);
            }

            Start();
        }
    }
}

If there was a way to add a text in game view when running the game around the cube and show each face and each rectangle values. To draw the numbers and text so it will be easy to understand each face and rectangles.

1 Like

Something like…

public GameObject CreateBox (float height, float width, float depth)
    {

        GameObject cube = new GameObject ();
        MeshFilter filter = cube.AddComponent<MeshFilter> ();
        MeshRenderer renderer = cube.AddComponent<MeshRenderer> ();

        Mesh mesh = new Mesh ();

        //Verts
        mesh.vertices = new Vector3[] {
            //FRONT
            new Vector3 (0, 0, 0), //BL [0][0]
            new Vector3 (width, 0, 0), //BR [1][1]
            new Vector3 (width, height, 0), //TR [2][2]
            new Vector3 (0, height, 0), //TL [3][3]

            //TOP
            new Vector3 (0, height, 0), //BL [4][0]
            new Vector3 (width, height, 0), //BR [5][1]
            new Vector3 (width, height, depth), //TR [6][2]
            new Vector3 (0, height, depth), //TL [7][3]

            //LEFT
            new Vector3 (0, 0, depth), //BL [8][0]
            new Vector3 (0, 0, 0), //BR [9][1]
            new Vector3 (0, height, 0), //TR [10][2]
            new Vector3 (0, height, depth), //TL [11][3]

            //RIGHT
            new Vector3 (width, 0, 0), //BL [12][0]
            new Vector3 (width, 0, depth), //BR [13][1]
            new Vector3 (width, height, depth), //TR [14][2]
            new Vector3 (width, height, 0), //TL [15][3]

            //BACK
            new Vector3 (width, 0, depth), //BL [16][0]
            new Vector3 (0, 0, depth), //BR [17][1]
            new Vector3 (0, height, depth), //TR [18][2]
            new Vector3 (width, height, depth), //TL [19][3]

            //BOTTOM
            new Vector3 (0, 0, depth), //BL [20][0]
            new Vector3 (width, 0, depth), //BR [21][1]
            new Vector3 (width, 0, 0), //TR [22][2]
            new Vector3 (0, 0, 0), //TL [23][3]
        };

        //UV
        mesh.uv = new Vector2[] {
            //FRONT
            new Vector2(0.25f, 0.33f),
            new Vector2(0.5f, 0.33f),
            new Vector2(0.5f,  0.66f),
            new Vector2(0.25f, 0.66f),

            //TOP
            new Vector2(0.25f, 0.66f),
            new Vector2(0.5f, 0.66f),
            new Vector2(0.5f, 1f),
            new Vector2(0.25f, 1f),

            //LEFT
            new Vector2(0, 0.33f),
            new Vector2(0.25f, 0.33f),
            new Vector2(0.25f, 0.66f),
            new Vector2(0, 0.66f),

            //RIGHT
            new Vector2(0.5f, 0.33f),
            new Vector2(0.75f, 0.33f),
            new Vector2(0.75f, 0.66f),
            new Vector2(0.5f, 0.66f),

            //BACK
            new Vector2(0.75f, 0.33f),
            new Vector2(1f, 0.33f),
            new Vector2(1f, 0.66f),
            new Vector2(0.75f, 0.66f),

            //BOTTOM
            new Vector2(0.25f, 0),
            new Vector2(0.5f, 0),
            new Vector2(0.5f, 0.33f),
            new Vector2(0.25f, 0.33f)
        };



        //Triangles
        mesh.triangles = new int[] {
            //Front
            0, 3, 2,
            0, 2, 1,

            //Top
            4, 7, 6,
            4, 6, 5,

            //Left
            8, 11, 10,
            8, 10, 9,

            //Right
            12, 15, 14,
            12, 14, 13,

            //Back
            16, 19, 18,
            16, 18, 17,

            //Bottom
            20, 23, 22,
            20, 22, 21

        };
        mesh.RecalculateNormals ();
        filter.mesh = mesh;
        renderer.material = material;
        return cube;
    }

…and you can add custom buttons to the inspector as follows…

[CustomEditor(typeof(ModelRenderer))]
    public class ObjectBuilderEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            ModelRenderer modelRenderer = (ModelRenderer)target;
            if(GUILayout.Button("Create Box"))
            {
                modelRenderer.CreateBox(modelRenderer.height, modelRenderer.width, modelRenderer.depth);
            }
        }
            
    }

Create public variables you can set in the inspector and you’re good to go.

2 Likes

This is working. But i mean something more like extrude mesh.To extrude deep down and create like path from side to side. What i mean is to create a canal/ditch kind of.

You would have to do some research to be able to fine tune what you are after.

I would suggest having a look at these tutorials :
http://catlikecoding.com/unity/tutorials/

I found his manipulation of meshes tutorials incredibly helpful.

1 Like