当前位置:网站首页>Unity Shader 标准光照模型——漫反射
Unity Shader 标准光照模型——漫反射
2022-07-30 05:47:00 【Misaki_Me】
Unity Shader 标准光照模型——漫反射
1. 逐顶点的漫反射
Shader "Unlit/DiffuseReflection"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 color : Color;
};
v2f vert (appdata_base v) //appdata_base 为unity Shader自己定义的一个结构体
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
//兰伯特公式
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * saturate(dot(worldNormal,worldLight));
o.color = diffuse + ambient;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color,1);
}
ENDCG
}
}
}
2. 逐片元的漫反射
Shader "Unlit/FragmentReflection"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
};
v2f vert (appdata_base v) //appdata_base为unity自己定义的结构体
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
o.worldNormal = worldNormal;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * max(0,dot(worldLightDir,i.worldNormal));
fixed3 color = ambient + diffuse;
return fixed4(color,1);
}
ENDCG
}
}
}
3.半兰伯特漫反射
Shader "Unlit/HalfLambert"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
o.worldNormal = worldNormal;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
//半兰伯特公式
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * dot(worldLightDir,i.worldNormal) * 0.5 + 0.5;
fixed3 color = ambient + diffuse;
return fixed4(color,1);
}
ENDCG
}
}
}
4. 各自的优缺点
逐顶点的漫反射与逐片元的漫反射的区别在于顶点漫反射在阴影切换处会有明显的锯齿反应,可以看的到明显的顶点。而片元漫反射则相对切换平滑。
以上的逐顶点和逐片元漫反射都是兰伯特漫反射公式,但是兰伯特漫反射公式有个缺点是暗的地方接近全暗,为了解决这个全暗的问题则有半兰伯特公式。半兰伯特公式在兰伯特公式的基础上 result * 0.5 + 0.5 .这就是半兰伯特漫反射。
最终效果如下。(从左至右依次为逐顶点、逐片元、半lambert)
边栏推荐
- 自定义类加载器
- This beta version of Typora is expired, please download and install a newer;解决方法
- js高级学习笔记(详细)
- 2021 soft exam intermediate pass
- Sklearn : train_test_split()函数的用法
- 动态规划入门 JS
- 四、6、前缀、中缀、后缀表达式(逆波兰表达式)
- Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
- 牛顿迭代法求方程的根
- c语言编程练习
猜你喜欢
Three working modes of CPU: real mode, protected mode, long mode
js advanced study notes (detailed)
IO进程线程->标准IO->day1
查看 word版本号
【已解决:el-input标签无法输入或不显示文字】
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
VSCode hides the left activity bar
多层板的层数,为啥选项都是偶数?就不能选奇数?
高交会重要活动之一|2020中国硬件创新大赛全国总决赛
随机推荐
sizeof和strlen最全区别,以及指针和数组运算解析
Sklearn : train_test_split()函数的用法
Antd简单启动一个企业级项目
Cannnot download sources不能下载源码百分百超详细解决方案
Difference between logical shift right and arithmetic right shift
Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
QT serial 4: LORA test platform based on QT and STM32H750 (3)
电子工程师怎么才能规范设计标准、提高设计效率?
测试第一题
多层板的层数,为啥选项都是偶数?就不能选奇数?
迷宫问题----经典回溯法解决
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
OpenLayers (ol包),Vite显示地图(附源码)
Explore the efficiency of make_shared
超详细的PCB高可靠辨别方法
Vim查找字符
VsCode与Sublime编辑器优缺点对比
独立按键控制led进阶(1)
QT weekly skills (3)~~~~~~~~~ serial port addition
【markdown常用用法】