您的位置:首页 > 运维架构

Shader Anisotropic 各项异性

2017-07-20 14:32 323 查看
在金属管,CD光碟上常看到这样的效果。水波纹也能形成这样的Specular 反射。

实际上就是因为物体上有水波纹这样的高低起伏的表面,让本来该平滑延展的Specular反射,也变得褶皱。

这是要改变光照模型。

用法线贴图来采样褶皱方向。





Properties
{
_MainTint("Diffuse Tint", Color) = (1,1,1,1)
_MainTex("Base (RGB)", 2D) = "white" {}
_SpecularColor("specular Color", Color) = (1,1,1,1)
_Specular("Specular Amount", Range(0,1)) = 0.5
_SpecPower("Specular Power", Range(0,1)) = 0.5
_AnisoDir("Anisotropic Direction", 2D) = "" {}
_AnisoOffset("Anisotropic Offset", Range(-1,1)) = -0.2
}

SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Anisotropic
#pragma target 3.0

sampler2D _MainTex;
sampler2D _AnisoDir;
float4 _MainTint;
float4 _SpecularColor;
float _AnisoOffset;
float _Specular;
float _SpecPower;

struct SurfaceAnisoOutput
{
fixed3 Albedo;
fixed3 Normal;
fixed3 Emission;
fixed3 AnisoDirection;
half Specular;
fixed Gloss;
fixed Alpha;
};

inline fixed4 LightingAnisotropic(SurfaceAnisoOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
fixed3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));
float NdotL = saturate(dot(s.Normal, lightDir));

fixed HdotA = dot(normalize(s.Normal + s.AnisoDirection), halfVector);
float aniso = max(0, sin(radians((HdotA + _AnisoOffset) * 180)));

float spec = saturate(pow(aniso, s.Gloss * 128) * s.Specular);

fixed4 c;
c.rgb = ((s.Albedo * _LightColor0.rgb * NdotL) + (_LightColor0.rgb * _SpecularColor.rgb * spec)) * (atten);
c.a = 1.0;
return c;
}

struct Input
{
float2 uv_MainTex;
float2 uv_AnisoDir;
};

void surf(Input IN, inout SurfaceAnisoOutput o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint;
float3 anisoTex = UnpackNormal(tex2D(_AnisoDir, IN.uv_AnisoDir));

o.AnisoDirection = anisoTex;
o.Specular = _Specular;
o.Gloss = _SpecPower;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: