這篇文章主要為大家展示了Unity3D如何實現描邊框效果,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

效果圖如下

將物體添加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;
}
//創建并設置線條材質
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);
}
}
}另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享名稱:Unity3D如何實現描邊框效果-創新互聯
URL標題:http://www.yijiale78.com/article30/pcdpo.html
成都網站建設公司_創新互聯,為您提供網站建設、云服務器、品牌網站制作、自適應網站、微信公眾號、定制網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯