1. 程式人生 > 程式設計 >Unity3D實現描邊框效果

Unity3D實現描邊框效果

Unity3d描邊框效果網上有很多,大多是使用Shader來實現的

本文介紹使用Collider來實現這麼一種效果

效果圖如下

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,rz);
   p2 = c + new Vector3(rx,-rz);
   p3 = c + new Vector3(-rx,-rz);
 
   p4 = c + new Vector3(-rx,ry,rz);
   p5 = c + new Vector3(rx,rz);
   p6 = c + new Vector3(rx,-rz);
   p7 = c + new Vector3(-rx,-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);
  }
 }
}

使用GL將Bound的8條邊通過畫線形式渲染出來!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。