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

Unity使用bounds绘制不规则图形边框

2020-06-30 17:25 513 查看

Unity使用bounds绘制不规则图形边框
for (int i = 0; i < screenList.Count; i++)
{
if (screenList[i].activeSelf == true)
{
Bounds bounds = screenList[i].GetComponent().mesh.bounds;
float x = bounds.size.x * screenList[i].transform.localScale.x;
// float y = bounds.size.y * screenList[i].transform.localScale.y;
float z = bounds.size.z * screenList[i].transform.localScale.z;
//计算矩形框顶点位置
Vector3 leftUp = new Vector3(bounds.center.x - x / 2, 0, bounds.center.z + z / 2);
Vector3 rightUp = new Vector3(bounds.center.x + x / 2, 0, bounds.center.z + z / 2);
Vector3 rightDown = new Vector3(bounds.center.x + x / 2, 0, bounds.center.z - z / 2);
Vector3 leftDown = new Vector3(bounds.center.x - x / 2, 0, bounds.center.z - z / 2);
//旋转后的矩形框顶点
Vector3 lur = RotateRound(leftUp, screenList[i].transform.position, Vector3.up, screenList[i].transform.rotation.eulerAngles.y);
Vector3 rur = RotateRound(rightUp, screenList[i].transform.position, Vector3.up, screenList[i].transform.rotation.eulerAngles.y);
Vector3 rdr = RotateRound(rightDown, screenList[i].transform.position, Vector3.up, screenList[i].transform.rotation.eulerAngles.y);
Vector3 ldr = RotateRound(leftDown, screenList[i].transform.position, Vector3.up, screenList[i].transform.rotation.eulerAngles.y);
//平移后的矩形框顶点
Vector3 lu = new Vector3(lur.x + screenList[i].transform.position.x, 0, lur.z + screenList[i].transform.position.z);
Vector3 ru = new Vector3(rur.x + screenList[i].transform.position.x, 0, rur.z + screenList[i].transform.position.z);
Vector3 rd = new Vector3(rdr.x + screenList[i].transform.position.x, 0, rdr.z + screenList[i].transform.position.z);
Vector3 ld = new Vector3(ldr.x + screenList[i].transform.position.x, 0, ldr.z + screenList[i].transform.position.z);
//显示矩形框
Debug.DrawLine(ld, lu, Color.green);
Debug.DrawLine(ld, rd, Color.green);
Debug.DrawLine(ru, lu, Color.green);
Debug.DrawLine(ru, rd, Color.green);
}

///
/// 围绕某点旋转指定角度
///
/// 自身坐标
/// 旋转中心
/// 围绕旋转轴
/// 旋转角度
///
private Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
{
return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: