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

將物體添加Collider(Box Collider、Mesh Collider......)
每個Collider都有自己的邊界Bound,描邊效果就是將Bound顯示出來
代碼如下
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();
//這里無需將矩陣從本地坐標(biāo)轉(zhuǎn)化為世界左邊
//GL.MultMatrix(transform.localToWorldMatrix);
for (int i = 0; i < colliders.Length; i++)
{
var col = colliders[i];
//獲取本物體對象在世界范圍內(nèi)的中心點位置 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個頂點位置
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使用該默認(rèn)的Shader作為線條材質(zhì)
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);
}
}
}
使用GL將Bound的8條邊通過畫線形式渲染出來!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用設(shè)計模式中的工廠方法模式進(jìn)行C#編程的示例講解
這篇文章主要介紹了使用設(shè)計模式中的工廠方法模式進(jìn)行C#編程的示例講解,工廠方法模式可以看作是對簡單工廠模式的進(jìn)一步擴展,需要的朋友可以參考下2016-02-02
DevExpress之ChartControl實現(xiàn)時間軸實例
這篇文章主要介紹了DevExpress中ChartControl實現(xiàn)時間軸的方法,涉及相關(guān)C#繪圖程序用法,具有一定的實用價值,需要的朋友可以參考下2014-10-10
C#采用HttpWebRequest實現(xiàn)保持會話上傳文件到HTTP的方法
這篇文章主要介紹了C#采用HttpWebRequest實現(xiàn)保持會話上傳文件到HTTP的方法,很實用的功能,需要的朋友可以參考下2014-08-08
C#中創(chuàng)建統(tǒng)一API接口的實現(xiàn)方案
在 C# 中創(chuàng)建統(tǒng)一 API 接口需要從架構(gòu)設(shè)計、技術(shù)選型和代碼實現(xiàn)等多個層面進(jìn)行規(guī)劃,本文給大家詳細(xì)介紹了實現(xiàn)方案和完整示例代碼,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04

