当前位置:网站首页>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)
边栏推荐
- OpenLayers 初学者指南,源码测试可用
- The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
- 二、1稀疏sparsearray数组
- Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)
- 暂时存着阿里云
- 2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
- 闭包(你不知道的JS)
- -----博客声明
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
- 力扣题解7.27
猜你喜欢

IO进程线程->目录IO->day3

无法完成包的安装npm ERR! Refusing to install package with name “moment“ under a package also called “moment“
![[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]

独立按键控制led
![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]

【已解决:el-input标签无法输入或不显示文字】

sizeof和strlen最全区别,以及指针和数组运算解析

pdf和word等文档添加水印

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

ES6语法笔记(ES6~ES11)
随机推荐
Comparison of advantages and disadvantages of VsCode and Sublime editors
Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
FPGA parsing B code----serial 2
Insertion Sort in Classic Sort
QT serial and CAN dynamic real-time display the log data
爬楼梯C语言
vs compile boost library script
关于 PCB 多层板制程能力不得不说的那些事儿
OpenLayers (ol包),Vite显示地图(附源码)
QT Weekly Skills (1) ~~~~~~~~~ Running Icon
(*(void (*)())0)() Interpretation
ES6语法笔记(ES6~ES11)
《C陷阱和缺陷》void (*signal(int , void(*)(int)))(int)的深刻解读
力扣题解
ES6 syntax notes (ES6~ES11)
QT serial 3: LORA test platform based on QT and STM32H750 (2)
干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
The IEEE under the specified journal search related papers
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"