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"
实际上就是因为物体上有水波纹这样的高低起伏的表面,让本来该平滑延展的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"
相关文章推荐
- 各项异性扩散(Anisotropic diffusion)
- 各项异性滤波简介Anisotropic Filtering(AF)
- 最近做了个并行的计算各项异性地震波的有限元程序(cuda+mpi),可以计算超大规模问题,网格点个数可以几百亿都没有问题.
- 各项异性扩散滤波 -- OpenCV实现
- 各项异性过滤
- 各项异性滤波简单介绍Anisotropic Filtering(AF)
- 各项异性扩散(Anisotropic diffusion)
- OpenGL蓝宝书源码学习(十)第五章——纹理的应用、Mip贴图、各项异性过滤和纹理压缩基础
- 头发各项异性
- vsftpd.conf各项参数详解
- 婚后面对异性的邀请
- tomcat6连接池的各项配置详解及例程
- 细数.net3.0的各项新特性
- 给站长们介绍个测试你网站各项性能的免费在线工具
- Windows XP 任务管理器―性能 各项的意思
- 使用vmstat命令查看系统资中各项的解释
- 注册表中的各项解释
- Silverlight中的NullExtension与Transparent在事件处理中的相异性
- PowerDesigner 设置各项变量参数的路径
- vc++ 创建异性窗体(1)