0
我發現了一段時間沒有更新的舊教程,並按照它創建了一些1D平面地形。它使用貝塞爾曲線生成表面,然後作爲土地。如何將額外的頂點添加到程序生成的平面?
但是,這隻會創建兩組頂點 - 一個在底部,一個在頂部。我想填充它以便生成更多的立方體形狀,但我不確定在哪裏添加其他頂點。另一個目標是在頂部和底部之間添加更多頂點以創建更堅固的形狀。我應該在哪裏添加額外的頂點循環以創建類似立方體的形狀,從而爲網格提供更高的分辨率和深度?
using UnityEngine;
using System.Collections.Generic;
public class TerrainGenerator : MonoBehaviour
{
public Vector3[] meshPoints = null;
private Mesh _mesh = null;
public List<Vector3> vertices = new List<Vector3>();
private List<int> triangles = new List<int>();
private MeshCollider _collider;
private MeshFilter _filter;
private float terrainSize = 0.4f;
public LandTypes type;
public float lastHeight = 3;
void Awake()
{
_collider = GetComponent<MeshCollider>();
_filter = GetComponent<MeshFilter>();
}
public void GenerateMesh(float lh, LandTypes Type)
{
type = Type;
_mesh = _filter.mesh;
_mesh.Clear();
meshPoints = new Vector3[4];
switch(Type)
{
case LandTypes.Flat:
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh, 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh, 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh, 0f);
break;
case LandTypes.Up:
int typeOfUpChance = Random.Range(1, 20);
if (typeOfUpChance > 10)
{
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh + 1, 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh + 2, 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh + 3, 0f);
} else
{
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh + Random.Range(2, 3), 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh + Random.Range(2, 4), 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh + 5, 0f);
}
break;
case LandTypes.Down:
if (lh > 6f)
{
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh - 2, 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh - 3, 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh - 4, 0f);
}
else
{
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh, 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh, 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh, 0f);
}
break;
case LandTypes.Hill:
meshPoints[0] = new Vector3(terrainSize * (float)0, lh, 0f);
meshPoints[1] = new Vector3(terrainSize * (float)1, lh + 1.5f, 0f);
meshPoints[2] = new Vector3(terrainSize * (float)2, lh + 1.5f, 0f);
meshPoints[3] = new Vector3(terrainSize * (float)3, lh, 0f);
break;
}
LandController.Instance.HeightCounts.Add(meshPoints[3].y);
LandController.Instance.lastHeight = meshPoints[3].y;
int resolution = 8;
for (int i = 0; i < resolution; i++)
{
float t = (float)i/(float)(resolution - 1);
Vector3 p = CalculateBezierPoint(t, meshPoints[0], meshPoints[1], meshPoints[2], meshPoints[3]);
AddTerrainPoint(p);
}
_mesh.vertices = vertices.ToArray();
_mesh.triangles = triangles.ToArray();
_mesh.RecalculateBounds();
_mesh.RecalculateNormals();
_collider.sharedMesh = _mesh;
}
void AddTerrainPoint(Vector3 point)
{
vertices.Add(new Vector3(point.x, 0f, 0f));
vertices.Add(point);
if (vertices.Count >= 4)
{
int start = vertices.Count - 4;
triangles.Add(start + 0);
triangles.Add(start + 1);
triangles.Add(start + 2);
triangles.Add(start + 1);
triangles.Add(start + 3);
triangles.Add(start + 2);
}
}
private Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * tt * p2;
p += ttt * p3;
return p;
}
}