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.