当前位置:网站首页>[Graphics] 19 Lighting model (four, Blinn-Phong lighting model)
[Graphics] 19 Lighting model (four, Blinn-Phong lighting model)
2022-08-09 03:29:00 【paper limit】
来源:《UNITY SHADER入门精要》
1、回忆Blinn光照模型
BlinnIllumination model no use 反射方向、The introduction of the new half vector(bisecoter) h ^ \boldsymbol{\hat{h}} h^,它是通过对视角方向 v ^ \hat{v} v^ And the light direction and r ^ \hat{r} r^ Want to add more normalization of:
h ^ = v ^ + I ∣ v ^ + I ∣ \mathbf{\hat{h}}=\frac{\mathbf{\hat{v}}+\boldsymbol{I}}{|\mathbf{\hat{v}}+\boldsymbol{I}|} h^=∣v^+I∣v^+I
BlinnModel calculation specular reflection formula is as follows:
c s p c u l a r = ( c l i g h t ⋅ m s p e c u l a r ) max ( 0 , n ^ ⋅ h ^ ) m g l o s s \boldsymbol{c}_{spcular}=\left( \boldsymbol{c}_{light}\cdot \boldsymbol{m}_{specular} \right) \max \left( 0,\hat{n}\,\,\cdot \,\,\hat{h} \right) ^{m_{gloss}} cspcular=(clight⋅mspecular)max(0,n^⋅h^)mgloss
2、Blinn-Phong光照模型的实现
Here just give a per-pixel implementation.
Shader "Unity Shaders Book/Chapter 6/Blinn-Phong" {
Properties {
_Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
_Specular ("Specular", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
SubShader {
Pass {
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
v2f vert(a2v v) {
v2f o;
// Transform the vertex from object space to projection space
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// Transform the normal from object space to world space
o.worldNormal = mul(v.normal, (float3x3)_World2Object);
// Transform the vertex from object spacet to world space
o.worldPos = mul(_Object2World, v.vertex).xyz;
return o;
}
The previous code is the same as before,根据公式,We only change the part of the fragment shader code.
Remember why 39 Line is the left,Because they both changed the location,To avoid the invert operation.
fixed4 frag(v2f i) : SV_Target {
// Get ambient term
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
// Compute diffuse term
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir));
// Get the view direction in world space
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
// Get the half direction in world space
fixed3 halfDir = normalize(worldLightDir + viewDir);
// Compute specular term
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
FallBack "Specular"
}
And the code is not the same as before is 第14行,采用的是 半程向量(bisecoter),就是worldLightDir + viewDir ,Then basis, the,然后参与计算.
3、UnityThe built-in help function
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U7lgGW6J-1659936047643)(assets/image-20220620171848498.png)]
Some of the function is simple,Don't help function so we can quickly write out,也能够在 UnityCG.cginc 中找到.比如:
①WorldSpaceViewDir(float4 v)
:
// Computes world space view direction, from object space position
// *Legacy* Please use UnityWorldSpaceViewDir instead
inline float3 WorldSpaceViewDir( in float4 localPos )
{
float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
return UnityWorldSpaceViewDir(worldPos);
}
传入的参数是 模型空间 中的位置,Function to turn into world space,再传入unityworldSpaceViewDir(worldPos)函数
.
当然,We can see from the official comments,This is a legacy of the old function,不如直接使用 加了 unity 的版本.
②UnityWorldSpaceViewDir( in float3 worldPos )
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
return _WorldSpaceCameraPos.xyz - worldPos;
}
There is also carried out 摄像机-顶点位置,这个操作了.其他也没啥了.
⑤UnityWroldSpaceLightDir(float4 v)函数
The other three table about LightDir The function of all is the use of the _WorldSpaceLightPos0
The built-in for the direction of the light.Must be prior to rendering it will be correct assignment.
⑦ UnityObjectToWorldNormal( in float3 norm )函数
// Transforms normal from object to world space
inline float3 UnityObjectToWorldNormal( in float3 norm )
{
#ifdef UNITY_ASSUME_UNIFORM_SCALING
return UnityObjectToWorldDir(norm);
#else
return normalize(mul(norm, (float3x3)unity_WorldToObject));
#endif
}
法线的 模型空间 到 世界空间 The transformation and other normal vector is different.如果对此有疑问,可以参考《UnityShader入门精要》的4.7节.Can also refer to this series[13 UnityShader入门(四)](13 UnityShader入门(四).md/## 6、法线变换问题),Through the formula to see why.
⑧UnityObjectToWorldDir( in float3 dir )函数
:
inline float3 UnityObjectToWorldDir( in float3 dir )
{
return normalize(mul((float3x3)unity_ObjectToWorld, dir));
}
把方向矢量从 模型空间 转到 世界空间.
边栏推荐
- 深度学习:优化器
- 手把手教你uniapp接入聊天IM即时通讯功能-源码分享
- flat() :递归地将数组展平到指定的深度
- 以赛促练-力扣第84场双周赛反思以及第305场周赛补题
- Leetcode Brushing Questions - 148. Sort Linked List
- 对线面试官实现去重和幂等
- MKNetworkKit更换域名时错误解决方法
- The condition variable condition_variable implements thread synchronization
- 数组与切片
- VS2019 compiles boost_1_79, generates 32-bit and 64-bit static libraries
猜你喜欢
H264之sps解析分辨率
ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
Leetcode Brushing Questions - 148. Sort Linked List
Embedded system driver advanced [2] - platform bus driver development _ basic framework
hcip MPLS 实验
i18n 国际化
The condition variable condition_variable implements thread synchronization
unshift() :将一个或多个元素添加到数组的开头
Win10开始菜单打不开怎么办?
条件变量condition_variable实现线程同步
随机推荐
Second data CEO CAI data warming invited to jointly organize the acceleration data elements online salon
static成员及代码块
phpStdudy的下载和DVWA的搭建
Chapter2多元函数
Win10开始菜单打不开怎么办?
通过kvm创建共享磁盘
2022微服务面试题 最新50道题(含答案解析)
创建一个DAPP的全流程
条件变量condition_variable实现线程同步
leetcode 33/81. 搜索旋转排序数组
leetcode-23.合并K个升序链表
23 Lectures on Disassembly of Multi-merchant Mall System Functions-Platform Distribution Level
全链路UI设计笔记
盘点检索任务中的损失函数
嵌入式系统驱动高级【3】——平台总线式驱动开发下__ID匹配和设备树匹配
状态机使用小结
VMware不正常关机
关于微软2022/2023秋招内推的几句
VS2019 compiles boost_1_79, generates 32-bit and 64-bit static libraries
33 基本统计知识——单项非参数检验