Unity3D如何實現(xiàn)描邊框效果-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站主要從事網(wǎng)站建設、成都網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務青島,10余年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575

效果圖如下

Unity3D如何實現(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)建并設置線條材質
  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);
  }
 }
}

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享名稱:Unity3D如何實現(xiàn)描邊框效果-創(chuàng)新互聯(lián)
URL標題:http://www.muchs.cn/article30/pcdpo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設、云服務器、品牌網(wǎng)站制作、自適應網(wǎng)站、微信公眾號定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司