- 4
- 0
- 约 6页
- 2017-06-07 发布于重庆
- 举报
Unity3DShaderLab法线贴图
说到法线贴图,应该算是我们最常使用的一种增强视觉效果的贴图。将法线贴图的各个像素点座位模型的法线,这样我们的光照可以模拟出高分辨率的效果,更多精彩请关注【狗刨学习网】
? ? 同时也保持较低的多边形数。法线贴图通常存储在一个普通的rgb图片,他的rgb分量分别对应了曲面法线的xyz坐标。
? ? 在Unity中,会通过UnpackNormals()函数来使用法线贴图,这使得在表面着色范围内为我们的着色器添加使用法线的过程变得更容易。
? ? 首先,创建一个shader和材质球。我们开始修改shader代码。
1,Properties中添加法线贴图
Properties {
_MainTint(Main Color,Color)=(1,1,1,1)
_NormalTex (Normal Map, 2D) = bump {}
_NormalIntensity(Normal Intensity,Range(0,3))=0.2
}
复制代码
2,SubShader添加变量,修改Input结构体
float4 _MainTint;
sampler2D _NormalTex;
float _NormalIntensity;
struct Input {
float2 uv_NormalTex;
};
复制代码
3,修改surf函数
void surf (Input IN, inout SurfaceOutput o) {
//提取法线信息;
float3 normalMap = UnpackNormal(tex2D(_NormalTex,IN.uv_NormalTex));
normalMap = float3(normalMap.x * _NormalIntensity, normalMap.y * _NormalIntensity, normalMap.z);
o.Normal = normalMap.rgb;
o.Albedo = _MainTint.rgb;
o.Alpha = _MainTint.a;
}
复制代码
? ???修改完毕,返回Unity中,添加贴图,设置Tiling值为10,效果如下:
? ?? ??
? ?? ??
? ? 我们可以看到调整NormalIntensity可以明显改善法线贴图的法线强度。
? ? 在上面我们用到了 UnpackNormals()函数,它是位于UnityCG.cginc文件中定义好的函数。
? ? 通过它我们进行了贴图的法线运算处理,我们就可以利用返回值在光照函数中进行使用了。
Shader 91YGame/Normal {
? ? Properties {
? ?? ???_MainTint(Main Color,Color)=(1,1,1,1)
? ?? ???_NormalTex (Normal Map, 2D) = bump {}
? ?? ???_NormalIntensity(Normal Intensity,Range(0,3))=0.2
? ? }
? ? SubShader {
? ?? ???Tags { RenderType=Opaque }
? ?? ???LOD 200
? ?? ???
? ?? ???CGPROGRAM
? ?? ???#pragma surface surf Lambert
? ?? ???float4 _MainTint;
? ?? ???sampler2D _NormalTex;
? ?? ???float _NormalIntensity;
? ?? ???struct Input {
? ?? ?? ?? ?float2 uv_NormalTex;
? ?? ???};
? ?? ???void surf (Input IN, inout SurfaceOutput o) {
? ?? ?? ?? ?//提取法线;
? ?? ?? ?? ?float3 normalMap = UnpackNormal(tex2D(_NormalTex,IN.uv_NormalTex));
? ?? ?? ?? ?normalMap = float3(normalMap.x * _NormalIntensity, normalMap.y * _NormalIntensity, normalMap.z);
? ?? ?? ?? ?o.Normal = normalMap.rgb;
? ?? ?? ?? ?o.Albedo = _MainTint.rgb;
? ?? ?? ?? ?o.Alpha = _MainTint.a;
? ?? ???}
? ?? ???ENDCG
? ? }
? ? FallBack Diffuse
}
原创力文档

文档评论(0)