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

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值