Unity显示物体的边框

文章介绍了如何使用Unity中的脚本,在OnRenderObject事件中获取物体的碰撞体,并使用OpenGL绘制其轮廓,展示了创建和配置线条材质的过程。
摘要由CSDN通过智能技术生成

注释

将脚本挂在物体上即可,画的是当前物体的碰撞体轮廓

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
public class ShowBoxCollider : MonoBehaviour
{
 void OnRenderObject()
 {
  var colliders = gameObject.GetComponents<Collider>();
  if (colliders == null)
  {
   return;
  }
  //创建并设置线条材质
  CreateLineMaterial();
  lineMaterial.SetPass(0);
  GL.PushMatrix();
  //这里无需将矩阵从本地坐标转化为世界左边
  //GL.MultMatrix(transform.localToWorldMatrix);
 
  for (int i = 0; i < colliders.Length; i++)
  {
   var col = colliders[i];
   //获取本物体对象在世界范围内的中心点位置 col.center是本地坐标位置
   var c = col.bounds.center;
   //collider大小
   var size = col.bounds.size;
   float rx = size.x / 2f;
   float ry = size.y / 2f;
   float rz = size.z / 2f;
   //获取collider边界的8个顶点位置
   Vector3 p0, p1, p2, p3;
   Vector3 p4, p5, p6, p7;
   p0 = c + new Vector3(-rx, -ry, rz);
   p1 = c + new Vector3(rx, -ry, rz);
   p2 = c + new Vector3(rx, -ry, -rz);
   p3 = c + new Vector3(-rx, -ry, -rz);
 
   p4 = c + new Vector3(-rx, ry, rz);
   p5 = c + new Vector3(rx, ry, rz);
   p6 = c + new Vector3(rx, ry, -rz);
   p7 = c + new Vector3(-rx, ry, -rz);
 
   //画线
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p0);
   GL.Vertex(p1);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p1);
   GL.Vertex(p2);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p2);
   GL.Vertex(p3);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p0);
   GL.Vertex(p3);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p4);
   GL.Vertex(p5);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p5);
   GL.Vertex(p6);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p6);
   GL.Vertex(p7);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p4);
   GL.Vertex(p7);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p0);
   GL.Vertex(p4);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p1);
   GL.Vertex(p5);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p2);
   GL.Vertex(p6);
   GL.End();
 
   GL.Begin(GL.LINES);
   GL.Color(Color.cyan);
   GL.Vertex(p3);
   GL.Vertex(p7);
   GL.End();
  }
  GL.PopMatrix();
 }
 
 static Material lineMaterial;
 static void CreateLineMaterial()
 {
  if (!lineMaterial)
  {
   // Unity3d使用该默认的Shader作为线条材质 
   Shader shader = Shader.Find("Hidden/Internal-Colored");
   lineMaterial = new Material(shader);
   lineMaterial.hideFlags = HideFlags.HideAndDontSave;
   // 开启 alpha blending 
   lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
   lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
   // 开启背面遮挡 
   lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
   // Turn off depth writes 
   lineMaterial.SetInt("_ZWrite", 0);
  }
 }
}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值