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物體外邊框顯示原文 | 👈

*** |  以上內容僅為學習參考、學習筆記使用  | ***

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM