C# 程序网格数学
在过去的几天里,我一直在努力制作一个可以弯曲的程序生成的网格,也就是道路。“道路脚本”有一个使其以给定角度转弯的函数,还有一个使其以给定角度前进的函数。如下图所示,我的问题出现在已经向前行驶后试图右转时 我一辈子都无法解决代码中的问题,但我假设我在某个地方用x和y坐标犯了一个会计错误C# 程序网格数学,c#,math,unity3d,procedural-generation,C#,Math,Unity3d,Procedural Generation,在过去的几天里,我一直在努力制作一个可以弯曲的程序生成的网格,也就是道路。“道路脚本”有一个使其以给定角度转弯的函数,还有一个使其以给定角度前进的函数。如下图所示,我的问题出现在已经向前行驶后试图右转时 我一辈子都无法解决代码中的问题,但我假设我在某个地方用x和y坐标犯了一个会计错误 using UnityEngine; using System.Collections; public class procPlane : MonoBehaviour { public Mesh me
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.
public float x_out = -0.5f;
public float z_out = 0.1f;
public float angle = 0.0f;
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 () {
// You can change that line to provide another MeshFilter
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))
{
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;
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;
//Debug.LogWarning(z_out);
z_out = zo + z_outer;
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_in - (x_inner+(x_out));
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();
}
}
}
不确定,但使用Quaternion.Euler可能会遇到万向节锁问题。当数学陷入混乱时,检查你的角度 检查一下这个
Ryan这个问题可能太宽泛了,所以…线框截图可能更有用。试试四叉树算法。事情不是这样的。