135-1821-9792

Unity3D如何实现描边框效果

这篇文章主要为大家展示了Unity3D如何实现描边框效果,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出驿城免费做网站回馈大家。

效果图如下

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();
  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);
  }
 }
}

使用GL将Bound的8条边通过画线形式渲染出来!

以上就是关于Unity3D如何实现描边框效果的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。


网页名称:Unity3D如何实现描边框效果
本文链接:http://kswsj.com/article/piochs.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号