Unity3D中如何制作游戏角色的描边框效果?

2026-05-17 18:429阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计843个文字,预计阅读时间需要4分钟。

Unity3D中如何制作游戏角色的描边框效果?

Unity3D 边界框效果网上有很多,主要靠Shader实现。下面简要介绍如何使用Collider实现这种效果:

1. 添加Collider:将物体添加Box Collider(盒型Collider)或Mesh Collider(网格Collider)等,使其拥有边界。

2. 设置Collider边界:每个Collider都有自己的边界范围,可以根据需要调整。

3. Shader实现:使用Shader编写代码,通过Collider的边界信息实现边界框效果。

例如,以下是一个简单的Shader代码片段:

shaderShader Custom/BoundShader{ Properties { _MainTex (Texture, 2D)=white {} _BoundsColor (Bounds Color, Color)=(1,1,1,1) } SubShader { Tags { RenderType=Opaque } LOD 100

Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag

#include UnityCG.cginc

struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; };

struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; };

sampler2D _MainTex; float4 _BoundsColor; float4 _BoundsMin; float4 _BoundsMax;

v2f vert (appdata v) { v2f o; o.vertex=UnityObjectToClipPos(v.vertex); o.uv=v.uv; return o; }

Unity3D中如何制作游戏角色的描边框效果?

fixed4 frag (v2f i) : SV_Target { float2 uv=i.uv; float4 color=tex2D(_MainTex, uv); float4 bounds=lerp(_BoundsMin, _BoundsMax, uv); if (length(bounds.xy - uv) <0.1) color=_BoundsColor; return color; } ENDCG } } FallBack Diffuse}

使用上述Shader,根据物体Collider的边界范围,在物体表面渲染出指定的颜色,实现边界框效果。

Unity3d描边框效果网上有很多,大多是使用Shader来实现的

本文介绍使用Collider来实现这么一种效果

效果图如下

将物体添加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); } } }

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计843个文字,预计阅读时间需要4分钟。

Unity3D中如何制作游戏角色的描边框效果?

Unity3D 边界框效果网上有很多,主要靠Shader实现。下面简要介绍如何使用Collider实现这种效果:

1. 添加Collider:将物体添加Box Collider(盒型Collider)或Mesh Collider(网格Collider)等,使其拥有边界。

2. 设置Collider边界:每个Collider都有自己的边界范围,可以根据需要调整。

3. Shader实现:使用Shader编写代码,通过Collider的边界信息实现边界框效果。

例如,以下是一个简单的Shader代码片段:

shaderShader Custom/BoundShader{ Properties { _MainTex (Texture, 2D)=white {} _BoundsColor (Bounds Color, Color)=(1,1,1,1) } SubShader { Tags { RenderType=Opaque } LOD 100

Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag

#include UnityCG.cginc

struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; };

struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; };

sampler2D _MainTex; float4 _BoundsColor; float4 _BoundsMin; float4 _BoundsMax;

v2f vert (appdata v) { v2f o; o.vertex=UnityObjectToClipPos(v.vertex); o.uv=v.uv; return o; }

Unity3D中如何制作游戏角色的描边框效果?

fixed4 frag (v2f i) : SV_Target { float2 uv=i.uv; float4 color=tex2D(_MainTex, uv); float4 bounds=lerp(_BoundsMin, _BoundsMax, uv); if (length(bounds.xy - uv) <0.1) color=_BoundsColor; return color; } ENDCG } } FallBack Diffuse}

使用上述Shader,根据物体Collider的边界范围,在物体表面渲染出指定的颜色,实现边界框效果。

Unity3d描边框效果网上有很多,大多是使用Shader来实现的

本文介绍使用Collider来实现这么一种效果

效果图如下

将物体添加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); } } }

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。