当前位置:网站首页>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)
边栏推荐
- 独立按键控制led进阶(1)
- (*(void (*)())0)() Interpretation
- QT serial port dynamically displays a large number of data waveform curves in real time (5) ======== "Final perfect solution version"
- Three working modes of CPU: real mode, protected mode, long mode
- 四、6、前缀、中缀、后缀表达式(逆波兰表达式)
- 虚拟机栈帧结构
- led闪烁
- 矩阵键盘
- QT serial and CAN dynamic real-time display the log data
- 51数码管显示
猜你喜欢

Duplicate keys detected:‘/da…‘

vscode set sublime theme

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

sizeof和strlen最全区别,以及指针和数组运算解析
![[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]

VsCode打开终端的方法

QT Weekly Skills (1) ~~~~~~~~~ Running Icon

Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)

IO进程线程->标准IO->day1

FPGA parsing B code----serial 2
随机推荐
工程师必看:常见的PCB检测方法有哪些?
[Common usage of markdown]
与所有 ARM 工具、软件兼容?韦斯佰瑞这款MCU内核值得关注!
Through the bit operations to convert the characters are case sensitive
QT weekly skills (2)~~~~~~~~~ interface buttons
VsCode连接远程服务器并修改文件代码
查看 word版本号
力扣题解7.27
(*(void (*)())0)() Interpretation
独立按键控制led
Explore the efficiency of make_shared
[Quick MSP430f149] Notes on learning MSP430f149 during the game
Insertion Sort in Classic Sort
【部分项目展示】
VsCode打开终端的方法
Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
FPGA parsing B code----serial 2
牛顿迭代法求方程的根
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
VSCode隐藏左边活动栏