当前位置:网站首页>[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));
}
把方向矢量从 模型空间 转到 世界空间.
边栏推荐
- 深度学习——以天气识别为例,探讨如何保存神经网络模型
- Leetcode刷题——148. 排序链表
- ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
- 01| Data Type
- 甲乙丙丁加工零件,加工的总数是370, 如果甲加工的零件数多10,如果乙加工的零件数少20,如果丙加工的 零件数乘以2,如果丁加工的零件数除以2,四个人的加工数量相等,求甲乙丙丁各自加工多少个零件?
- JSP入门
- 数据库工具DataGrip V2022.2正式发布——支持导入多个 CSV 文件的选项
- What are the functions and applications of the smart counter control board?
- 浅聊一下那些营销工具—优惠券
- Chapter2多元函数
猜你喜欢

el-popover 内嵌 el-table 后位置错位 乱飘 解决方案

浅聊一下那些营销工具—优惠券

Chapter2多元函数

对线面试官实现去重和幂等

Chapter 2数据分析

数据库工具DataGrip V2022.2正式发布——支持导入多个 CSV 文件的选项

ffmpeg之H265解码

Deep learning - in the recognition, for example, this paper discusses how to preserve the neural network model

下秒数据CEO蔡致暖受邀参加联合数据举办《数据要素加速跑》线上沙龙

lvs+keepalived高可用负载均衡集群
随机推荐
net core 读取sqlserver所有表转为json
理性预测,未来音视频开发前景将是这般光景
MKNetworkKit更换域名时错误解决方法
Embedded system driver advanced [2] - platform bus driver development _ basic framework
别了,IE浏览器
JSON beautification plugin for Chrome
DSPE-PEG-OH,DSPE-PEG-Hydroxyl,磷脂-聚乙二醇-羟基仅供科研实验使用
qt字符串之 QString详解
JSP入门
Chapter2多元函数
33 基本统计知识——单项非参数检验
进程和计划任务管理
如何应对网络攻击?
【CAS:41994-02-9 |Biotinyl Tyramide|】生物素基酪氨酰胺
lvs+keepalived高可用负载均衡集群
Exchange VLAN experiment
浅聊一下那些营销工具—优惠券
通过kvm创建共享磁盘
powershell execution strategy
Mysql表打不开