面板Mesh主要由四个点组成,这里生成的是双面Plane,所以需要四个点的坐标。
顶面顶点的顺序如图,以p0点和p2点为对角线切割成两个三角面
代码如下
public class PlaneShape : Shape
{
private static readonly string _planeMeshName = "Plane Mesh";//mesh的名称
#region private members
private float _xSize, _zSize;
#endregion
#region ctor
public PlaneShape(float xSize,float zSize,MeshPivot meshPivot = MeshPivot.CENTER) : base(meshPivot, _planeMeshName)
{
_xSize = xSize;
_zSize = zSize;
_verticeOffset = GetVertexOffset();
}
#endregion
#region override functions
///
/// 根据mesh中心点的位置,获得顶点位置的偏移量
///
///
protected override Vector3 GetVertexOffset()
{
Vector3 offset;
switch (_meshPivot)
{
case MeshPivot.CENTER:
case MeshPivot.TOP:
case MeshPivot.BOTTOM:
offset = Vector3.zero;
break;
case MeshPivot.LEFT:
offset = new Vector3(-_xSize, 0, 0) * 0.5f;
break;
case MeshPivot.RIGHT:
offset = new Vector3(_xSize, 0, 0) * 0.5f;
break;
case MeshPivot.FRONT:
offset = new Vector3(0, 0, _zSize) * 0.5f;
break;
case MeshPivot.BACK:
offset = new Vector3(0, 0, -_zSize) * 0.5f;
break;
default:
offset = Vector3.zero;
break;
}
return offset;
}
///
/// 获得顶点的数据集合
///
///
protected override Vector3[] GetVertices()
{
var p0 = new Vector3(-_xSize, 0, -_zSize) * 0.5f - _verticeOffset;//左下
var p1 = new Vector3(-_xSize, 0, _zSize) * 0.5f - _verticeOffset;//左上
var p2 = new Vector3(_xSize, 0, _zSize) * 0.5f - _verticeOffset;//右上
var p3 = new Vector3(_xSize, 0, -_zSize) * 0.5f - _verticeOffset;//右下
return new Vector3[]
{
p0,p1,p2,p3,
p3,p2,p1,p0
};
}
///
/// 获得法线方向的数据集合
///
///
protected override Vector3[] GetNormals()
{
return new Vector3[]
{
Vector3.up,Vector3.up,Vector3.up,Vector3.up,
Vector3.down,Vector3.down,Vector3.down,Vector3.down
};
}
///
/// 获得三角面顶点的索引
///
///
protected override int[] GetTriangles()
{
return new int[]
{
0,1,2,2,3,0,
4,5,6,6,7,4
};
}
///
/// 获得UV坐标的数据集合
///
///
protected override Vector2[] GetUVs()
{
return new Vector2[]
{
new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
};
}
#endregion
}