這篇文章主要為大家展示了Unity3D如何實現(xiàn)描邊框效果,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
效果圖如下
將物體添加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(); //這里無需將矩陣從本地坐標轉(zhuǎn)化為世界左邊 //GL.MultMatrix(transform.localToWorldMatrix); for (int i = 0; i < colliders.Length; i++) { var col = colliders[i]; //獲取本物體對象在世界范圍內(nè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作為線條材質(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); } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:Unity3D如何實現(xiàn)描邊框效果-創(chuàng)新互聯(lián)
URL標題:http://www.rwnh.cn/article30/pcdpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、云服務(wù)器、品牌網(wǎng)站制作、自適應(yīng)網(wǎng)站、微信公眾號、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容