一套QQ飞车的衣服,模仿其效果写的shader,效果如下:
部分shader如下:
Shader "qq/Cloth"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
_AlphaTex("Alpha", 2D) = "white" {} _SpecularColor ("Specular Color", Color) = (,,,) _Cubemap("Cubemap", Cube) = "" {}
_HighLightMask ("High Light Mask", 2D) = "white" {}
} SubShader
{
Tags
{
"Queue" = "Geometry"
"IgnoreProjector" = "True"
"RenderType" = "Geometry"
}
LOD // 裙子的不透明区域
Pass
{
Tags
{
"LightMode" = "ForwardBase"
} Cull Off CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase #include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc" sampler2D _MainTex;
sampler2D _NormalMap;
sampler2D _AlphaTex;
fixed4 _SpecularColor;
samplerCUBE _Cubemap;
sampler2D _HighLightMask; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 T2W1 : TEXCOORD1;
float4 T2W2 : TEXCOORD2;
float4 T2W3 : TEXCOORD3;
}; v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv; float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 binormal = cross(normalize(worldNormal), normalize(worldTangent)) * v.tangent.w;
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.T2W1 = float4(worldTangent.x, binormal.x, worldNormal.x, worldPos.x);
o.T2W2 = float4(worldTangent.y, binormal.y, worldNormal.y, worldPos.y);
o.T2W3 = float4(worldTangent.z, binormal.z, worldNormal.z, worldPos.z);
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv);
fixed3 alphaCol = tex2D(_AlphaTex, i.uv);
fixed alpha = alphaCol.g; float3 worldPos = float3(i.T2W1.w, i.T2W2.w, i.T2W3.w);
fixed3 worldView = normalize(UnityWorldSpaceViewDir(worldPos)); float3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv));
float3x3 tanToWorld = float3x3(i.T2W1.xyz, i.T2W2.xyz, i.T2W3.xyz);
float3 worldNormal = normalize(mul(tanToWorld, tangentNormal)); fixed3 ambient = albedo.xyz * UNITY_LIGHTMODEL_AMBIENT.xyz; // 漫反射 半兰伯特光照模型
fixed3 worldLight = worldView;
//float dotLN = dot(worldLight, worldNormal) * 0.5 + 0.5;
float dotLN = dot(worldLight, worldNormal) * 0.25 + 0.75; // [-1, 1] -> [0.5, 1]
fixed3 diffuse = albedo.xyz * _LightColor0.xyz * dotLN; // 金边
float3 refDir = reflect(-worldView, worldNormal);
fixed3 refCol = texCUBE(_Cubemap, refDir);
fixed3 maskCol = tex2D(_HighLightMask, i.uv);
diffuse = diffuse * ( + refCol.r * maskCol.r * * ( - alphaCol.r)); // 高光
fixed3 realWorldLight = normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 halfDir = normalize(worldView + realWorldLight);
float specD = abs(dot(halfDir, worldNormal));
fixed3 specular = albedo.xyz * _SpecularColor * pow(specD, ); clip(alpha - 0.95);
fixed4 col = fixed4(ambient + diffuse + specular, alpha); return col;
} ENDCG
} // 裙子的半透明区域
Pass
{
Tags
{
"LightMode" = "ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase #include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc" sampler2D _MainTex;
sampler2D _AlphaTex; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
}; v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv);
fixed3 alphaCol = tex2D(_AlphaTex, i.uv);
fixed alpha = alphaCol.g;
clip(0.95 - alpha);
fixed4 col = fixed4(albedo.rgb, alpha);
return col;
} ENDCG
} } Fallback "Diffuse"
}
cloth