Diffuse Shading——漫反射光照改善技巧
转:http://www.narkii.com/club/thread-355113-1.html
我们会列出两种方法:使用Half Lambert lighting model(半兰伯特光照模型)和使用一个ramp texture来控制diffuse shading。
准备工作
同样,我们需要你已经做好了上一篇文章中的内容,并得到了如下shader:
Shader “Custom/BasicDiffuse” {
Properties {
_EmissiveColor (“Emissive Color”, Color) = (1,1,1,1)
_AmbientColor(“Ambient Color”, Color) = (1,1,1,1)
_MySliderValue (“This is a Slider”, Range(0,10)) = 2.5
}
SubShader {
Tags { “RenderType”=”Opaque” }
LOD 200
CGPROGRAM
#pragma surface surf BasicDiffuse
//We need to declare the properties variable type inside of the
//CGPROGRAM so we can access its value from the properties block.
float4 _EmissiveColor;
float4 _AmbientColor;
float _MySliderValue;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
//We can then use the properties values in our shader
float4 c;
c =pow((_EmissiveColor + _AmbientColor), _MySliderValue);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal, lightDir));
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);
col.a = s.Alpha;
return col;
}
ENDCG
}
FallBack “Diffuse”
}
创建一个Half Lambert lighting model(半兰伯特光照模型)
如果你看过之前的文章中,相信还记得Lambert这个名字。没错,它就是Unity默认的diffuse lighting model。简单来说,Lambert定律认为,在平面某点漫反射光的光强与该反射点的法向量和入射光角度的余弦值成正比(即我们之前使用dot函数得到的结果)。Half Lambert最初是由Valve(游戏半条命2使用的引擎即是其开发的)提出来,用于提高物体在一些光线无法照射到的区域的亮度的。简单说来,它提高了漫反射光照的亮度,使得漫反射光线可以看起来照射到一个物体的各个表面。而Half Lambert最初也是被用于游戏半条命的画面渲染,为了防止某个物体的背光面丢失形状并且显得太过平面化。这个技术是完全没有基于任何物理原理的,而仅仅是一种感性的视觉增强(参考这里)。
好啦,说了这么多还是要演示一下,代码非常简单!我们只需要稍微更改上述的LightingBasicDiffuse函数:
inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal, lightDir));
// Add this line
float hLambert = difLight * 0.5 + 0.5;
float4 col;
// Modify this line
col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
col.a = s.Alpha;
return col;
}
由代码可以看出,我们定义了一个新的变量hLambert来替换difLight用于计算某点的颜色值。difLight的范围是0.0-1.0,而通过hLambert,我们将结果由0.0-1.0映射到了0.5-1.0,从而达到了增加亮度的目的。下图显示了这一变化:
我们可以通过对比来看一下Lambert和Half Lambert的渲染区别(分别对应左图和右图):
创建一个ramp texture来控制diffuse shading
下面介绍另一种简单实用的方法——使用一张ramp texture(渐变图)来控制漫反射光照的颜色。这允许你着重强调surface的颜色而减弱漫反射光线或者其他更高级光线的影响。 可以在很多卡通风格的游戏中看到这种技术,通常在这些情况下你需要一个更加艺术而非写实风格的画面,并且不需要很多的真实物理模拟的光照模型。
这个技术在Team Fortress 2(军团要塞2)中流行起来,这个技术也是由Valve提出来用于渲染他们的游戏角色的。他们发表了一个非常有名的论文,强烈建议你应该读一下它!这篇论文讲解了军团要塞2中使用的光照和渲染技术。
上代码!
我们重新修改LightingBasicDiffuse函数,增加一个新的变量ramp:
inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal, lightDir));
float hLambert = difLight * 0.5 + 0.5;
float3 ramp = tex2D(_RampTex, float2(hLambert)).rgb;
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (ramp);
col.a = s.Alpha;
return col;
}
其中,我们还需要一张texture,即_RampTex。即之前说到的渐变图。回忆一下,为了能够在Inspector中拖拽一个texture,并在shader中使用需要怎么做?首先,我们需要在Properties块中声明它,然后在SubShader中声明一个相同名字的变量,并制定它的类型,之后就可以在函数中访问它啦!忘记的请翻看之前的几篇文章。完整的代码如下:
Shader “Custom/RampDiffuse” {
Properties {
_EmissiveColor (“Emissive Color”, Color) = (1,1,1,1)
_AmbientColor(“Ambient Color”, Color) = (1,1,1,1)
_MySliderValue (“This is a Slider”, Range(0,10)) = 2.5
// Add this line
_RampTex (“Ramp Texture”, 2D) = “white”{}
}
SubShader {
Tags { “RenderType”=”Opaque” }
LOD 200
CGPROGRAM
#pragma surface surf BasicDiffuse
//We need to declare the properties variable type inside of the
//CGPROGRAM so we can access its value from the properties block.
float4 _EmissiveColor;
float4 _AmbientColor;
float _MySliderValue;
// Add this line
sampler2D _RampTex;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
//We can then use the properties values in our shader
float4 c;
c =pow((_EmissiveColor + _AmbientColor), _MySliderValue);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal, lightDir));
float hLambert = difLight * 0.5 + 0.5;
// Add this line
float3 ramp = tex2D(_RampTex, float2(hLambert)).rgb;
float4 col;
// Modify this line
col.rgb = s.Albedo * _LightColor0.rgb * (ramp);
col.a = s.Alpha;
return col;
}
ENDCG
}
FallBack “Diffuse”
}
使用的ramp texture(渐变图)如下:
其中最重要的代码只有一行:
float3 ramp = tex2D(_RampTex, float2(hLambert)).rgb;
这行代码返回一个rgb值。tex2D函数接受两个参数:第一个参数是操作的texture,第二个参数是需要采样的UV坐标。这里,我们并不像使用一个vertex来代表一个UV坐标进行采样,而仅仅想使用一个漫反射浮点值(即hLambert)来映射到渐变图上的某一个颜色值。最后得到的结果便是,我们将会根据计算得到的Half Lambert光照值来决定光线照射到一个物体表面的颜色变化。
我们再来对比看一下Half Lambert和添加了ramp texture控制后的渲染区别(分别对应左图和右图):
结束语
Diffuse Shader还有最后一篇文章就会阶段性结束了。通过这一些文章,相信已经对Unity Shaders有了一个大致的了解。呼,作为一个初学者,现在的渲染结果可能看起来还狠简陋,但是一口气吃个胖子是不现实的!呼呼,加油!相信学习这些对游戏渲染还是很有帮助的,毕竟每一个出色游戏几乎全部都使用了自己编写的shader,希望自己以后也可以有所创新,可以为游戏增光添彩。
Diffuse Shading——漫反射光照改善技巧的更多相关文章
- 【Unity Shaders】Diffuse Shading——漫反射光照改善技巧
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shaders】Diffuse Shading——使用2D ramp texture来创建一个假的BRDF(双向反射分布函数)
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shaders】概述及Diffuse Shading介绍
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- [Unity Shader] 逐顶点光照和逐片元漫反射光照
书中的6.4节讲的是漫反射的逐顶点光照和逐片元光照. 前一种算法是根据漫反射公式计算顶点颜色(顶点着色器),对颜色插值(光栅化过程)返回每个像素的颜色值(片元着色器). 第二种算法是获得顶点的法线(顶 ...
- 【Unity Shader学习笔记】Unity光照基础-漫反射光照
本代码只适用于平行光. 1.逐顶点漫反射光照 1.1漫反射光照原理 1.2代码实现 在Properties语义块中声明一个漫反射颜色属性 Properties { //漫反射参数,用于调整漫反射效果 ...
- 使用RampTexture来控制diffuse shading
[RampTexture] RampTexture(渐变纹理),可以是1D/2D纹理. This allows you to accentuate the surface's colors to fa ...
- 【Unity Shaders】Diffuse Shading——创建一个自定义的diffuse lighting model(漫反射光照模型)
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shaders】Diffuse Shading——在Surface Shader中使用properties
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 优化实现Mobile Diffuse动态直接光照shader
项目中美术使用了Unity自带的Mobile/Diffuse这个shader制作了一部分场景素材,这个shader会依赖场景中的动态实时光源,比较耗费. 于是自己手动重写一份,简化shader的消耗, ...
随机推荐
- [Java web]Spring+Struts2+Hibernate整合过程
摘要 最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh. 环境 工具IDE :Idea 2018 数据库 ...
- JSON序列——根据JSON生成事务性SQL
JSON序列——根据JSON生成事务性SQL procedure TForm1.Button5Click(Sender: TObject); begin var json: string :='' + ...
- Scala:Functional Objects
先上代码 class FunctionalObjects(var _x: Int, var _y: Int) { require(_x > 0) require(_y > 0) def t ...
- Cocos2d-x 3.x游戏开发之旅 笔记
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#include "MyHello ...
- 混沌的艺术--- YChaos通过数学公式生成混沌图像
艺术真得很难吗?也许如同编程一样容易.我写了一套软件,其功能是通过输入数学方程式,生成艺术图像.一提到数学有人可能会发怵,这里请不要担心,生成混沌的数学公式大都很是简单,基本上只用加.减.乘.除.余. ...
- Android GUI之Activity、Window、View
相信大家在接触Android之初就已经知道了Activity中的setContentView方法的作用了,很明显此方法是用于为Activity填充相应的布局的.那么,Activity是如何将填充的布局 ...
- 反向代理WebSocket连接自动断掉的问题
Nginx可能设置了超时时间,导致WebSocket一会儿就断了 解决方法: 1.增加Nginx配置 proxy_read_timeout 500s; 注:三种超时时间,参见 https://www. ...
- [Python设计模式] 第1章 计算器——简单工厂模式
github地址:https://github.com/cheesezh/python_design_patterns 写在前面的话 """ 读书的时候上过<设计模 ...
- 转发:查看centos中的用户和用户组
1.用户列表文件:/etc/passwd/ 2.用户组列表文件:/etc/group 3.查看系统中有哪些用户: cut -d : -f 1 /etc/passwd 4.查看可以登录系统的用户: ca ...
- redis性能测试报告
服务器配置:16核心,64G 250个并发读:250个并发写性能[内容8千byte] 163为读:164为写: