前面基础的讲到了一些网格的学习,现在我们来做一些特别的东西,先绘制一个正方体。
首先我们需要绘制8个点,我们依次添加8个点,
Verticles = new Vector3[]
{
new Vector3(0,0,0),new Vector3(1,0,0),new Vector3(0,1,0),new Vector3(1,1,0),
new Vector3(0,0,1),new Vector3(1,0,1),new Vector3(0,1,1),new Vector3(1,1,1),
};
添加完点后,接下来就是指定他们的绘制顺序:
int[] triangles = new int[36];
triangles[0 + 6 * 0] = 0; triangles[1 + 6 * 0] = 2; triangles[2 + 6 * 0] = 1;
triangles[3 + 6 * 0] = 1; triangles[4 + 6 * 0] = 2; triangles[5 + 6 * 0] = 3;
triangles[0 + 6 * 1] = 4; triangles[1 + 6 * 1] = 5; triangles[2 + 6 * 1] = 6;
triangles[3 + 6 * 1] = 5; triangles[4 + 6 * 1] = 7; triangles[5 + 6 * 1] = 6;
triangles[0 + 6 * 2] = 2; triangles[1 + 6 * 2] = 6; triangles[2 + 6 * 2] = 3;
triangles[3 + 6 * 2] = 3; triangles[4 + 6 * 2] = 6; triangles[5 + 6 * 2] = 7;
triangles[0 + 6 * 3] = 0; triangles[1 + 6 * 3] = 1; triangles[2 + 6 * 3] = 4;
triangles[3 + 6 * 3] = 1; triangles[4 + 6 * 3] = 5; triangles[5 + 6 * 3] = 4;
triangles[0 + 6 * 4] = 1; triangles[1 + 6 * 4] = 3; triangles[2 + 6 * 4] = 5;
triangles[3 + 6 * 4] = 5; triangles[4 + 6 * 4] = 3; triangles[5 + 6 * 4] = 7;
triangles[0 + 6 * 5] = 0; triangles[1 + 6 * 5] = 4; triangles[2 + 6 * 5] = 2;
triangles[3 + 6 * 5] = 4; triangles[4 + 6 * 5] = 6; triangles[5 + 6 * 5] = 2;
mymesh.triangles = triangles;
this.GetComponent<MeshFilter>().mesh = mymesh;
这里我没有贴uv,因为uv要每一个面的贴,当然也很简单,你们可以自行去贴。这样就可以看见一个白色的正方体,因为这是还没有uv和材质所以它是个白色的,啥也没有。正方体做完了,我们接下来还做一个有趣的,不知道有没有人玩过那种2D地图是非常好看的曲面,如下图:
这里这个原图片是个长方形的,我们知道这个怎么做了,我们做个那种2D山坡地形还难吗?
你们应该知道微积分,知道曲线是怎么来的吧,简单的说一下,在数学上,曲线是由无数个点,我们知道在计算机里面是没法做个到无限个,只能尽可能多的去无限接近它,所以假如我们一个10米的地形,我们有1000个长方形组成,那么每个长方形的宽度那么就只有0.01那么大了是吧,那么再手机上这样的屏幕上看上去就是曲线的,了解了它的原理之后,我们来聊一下,怎么在这个曲线上采样,这里我用的是简单的sin函数来取样的,当然你也可以用贝塞尔曲线(插值)或者catmul曲线进行采样,最后就是给每个长方形添加碰撞器了。运用这个原理我们可以做那种2D的水(游戏邦上面一篇叫做”2D动态水的制作“的文章),代码如下:
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class CreateWater : MonoBehaviour
{
// Use this for initialization
private Mesh _waterMesh;
private List<Vector3> _verticles;
private List<int> _trangels;
private List<Vector2> _uvList;
private MeshFilter _meshFilter;
private void Awake()
{
_meshFilter = this.GetComponent<MeshFilter>();
}
private void Start ()
{
GenerateMesh(0,Mathf.PI*2,-2,0.01f);
}
private void GenerateMesh(float left,float right,float bottom,float offset)
{
_verticles=new List<Vector3>();
_trangels=new List<int>();
_uvList=new List<Vector2>();
_waterMesh=new Mesh();
for (float i = left; i <= right; )
{
Vector3 leftPoint = new Vector3(i,bottom,0);
i += offset;
_verticles.Add(leftPoint);
float b = (i - left)/(right - left);
_uvList.Add(new Vector2(b,0));
}
for (float i = left; i <= right; )
{
float topY = Mathf.Sin(i);
float b = (i - left) / (right - left);
_uvList.Add(new Vector2(b, 1));
Vector3 leftTopPoint=new Vector3(i,topY,0);
i += offset;
b = (i - left) / (right - left);
Vector3 rightTopPoint = new Vector3(i, topY, 0);
_uvList.Add(new Vector2(b, 1));
_verticles.Add(leftTopPoint);
_verticles.Add(rightTopPoint);
}
_meshFilter.mesh.vertices = _verticles.ToArray();
int totalTrangle =(int)((right - left)/offset);
for (int i = 0; i < totalTrangle; i++)
{
_trangels.Add(i);
_trangels.Add(totalTrangle + 1 + 2 * i);
_trangels.Add(totalTrangle + 1 + 2 * i+1);
_trangels.Add(i);
_trangels.Add(totalTrangle + 1 + 2 * i+1);
_trangels.Add(i+1);
}
_meshFilter.mesh.triangles = _trangels.ToArray();
_meshFilter.mesh.uv = _uvList.ToArray();
}
}
下节我们讲一下简单的网格切分和组合。