Unity3D實(shí)現(xiàn)描邊框效果
Unity3d描邊框效果網(wǎng)上有很多,大多是使用Shader來(lái)實(shí)現(xiàn)的
本文介紹使用Collider來(lái)實(shí)現(xiàn)這么一種效果
效果圖如下

將物體添加Collider(Box Collider、Mesh Collider......)
每個(gè)Collider都有自己的邊界Bound,描邊效果就是將Bound顯示出來(lái)
代碼如下
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;
}
//創(chuàng)建并設(shè)置線條材質(zhì)
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.PushMatrix();
//這里無(wú)需將矩陣從本地坐標(biāo)轉(zhuǎn)化為世界左邊
//GL.MultMatrix(transform.localToWorldMatrix);
for (int i = 0; i < colliders.Length; i++)
{
var col = colliders[i];
//獲取本物體對(duì)象在世界范圍內(nèi)的中心點(diǎn)位置 col.center是本地坐標(biāo)位置
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個(gè)頂點(diǎn)位置
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);
//畫(huà)線
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使用該默認(rèn)的Shader作為線條材質(zhì)
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// 開(kāi)啟 alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// 開(kāi)啟背面遮擋
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
}
使用GL將Bound的8條邊通過(guò)畫(huà)線形式渲染出來(lái)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例
這篇文章主要介紹了c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12
使用設(shè)計(jì)模式中的工廠方法模式進(jìn)行C#編程的示例講解
這篇文章主要介紹了使用設(shè)計(jì)模式中的工廠方法模式進(jìn)行C#編程的示例講解,工廠方法模式可以看作是對(duì)簡(jiǎn)單工廠模式的進(jìn)一步擴(kuò)展,需要的朋友可以參考下2016-02-02
DevExpress之ChartControl實(shí)現(xiàn)時(shí)間軸實(shí)例
這篇文章主要介紹了DevExpress中ChartControl實(shí)現(xiàn)時(shí)間軸的方法,涉及相關(guān)C#繪圖程序用法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
c# 從IE瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容
從IE瀏覽器獲取當(dāng)前頁(yè)面內(nèi)容可能有多種方式,今天我所介紹的是其中一種方法?;驹恚寒?dāng)鼠標(biāo)點(diǎn)擊當(dāng)前IE頁(yè)面時(shí),獲取鼠標(biāo)的坐標(biāo)位置,根據(jù)鼠標(biāo)位置獲取當(dāng)前頁(yè)面的句柄,然后根據(jù)句柄,調(diào)用win32的東西進(jìn)而獲取頁(yè)面內(nèi)容。感興趣的朋友可以參考下本文2021-06-06
C#使用winform實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了C#使用winform實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法
這篇文章主要介紹了C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#中創(chuàng)建統(tǒng)一API接口的實(shí)現(xiàn)方案
在 C# 中創(chuàng)建統(tǒng)一 API 接口需要從架構(gòu)設(shè)計(jì)、技術(shù)選型和代碼實(shí)現(xiàn)等多個(gè)層面進(jìn)行規(guī)劃,本文給大家詳細(xì)介紹了實(shí)現(xiàn)方案和完整示例代碼,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04

