共计 4207 个字符,预计需要花费 11 分钟才能阅读完成。
生成方块盒子的 mesh,由底面的四个顶点和顶面的四个顶点组成,底面的 Mesh 是朝下的,所以底面的四个顶点以逆时针的形式排列,顶面的四个顶点还是以顺时针顺序排列。
1、取得顶点的数据汇合
protected override Vector3[] GetVertices()
{var p0 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 左下
var p1 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 右下
var p2 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 右上
var p3 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 左上
var p4 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 左下
var p5 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 左上
var p6 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 右上
var p7 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 右下
return new Vector3[]
{
// 下
p1,p2,p3,p0,
// 上
p4,p5,p6,p7,
// 左
p3,p5,p4,p0,
// 右
p1,p7,p6,p2,
// 前
p2,p6,p5,p3,
// 后
p0,p4,p7,p1
};
}
2、取得三角面顶点的索引
protected override int[] GetTriangles()
{return new int[]
{
// 下
0, 1, 2,
2, 3, 0,
// 上
0 + 4 * 1, 1 + 4 * 1, 2 + 4 * 1,
2 + 4 * 1, 3 + 4 * 1, 0 + 4 * 1,
// 左
0 + 4 * 2, 1 + 4 * 2, 2 + 4 * 2,
2 + 4 * 2, 3 + 4 * 2, 0 + 4 * 2,
// 右
0 + 4 * 3, 1 + 4 * 3, 2 + 4 * 3,
2 + 4 * 3, 3 + 4 * 3, 0 + 4 * 3,
// 前
0 + 4 * 4, 1 + 4 * 4, 2 + 4 * 4,
2 + 4 * 4, 3 + 4 * 4, 0 + 4 * 4,
// 后
0 + 4 * 5, 1 + 4 * 5, 2 + 4 * 5,
2 + 4 * 5, 3 + 4 * 5, 0 + 4 * 5,
};
}
BoxShape 整体代码如下
public class BoxShape : Shape
{
private static readonly string _boxMeshName = "Box Mesh";//mesh 的名称
#region private members
private float _xSize, _ySize, _zSize;
#endregion
#region ctor
public BoxShape(float xSize, float ySize, float zSize, MeshPivot meshPivot) : base(meshPivot, _boxMeshName)
{
_xSize = xSize;
_ySize = ySize;
_zSize = zSize;
_verticeOffset = GetVertexOffset();}
#endregion
#region override functions
/// <summary>
/// 依据 mesh 中心点的地位,取得顶点地位的偏移量
/// </summary>
/// <returns></returns>
protected override Vector3 GetVertexOffset()
{
Vector3 offset;
switch (_meshPivot)
{
case MeshPivot.CENTER:
offset = Vector3.zero;
break;
case MeshPivot.TOP:
offset = new Vector3(0, _ySize, 0) * 0.5f;
break;
case MeshPivot.BOTTOM:
offset = new Vector3(0, -_ySize, 0) * 0.5f;
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;
}
/// <summary>
/// 取得顶点的数据汇合
/// </summary>
/// <returns></returns>
protected override Vector3[] GetVertices()
{var p0 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 左下
var p1 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 右下
var p2 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 右上
var p3 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 左上
var p4 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 左下
var p5 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 左上
var p6 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _verticeOffset;// 右上
var p7 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _verticeOffset;// 右下
return new Vector3[]
{
// 下
p1,p2,p3,p0,
// 上
p4,p5,p6,p7,
// 左
p3,p5,p4,p0,
// 右
p1,p7,p6,p2,
// 前
p2,p6,p5,p3,
// 后
p0,p4,p7,p1
};
}
/// <summary>
/// 取得法线方向的数据汇合
/// </summary>
/// <returns></returns>
protected override Vector3[] GetNormals()
{return new Vector3[]
{
// 下
Vector3.down,Vector3.down,Vector3.down,Vector3.down,
// 上
Vector3.up, Vector3.up, Vector3.up, Vector3.up,
// 左
Vector3.left,Vector3.left,Vector3.left,Vector3.left,
// 右
Vector3.right,Vector3.right,Vector3.right,Vector3.right,
// 前
Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward,
// 后
Vector3.back,Vector3.back,Vector3.back,Vector3.back,
};
}
/// <summary>
/// 取得三角面顶点的索引
/// </summary>
/// <returns></returns>
protected override int[] GetTriangles()
{return new int[]
{
// 下
0, 1, 2,
2, 3, 0,
// 上
0 + 4 * 1, 1 + 4 * 1, 2 + 4 * 1,
2 + 4 * 1, 3 + 4 * 1, 0 + 4 * 1,
// 左
0 + 4 * 2, 1 + 4 * 2, 2 + 4 * 2,
2 + 4 * 2, 3 + 4 * 2, 0 + 4 * 2,
// 右
0 + 4 * 3, 1 + 4 * 3, 2 + 4 * 3,
2 + 4 * 3, 3 + 4 * 3, 0 + 4 * 3,
// 前
0 + 4 * 4, 1 + 4 * 4, 2 + 4 * 4,
2 + 4 * 4, 3 + 4 * 4, 0 + 4 * 4,
// 后
0 + 4 * 5, 1 + 4 * 5, 2 + 4 * 5,
2 + 4 * 5, 3 + 4 * 5, 0 + 4 * 5,
};
}
/// <summary>
/// 取得 UV 坐标的数据汇合
/// </summary>
/// <returns></returns>
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),
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),
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
}
正文完