Unity显示物体的边框

注释

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

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

相关推荐

  1. Unity显示物体边框

    2024-01-29 13:30:04       31 阅读
  2. unity中UI、shader显示在3D物体

    2024-01-29 13:30:04       28 阅读
  3. unity实时保存物体坐标信息txt

    2024-01-29 13:30:04       45 阅读
  4. Unity获取相机渲染范围内所有物体

    2024-01-29 13:30:04       30 阅读
  5. unity中 canvas下物体朝向跟随

    2024-01-29 13:30:04       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 13:30:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 13:30:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 13:30:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 13:30:04       20 阅读

热门阅读

  1. 面试经典题---30.串联所有单词的子串

    2024-01-29 13:30:04       30 阅读
  2. 2024/1/28 备战蓝桥杯 1-3

    2024-01-29 13:30:04       34 阅读
  3. python—基础知识补充

    2024-01-29 13:30:04       31 阅读
  4. 网络爬虫的基本原理、应用场景及注意事项

    2024-01-29 13:30:04       34 阅读
  5. Unity2D_角色移动&跳跃

    2024-01-29 13:30:04       35 阅读
  6. Uboot中ARMV7和ARMV8 MMU配置

    2024-01-29 13:30:04       23 阅读
  7. 【微信小程序框架的详细介绍】

    2024-01-29 13:30:04       34 阅读
  8. 面经——面试中常问到的计算机网络相关问题

    2024-01-29 13:30:04       36 阅读