- Home /
Question by
sorasnobody · Apr 21, 2015 at 02:35 PM ·
mathproceduralprocedural mesh
Struggling with procedural mesh math
Hello! I've been struggling for the last few days to make a procedurally generated road. The road script has a function that makes it turn at a given angle, and a function that lets it go forward at the given angle. My problem arises when trying to make a right turn after having already moved forward. I can't for the life of me figure out the issue, but I must be messing up the x and y coordinates somewhere.
using UnityEngine;
using System.Collections;
public class procPlane : MonoBehaviour {
public Mesh mesh;
public Vector3[] vertices;
public int currentVertice;
public Vector3[] normales;
public Vector2[] uvs;
public int[] triangles;
public int currentTriangle;
//current vertice number
public int verticeNumber = 4;
//current x and z outer values.
//values for the outer vertice
public float x_out = -0.5f;
public float z_out = 0.1f;
public float angle = 0.0f;
//values for the inner vertice
private float x_in = 0.5f;
public float z_in = 0.1f;
public float xi=0.5f, xo=-0.1f, zi=0.1f, zo=0.1f;
// Use this for initialization
void Start () {
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
mesh = filter.mesh;
mesh.Clear();
#region Vertices
vertices = new Vector3[1000];
currentVertice = 0;
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, -0.1f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, -0.1f);
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.0f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.0f);
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.1f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.1f);
#endregion
#region Normales
normales = new Vector3[ vertices.Length ];
for( int n = 0; n < normales.Length; n++ )
normales[n] = Vector3.up;
#endregion
#region Triangles
triangles = new int[2*6*100];
currentTriangle = 0;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 1;
triangles[currentTriangle++] = 0;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 3;
triangles[currentTriangle++] = 1;
triangles[currentTriangle++] = 4;
triangles[currentTriangle++] = 3;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 4;
triangles[currentTriangle++] = 5;
triangles[currentTriangle++] = 3;
#endregion
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.UpArrow))
{
//Translating vertices forward at given angle
Vector3 A = Quaternion.Euler(0,angle,0)*Vector3.forward*0.1f;
x_out+=A.x;
x_in+=A.x;
z_in += A.z;
z_out += A.z;
Debug.LogWarning(z_in);
xo += A.x;
xi += A.x;
zo += A.z;
zi += A.z;
vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
//normals
normales[currentVertice] = Vector3.up;
//triangles
verticeNumber += 2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber-1;
triangles[currentTriangle++] = verticeNumber-2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber+1;
triangles[currentTriangle++] = verticeNumber-1;
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
//z_out += z_out;
//z_in += z_in;
angle += 9.0f;
//calculating x and y positions on the outer edge of the bend.
float x_outer = (float)-(2.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
float z_outer = (float)(2.0f * Mathf.Sin(angle*Mathf.PI/180.0f));
x_out = x_outer;
z_out = zo + z_outer;
//calculating x and y positions on the inner edge of the bend.
float x_inner = (float)-(1.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
float z_inner = (float)(1.0f * Mathf.Sin(angle*Mathf.PI/180.0f))-0.1f;
x_in = x_inner;
z_in = zi + z_inner;
Debug.LogWarning(zi);
Debug.LogWarning(z_inner);
//outer vertice
vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
//inner vertice
vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
//normals
normales[currentVertice] = Vector3.up;
//triangles
verticeNumber += 2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber-1;
triangles[currentTriangle++] = verticeNumber-2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber+1;
triangles[currentTriangle++] = verticeNumber-1;
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Adding extra materials to a mesh programmatically 0 Answers
Making a Procedural Cube Mesh 1 Answer
C# Proceducal Mesh terrain 2 Answers
Sphere made of cubes algorithm 4 Answers
How to avoid Memory allocation on procedural Mesh generation 1 Answer