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

发表于2019-01-17
评论0 7.4k浏览
游戏都是有很多场景组成的,像一些河流、山丘等等,这样一款游戏的元素才足够丰富,也才能吸引到玩家。就像本篇文章要给大家分享的开发过程中程序生成mesh并实现2D山丘地形,一起来看看吧。

首先看一下最终效果:

这里我假设读者已经了解并知道如何使用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。

最后运行场景就能看到文章最开始的效果了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引