Unity shader之金属质感衣服

一套QQ飞车的衣服,模仿其效果写的shader,效果如下:

 

部分shader如下:

  1 Shader "qq/Cloth"
  2 {
  3     Properties
  4     {
  5         _MainTex ("Base (RGB)", 2D) = "white" {}
  6         _NormalMap("Normal Map", 2D) = "bump" {}
  7         _AlphaTex("Alpha", 2D) = "white" {}
  8 
  9         _SpecularColor ("Specular Color", Color) = (1,1,1,1)
 10 
 11         _Cubemap("Cubemap", Cube) = "" {}
 12         _HighLightMask ("High Light Mask", 2D) = "white" {}
 13     }
 14 
 15     SubShader 
 16     {
 17         Tags
 18         {
 19             "Queue" = "Geometry"
 20             "IgnoreProjector" = "True"
 21             "RenderType" = "Geometry"
 22         }
 23         LOD 200        
 24 
 25         // 裙子的不透明区域
 26         Pass
 27         {
 28             Tags
 29             {
 30                 "LightMode" = "ForwardBase"
 31             }
 32 
 33             Cull Off
 34 
 35             CGPROGRAM
 36             #pragma vertex vert
 37             #pragma fragment frag
 38             #pragma multi_compile_fwdbase
 39 
 40             #include "UnityCG.cginc"
 41             #include "AutoLight.cginc"
 42             #include "Lighting.cginc"
 43 
 44             sampler2D _MainTex;
 45             sampler2D _NormalMap;
 46             sampler2D _AlphaTex;
 47             fixed4 _SpecularColor;
 48             samplerCUBE _Cubemap;
 49             sampler2D _HighLightMask;
 50 
 51             struct appdata
 52             {
 53                 float4 vertex : POSITION;
 54                 float2 uv : TEXCOORD0;
 55                 float3 normal : NORMAL;
 56                 float4 tangent : TANGENT;
 57             };
 58 
 59             struct v2f
 60             {
 61                 float4 pos : SV_POSITION;
 62                 float2 uv : TEXCOORD0;
 63                 float4 T2W1 : TEXCOORD1;
 64                 float4 T2W2 : TEXCOORD2;
 65                 float4 T2W3 : TEXCOORD3;
 66             };
 67 
 68             v2f vert(appdata v)
 69             {
 70                 v2f o;
 71                 o.pos = UnityObjectToClipPos(v.vertex);
 72                 o.uv = v.uv;
 73 
 74                 float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
 75                 float3 worldNormal = UnityObjectToWorldNormal(v.normal);
 76                 float3 binormal = cross(normalize(worldNormal), normalize(worldTangent)) * v.tangent.w;
 77                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
 78                 o.T2W1 = float4(worldTangent.x, binormal.x, worldNormal.x, worldPos.x);
 79                 o.T2W2 = float4(worldTangent.y, binormal.y, worldNormal.y, worldPos.y);
 80                 o.T2W3 = float4(worldTangent.z, binormal.z, worldNormal.z, worldPos.z);
 81                 return o;
 82             }
 83 
 84             fixed4 frag(v2f i) : SV_TARGET
 85             {
 86                 fixed4 albedo = tex2D(_MainTex, i.uv);
 87                 fixed3 alphaCol = tex2D(_AlphaTex, i.uv);
 88                 fixed alpha = alphaCol.g;
 89 
 90                 float3 worldPos = float3(i.T2W1.w, i.T2W2.w, i.T2W3.w);
 91                 fixed3 worldView = normalize(UnityWorldSpaceViewDir(worldPos));
 92 
 93                 float3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv));
 94                 float3x3 tanToWorld = float3x3(i.T2W1.xyz, i.T2W2.xyz, i.T2W3.xyz);
 95                 float3 worldNormal = normalize(mul(tanToWorld, tangentNormal));
 96 
 97                 fixed3 ambient = albedo.xyz * UNITY_LIGHTMODEL_AMBIENT.xyz;
 98 
 99                 // 漫反射 半兰伯特光照模型
100                 fixed3 worldLight = worldView;
101                 //float dotLN = dot(worldLight, worldNormal) * 0.5 + 0.5;
102                 float dotLN = dot(worldLight, worldNormal) * 0.25 + 0.75;        // [-1, 1] -> [0.5, 1]
103                 fixed3 diffuse = albedo.xyz * _LightColor0.xyz * dotLN;
104 
105                 // 金边
106                 float3 refDir = reflect(-worldView, worldNormal);
107                 fixed3 refCol = texCUBE(_Cubemap, refDir);
108                 fixed3 maskCol = tex2D(_HighLightMask, i.uv);
109                 diffuse = diffuse * (1 + refCol.r * maskCol.r * 8 * (1 - alphaCol.r));
110 
111                 // 高光
112                 fixed3 realWorldLight = normalize(UnityWorldSpaceLightDir(worldPos));
113                 fixed3 halfDir = normalize(worldView + realWorldLight);
114                 float specD = abs(dot(halfDir, worldNormal));
115                 fixed3 specular = albedo.xyz * _SpecularColor * pow(specD, 8);
116 
117                 clip(alpha - 0.95);
118                 fixed4 col = fixed4(ambient + diffuse + specular, alpha);
119 
120                 return col;
121             }
122 
123             ENDCG
124         }
125 
126 
127         // 裙子的半透明区域
128         Pass
129         {
130             Tags
131             {
132                 "LightMode" = "ForwardBase"
133             }
134             Blend SrcAlpha OneMinusSrcAlpha
135             ZWrite Off
136             Cull Off
137 
138             CGPROGRAM
139             #pragma vertex vert
140             #pragma fragment frag
141             #pragma multi_compile_fwdbase
142 
143             #include "UnityCG.cginc"
144             #include "AutoLight.cginc"
145             #include "Lighting.cginc"
146 
147             sampler2D _MainTex;
148             sampler2D _AlphaTex;
149 
150             struct appdata
151             {
152                 float4 vertex : POSITION;
153                 float2 uv : TEXCOORD0;
154             };
155 
156             struct v2f
157             {
158                 float4 pos : SV_POSITION;
159                 float2 uv : TEXCOORD0;
160             };
161 
162             v2f vert(appdata v)
163             {
164                 v2f o;
165                 o.pos = UnityObjectToClipPos(v.vertex);
166                 o.uv = v.uv;
167                 return o;
168             }
169 
170             fixed4 frag(v2f i) : SV_TARGET
171             {
172                 fixed4 albedo = tex2D(_MainTex, i.uv);
173                 fixed3 alphaCol = tex2D(_AlphaTex, i.uv);
174                 fixed alpha = alphaCol.g;
175                 clip(0.95 - alpha);
176                 fixed4 col = fixed4(albedo.rgb, alpha);
177                 return col;
178             }
179 
180             ENDCG
181         }
182 
183     } 
184 
185     Fallback "Diffuse"
186 }
cloth

 

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/10830238.html

资源如下:https://files.cnblogs.com/files/jietian331/qq%E9%A3%9E%E8%BD%A6_%E9%87%91%E5%B1%9E%E8%B4%A8%E6%84%9F%E8%A1%A3%E6%9C%8D.zip

 

posted @ 2019-05-08 10:18  孤独の巡礼  阅读(2817)  评论(2编辑  收藏  举报