Unity Shader物体线框

Shader

Shader 的中文意思是着色器

简单通俗的理解就是给模型上色的一个工具

这里的上色并不是简单的填色,而是通过对一些模型数据和光照信息的计算而产生相应效果的颜色艺术

物体外部线框

在模拟建造类的游戏中,游戏对象往往需要一个创建前的预览效果

而用游戏物体的本身外部边框来进行前期预览就是最简单的一种显示效果

具体步骤如下:

创建一个名为 Wireframe 的 Shader

  1 Shader "Wireframe"
  2 {
  3     Properties
  4     {
  5             _Color("Color",Color) = (1.0,1.0,1.0,1.0)
  6             _EdgeColor("Edge Color",Color) = (1.0,1.0,1.0,1.0)
  7             _Width("Width",Range(0,1)) = 0.2
  8     }
  9         SubShader
 10     {
 11             Tags
 12         {
 13             "Queue" = "Transparent"
 14             "IgnoreProjector" = "True"
 15             "RenderType" = "Transparent"
 16             }
 17             Blend SrcAlpha OneMinusSrcAlpha
 18             LOD 200
 19             Cull Front
 20             zWrite off
 21             Pass {
 22             CGPROGRAM
 23             #pragma vertex vert
 24             #pragma fragment frag
 25             #pragma target 3.0
 26             #include "UnityCG.cginc"
 27 
 28             struct a2v {
 29                 half4 uv : TEXCOORD0;
 30                 half4 vertex : POSITION;
 31             };
 32 
 33             struct v2f {
 34                 half4 pos : SV_POSITION;
 35                 half4 uv : TEXCOORD0;
 36             };
 37             fixed4 _Color;
 38             fixed4 _EdgeColor;
 39             float _Width;
 40 
 41             v2f vert(a2v v)
 42             {
 43                 v2f o;
 44                 o.uv = v.uv;
 45                 o.pos = UnityObjectToClipPos(v.vertex);
 46                 return o;
 47             }
 48 
 49 
 50             fixed4 frag(v2f i) : COLOR
 51             {
 52                 fixed4 col;
 53                 float lx = step(_Width, i.uv.x);
 54                 float ly = step(_Width, i.uv.y);
 55                 float hx = step(i.uv.x, 1.0 - _Width);
 56                 float hy = step(i.uv.y, 1.0 - _Width);
 57                 col = lerp(_EdgeColor, _Color, lx * ly * hx * hy);
 58                 return col;
 59             }
 60             ENDCG
 61             }
 62             Blend SrcAlpha OneMinusSrcAlpha
 63             LOD 200
 64             Cull Back
 65             zWrite off
 66             Pass {
 67             CGPROGRAM
 68             #pragma vertex vert
 69             #pragma fragment frag
 70             #pragma target 3.0
 71             #include "UnityCG.cginc"
 72 
 73             struct a2v
 74             {
 75                 half4 uv : TEXCOORD0;
 76                 half4 vertex : POSITION;
 77             };
 78 
 79             struct v2f
 80             {
 81                 half4 pos : SV_POSITION;
 82                 half4 uv : TEXCOORD0;
 83             };
 84             fixed4 _Color;
 85             fixed4 _EdgeColor;
 86             float _Width;
 87 
 88             v2f vert(a2v v)
 89             {
 90                 v2f o;
 91                 o.uv = v.uv;
 92                 o.pos = UnityObjectToClipPos(v.vertex);
 93                 return o;
 94             }
 95 
 96 
 97             fixed4 frag(v2f i) : COLOR
 98             {
 99                 fixed4 col;
100                 float lx = step(_Width, i.uv.x);
101                 float ly = step(_Width, i.uv.y);
102                 float hx = step(i.uv.x, 1.0 - _Width);
103                 float hy = step(i.uv.y, 1.0 - _Width);
104                 col = lerp(_EdgeColor, _Color, lx * ly * hx * hy);
105                 return col;
106             }
107             ENDCG
108             }
109     }
110         FallBack "Diffuse"
111 }

然后创建一个材质球 Material,将该 Shader 设置为它的材质就可以了

其中:

Color 为物体颜色,需将透明度设置为0

Edge Color 为物体外线框颜色,设置为需要的颜色

 简单的预览应用效果:

 

 

 

 

 

👉 | Shader物体外边框显示原文 | 👈

*** |  以上内容仅为学习参考、学习笔记使用  | ***

 

posted @ 2021-12-08 17:48  Mr.Cat~  阅读(1304)  评论(0编辑  收藏  举报