您的位置:首页 > 移动开发 > Unity3D

unity 程序生成mesh并实现2D山丘地形

2018-02-10 21:49 525 查看
首先看一下最终效果:



这里我假设读者已经了解并知道如何使用Mesh。如果不懂的话可以百度一下,有很多文章。
实现起来其实非常简单,思路:
每两个顶点为一组,这两个点的x坐标相同,一个在上一个在下,下边的y坐标固定为0,上边的y坐标从曲线编辑器中取值。
然后把生成的mesh赋予物体的MeshCollider。

下面给出完整代码:using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshMount : MonoBehaviour {
//每2个定点为一组,此值代表有多少组
const int count = 50;
//每两组顶点的间隔距离,此值越小曲线越平滑
const float pointdis = 0.2f;

Material mat;
MeshCollider mc;

//曲线
public AnimationCurve anim;

// Use this for initialization
void Start () {

mat = Resources.Load<Material>("Shader/MeshMount/Custom_meshshader");
mc = gameObject.AddComponent<MeshCollider>();

MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
mr.material = mat;

DrawSquare();
}

// Update is called once per frame
void Update () {

}

void DrawSquare()
{
//创建mesh
Mesh mesh = gameObject.AddComponent<MeshFilter>().mesh;
mesh.Clear();

//定义顶点列表
List<Vector3> pointList = new List<Vector3>();
//uv列表
List<Vector2> uvList = new List<Vector2>();
//第一列的前2个点直接初始化好
pointList.Add(new Vector3(0, 0, 0));
pointList.Add(new Vector3(0, 1, 0));

//设置前2个点的uv
uvList.Add(new Vector2(0, 0));
uvList.Add(new Vector2(0,1));

//三角形数组
List<int> triangleList = new List<int>();

for (int i = 0; i < count; i ++){
//计算当前列位于什么位置
float rate = (float)i / (float)count;
//计算当前的顶点
pointList.Add(new Vector3((i + 1)*pointdis, 0, 0));

//这里从曲线函数中取出当点的高度
pointList.Add(new Vector3((i + 1)*pointdis,anim.Evaluate(rate), 0));

//uv直接使用rate即可
uvList.Add(new Vector2(rate, 0));
uvList.Add(new Vector2(rate, 1));

//计算当前2个点与前面2个点组成的2个三角形
int startindex = i * 2;
triangleList.Add(startindex + 0);
triangleList.Add(startindex + 1);
triangleList.Add(startindex + 2);

triangleList.Add(startindex + 3);
triangleList.Add(startindex + 2);
triangleList.Add(startindex + 1);
}

//把最终的顶点和三角形数组赋予mesh;
mesh.vertices = pointList.ToArray();
mesh.triangles = triangleList.ToArray();
mesh.uv = uvList.ToArray();
mesh.RecalculateNormals();

//把mesh赋予MeshCollider
mc.sharedMesh = mesh;
}
}


使用方法:
首先创建一个空物体,把脚本挂到此物体上,然后在Inspector面板中双击anmi,编辑出一个喜欢的曲线形状。注意横轴只有在[0,1]的范围内才有效。
接下来创建一个球体并赋予rigidbode。
最后运行场景就能看到文章最开始的效果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  untiy mesh 山丘地形