当前位置:网站首页>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)
边栏推荐
猜你喜欢
![[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]](/img/96/a98e8b813a2fd9d0a44d3121aaee6a.png)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]

【Exhibition of some projects】

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
![[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]](/img/22/9c0dd4390a98fa2aa4e45164c078cc.png)
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]

Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)

QT serial 3: LORA test platform based on QT and STM32H750 (2)

使用Dva项目作Antd的Demo

牛顿迭代法求方程的根
![[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]](/img/e5/87cf293ac3d0c613864e99a8fe9a47.png)
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]

Acwing刷题第一节
随机推荐
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
力扣题解7.27
Insert map data efficiently
【部分项目展示】
单片机第一步
This beta version of Typora is expired, please download and install a newer;解决方法
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
The IEEE under the specified journal search related papers
IO进程线程->文件IO->day2
二、1稀疏sparsearray数组
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
IEEE在指定期刊下搜索相关论文
conda常用命令总结(持续更新)
工程师必看:常见的PCB检测方法有哪些?
单片机之流水灯
ssh script space character conversion
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
HSPF model application
IO进程线程->标准IO->day1
pdf和word等文档添加水印