6.Half Lambert光照Diffuse Shader
2016-07-06 10:52
501 查看
简单来说,Lambert定律认为,在平面某点漫反射光的光强与该反射点的法向量和入射光角度的余弦值成正比(即我们之前使用dot函数得到的结果)。Half Lambert最初是由Valve(游戏半条命2使用的引擎即是其开发的)提出来,用于提高物体在一些光线无法照射到的区域的亮度的。简单说来,它提高了漫反射光照的亮度,使得漫反射光线可以看起来照射到一个物体的各个表面。而Half
Lambert最初也是被用于游戏半条命的画面渲染,为了防止某个物体的背光面丢失形状并且显得太过平面化。这个技术是完全没有基于任何物理原理的,而仅仅是一种感性的视觉增强
相比之前的Lambert光照shader,只是多加了一行代码
float hLambert = difLight * 0.5 + 0.5;
由代码可以看出,我们定义了一个新的变量hLambert来替换difLight用于计算某点的颜色值。difLight的范围是0.0 - 1.0,而通过hLambert,我们将结果由0.0 - 1.0映射到了0.5 - 1.0,从而达到了增加亮度的目的。下图显示了这一变化:
![](https://img-blog.csdn.net/20131220211508250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FuZHljYXQxOTky/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Lambert最初也是被用于游戏半条命的画面渲染,为了防止某个物体的背光面丢失形状并且显得太过平面化。这个技术是完全没有基于任何物理原理的,而仅仅是一种感性的视觉增强
相比之前的Lambert光照shader,只是多加了一行代码
float hLambert = difLight * 0.5 + 0.5;
由代码可以看出,我们定义了一个新的变量hLambert来替换difLight用于计算某点的颜色值。difLight的范围是0.0 - 1.0,而通过hLambert,我们将结果由0.0 - 1.0映射到了0.5 - 1.0,从而达到了增加亮度的目的。下图显示了这一变化:
Shader "Study/6_HalfLambertDiffuse" { <span style="white-space:pre"> </span>Properties{ <span style="white-space:pre"> </span>_Color("Main Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_MainTex("Base (RGB)", 2D) = "white" {} <span style="white-space:pre"> </span>_EmissiveColor("Emissive Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_AmbientColor("Ambient Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_MySliderValue("This is a Slider", Range(0,10)) = 2.5 <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>SubShader{ <span style="white-space:pre"> </span>Tags{ "RenderType" = "Opaque" } <span style="white-space:pre"> </span>LOD 200 <span style="white-space:pre"> </span>CGPROGRAM #pragma surface surf Lambert MyLambertDiffuse <span style="white-space:pre"> </span>sampler2D _MainTex; <span style="white-space:pre"> </span>fixed4 _Color; <span style="white-space:pre"> </span>//We need to declare the properties variable type inside of the <span style="white-space:pre"> </span>//CGPROGRAM so we can access its value from the properties block. <span style="white-space:pre"> </span>float4 _EmissiveColor; <span style="white-space:pre"> </span>float4 _AmbientColor; <span style="white-space:pre"> </span>float _MySliderValue; <span style="white-space:pre"> </span>struct Input <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>float2 uv_MainTex; <span style="white-space:pre"> </span>}; <span style="white-space:pre"> </span>void surf(Input IN, inout SurfaceOutput o) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//We can then use the properties values in our shader <span style="white-space:pre"> </span>//float4 c; <span style="white-space:pre"> </span>fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; <span style="white-space:pre"> </span>c = c * pow((_EmissiveColor + _AmbientColor), _MySliderValue); <span style="white-space:pre"> </span>o.Albedo = c.rgb; <span style="white-space:pre"> </span>o.Alpha = c.a; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>inline float4 LightingMyLambertDiffuse(SurfaceOutput s, fixed3 lightDir, fixed atten) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>float difLight = max(0, dot(s.Normal, lightDir)); <span style="white-space:pre"> </span>float hLambert = difLight * 0.5 + 0.5; <span style="white-space:pre"> </span>float4 col; <span style="white-space:pre"> </span>col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2); <span style="white-space:pre"> </span>col.a = s.Alpha; <span style="white-space:pre"> </span>return col; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>ENDCG <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>FallBack "Diffuse" }
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- Unity UGUI教程之实现滑页效果
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3D-5.3.5发布VR项目到Android
- unity 新UI草稿
- [软件资讯]Unity已支持将3D游戏导出成Flash
- ShaderLab: Pass
- Unity 武器拖尾效果
- unity常见问题之20题
- 反编译ARB program to GLSL shader日记
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- uLua中遇到的问题
- Unity3d 协程、调用函数、委托