当前位置:网站首页>[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-23. Merge K ascending linked lists
- Chapter2多元函数
- 5.索引优化实战
- 【21天学习挑战赛】二分查找题目之寻找峰值
- 深度学习——以天气识别为例,探讨如何保存神经网络模型
- How to deal with cyber attacks?
- 《剑指offer》题解——week1(持续更新)
- 数据库工具DataGrip V2022.2正式发布——支持导入多个 CSV 文件的选项
- Error detected while processing /home/test/.vim/plugin/visualmark.vim
- leetcode 5705. 判断国际象棋棋盘中一个格子的颜色
猜你喜欢
DSPE-PEG-OH,DSPE-PEG-Hydroxyl,磷脂-聚乙二醇-羟基仅供科研实验使用
宝塔实测-TinkPHP5.1框架小程序商城源码
Chapter 2数据分析
H264之sps解析分辨率
Deep learning - in the recognition, for example, this paper discusses how to preserve the neural network model
leetcode-23. Merge K ascending linked lists
浅聊一下那些营销工具—优惠券
powershell execution strategy
23 Lectures on Disassembly of Multi-merchant Mall System Functions-Platform Distribution Level
宝塔实测-在线药店商城源码带WAP版
随机推荐
项目中'说到做不到'的个人分析
06 动态内存
SQL注入(3)
MutationObserver接口(一) 基本用法
el-popover 内嵌 el-table 后位置错位 乱飘 解决方案
宝塔实测-TinkPHP5.1框架小程序商城源码
5823. 字符串转化后的各位数字之和
wift3.0设置导航栏,标题,字体,item颜色和字体大小
光刻机随感
JSP入门
Matlab optimization method -- 0.618 method
条件变量condition_variable实现线程同步
33 基本统计知识——单项非参数检验
Talk about those marketing tools - coupons
别了,IE浏览器
sql语句实现按顺序排序而不是拼音首字母排序
ros入门(安装)
Promoting practice with competitions-Like the 84th biweekly game reflection and the 305th weekly game supplementary questions
leetcode 5705. 判断国际象棋棋盘中一个格子的颜色
Chapter3 numpy创建数组