当前位置:网站首页>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)
边栏推荐
- Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
- Duplicate keys detected:‘/da…‘
- NS3报错 fatal error: ns3/opengym-module.h: No such file or directory
- 高交会重要活动之一|2020中国硬件创新大赛全国总决赛
- 编程测试6.21
- IO进程线程->文件IO->day2
- lcd1602调试
- 超详细的PCB高可靠辨别方法
- 快速排序学习记录
- 创建快捷方式时如何不带“快捷方式“后缀字样?
猜你喜欢

The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
![Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]](/img/38/239933ac987da762135db2d13902d0.png)
Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]

华秋电子成为开放原子开源基金会openDACS捐赠人,共建 openDACS开源生态

VSCode hides the left activity bar

QT serialization 1: readyRead() function, the solution to incomplete data subcontracting

QT serial and CAN dynamic real-time display the log data

力扣题解

查看 word版本号
![[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]](/img/f8/3eabe8833caec0be25aefb72f9c86e.png)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]

FPGA parsing B code----serial 2
随机推荐
2021 soft exam intermediate pass
IO进程线程->文件IO->day2
This beta version of Typora is expired, please download and install a newer;解决方法
实现二叉树--实现删除
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
你不知道的JS语法篇笔记
xxx is not in the sudoers file.This incident will be reported错误
给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
主机和从机配置,建立ssh连接实现Rviz远程控制
二、1稀疏sparsearray数组
Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
Through the bit operations to convert the characters are case sensitive
JS的值和引用,复制和传递
---------手撕二叉树,完成二叉树的前中后序遍历,以及前中后序查找
memset()函数的使用总结和细节
IEEE在指定期刊下搜索相关论文
信号链模拟芯片是什么?
MindSpore 提 PR 全流程
Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]