Unity3D實(shí)現(xiàn)描邊框效果
Unity3d描邊框效果網(wǎng)上有很多,大多是使用Shader來實(shí)現(xiàn)的
本文介紹使用Collider來實(shí)現(xiàn)這么一種效果
效果圖如下
將物體添加Collider(Box Collider、Mesh Collider......)
每個(gè)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)的中心點(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); //畫線 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)文章
C#中IEnumerable、ICollection、IList、List之間的區(qū)別
IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。對C# IEnumerable、ICollection、IList、List相關(guān)知識,感興趣的朋友一起看看吧2021-07-07C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解
List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來都很簡單,這篇文章主要給大家介紹了關(guān)于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關(guān)資料,需要的朋友可以參考下2023-11-11C# PictureBox圖片控件實(shí)現(xiàn)圖片交換
在c#中可以使用PictureBox控件來呈現(xiàn)圖像,本文主要介紹了C# PictureBox實(shí)現(xiàn)圖片交換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫的方法
這篇文章主要介紹了C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫的方法,實(shí)例簡述了實(shí)現(xiàn)讀取excel及寫入SQL數(shù)據(jù)庫的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01C# Winform實(shí)現(xiàn)自定義漂亮的通知效果
這篇文章主要介紹了C# Winform實(shí)現(xiàn)自定義漂亮的通知效果,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法
這篇文章主要介紹了C#將字節(jié)數(shù)組轉(zhuǎn)換成數(shù)字的方法,涉及C#類型轉(zhuǎn)換的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色
這篇文章介紹了C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03