当前位置:网站首页>Basic lighting of unity
Basic lighting of unity
2022-07-27 22:58:00 【YF Yunfei】
Preface
In Optics , We use radiance to quantify light .
Illumination is divided into two forms according to different scattering directions : Diffuse reflection (diffuse) And specular reflection (specular).
Specular reflection describes how an object reflects light , Diffuse reflection is how much light is refracted 、 Absorb and scatter out of the surface . According to the number and direction of incident light , We can calculate the number and direction of the emitted light , It is usually described by emittance . The relationship between radiance and emittance is linear , The ratio between them is the diffuse and specular properties of the material .
Next we will introduce Unity The principle and code implementation of the two illumination methods in :
Content
BRDF Model
Early game engines generally had only one lighting model ,BRDF Model , Standard illumination model (Bidirectional Reflectance Distribution Function), also called Phong Model .
Its basic method is , Divide the light entering the camera into 4 part , Each part uses a method to calculate its contribution .
- Spontaneous light Describe when given a direction , How much radiation does a surface itself emit in that direction . Be careful , If global illumination is not used , These self luminous surfaces do not illuminate the surrounding objects , It's just that his body looks brighter .
- Specular reflection ( Metal or something ) Describe when light strikes the surface of a model from a light source , How much radiation will the surface scatter in the direction of full specular reflection .
- Diffuse reflection How much radiation will the surface scatter around
- The ambient light Describe all other indirect lighting ( Is the light emitted by other objects ).
Lambert's law
The intensity of the emitted light is proportional to the cosine of the angle between the surface normal and the direction of the light source .
Calculation formula of diffuse emission
C = (c * m) * max( 0 , n * l ).
A lowercase letter c Color the light source ,m Is the diffuse color ,n The normal is the surface ,l Is the unit vector pointing to the light source . It should be noted that the result multiplied by the light source point should be prevented from being negative ( Avoid objects being illuminated by light sources from behind ), So use max The function limits it to a positive number .
Calculation formula of specular reflection
- r = 2(n * l)n - l
- C = (c * m) * (max(0,v * r)) ^ gloss
n,l The representative meaning is the same as the diffuse reflection formula ,r Is the reflection direction vector of light ,m Is the specular color ,v by View direction vector ,gloss Is the gloss of the material ,gloss The bigger the highlight, the smaller .
Blinn Calculation formula of specular reflection of the model
With the above Phong The model is different from ,Blinn The model introduces a new vector h, Through to v and l After averaging and normalizing, we get :h = (v + l) / | v + l |.
Formula for :C = (c * m) * (max ( 0, n * h ))^gloss
Per pixel and per vertex lighting
Calculate... In a slice shader , It's called per pixel illumination . Calculate in vertex shaders , It's called per vertex lighting ( Gorod coloring ). Lighting per vertex is the calculation of lighting on each vertex , Then linear interpolation is performed inside the rendering entity , Finally, output the imager color . Because the number of vertices is less than the number of pixels , Therefore, the amount of calculation is less than per pixel illumination , Therefore, there will be jagged at the junction of shadows , The fineness is not as good as pixel by pixel illumination .
Unity Ambient light and self illumination
Ambient light can pass through Shader Built in variables for UNITY_LIGHTMODEL_AMBIENT visit
Self illumination only needs to be done before the last color is output by the slice shader , Add the self illumination color of the material to the output color .
Diffuse and specular Shader Realization
Diffuse light model ( per-vertex )
Shader "Unity Shader Book/Chapter6/Diffuse Vertex-Level"
{
Properties
{
_Diffuse("Diffuse",Color) = (1.0,1.0,1.0,1.0)
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
struct appdata
{
float4 vertex : POSITION; // Coordinates of vertex in model space
float3 normal : NORMAL; // normal
} ;
struct v2f
{
fixed3 color : COLOR; // output color
float4 pos : SV_POSITION; // Output location
} ;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex); // Convert model space to crop space
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; // The ambient light
fixed3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject)); // Normal direction n
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz); // Light source location
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLight)); // Formula calculation
o.color = ambient + diffuse;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
}Specular reflection model ( per-vertex )
Shader "Unity Shader Book/Chapter6/SpecularVertexLevel"
{
Properties
{
_Diffuse("Diffuse",Color) = (1.0,1.0,1.0,1.0)
_Specular("Specular",Color) = (1.0,1.0,1.0,1.0)
_Gloss("Gloss",Range(8.0,256)) = 20 // Specular reflection area
}
SubShader
{
Tags { "LightMode"="ForwardBase" } // look out , If there is no such light source, it will be reversed
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
} ;
struct v2f
{
float3 color: COLOR;
float4 pos : SV_POSITION;
} ;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
// Equate to "o.pos = mul(UNITY_MATRIX_MVP,v.vertex);"
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
// Or use UnityObjectToWorldNormal(v.normal)
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal)); // Reflection direction vector r
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld,v.vertex).xyz); // Direction of sight
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss); // Formula calculation
o.color = ambient + diffuse + specular;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
}边栏推荐
- In depth understanding of redis master-slave principle
- The wave of smart home is coming, how to make machines understand the world [there is information at the end]
- Window localstorage properties and location objects
- PyQt5快速开发与实战 4.10 窗口绘图类控件
- Motorola v. hytera: hytera was awarded 5.3 billion yuan
- QT common operation collection
- It's time to say goodbye gracefully to nullpointexception
- Feed流应用重构-架构篇
- Redis learning
- 2022/5/31考试总结
猜你喜欢

Object creation process and object layout

Bluetooth framework summary

catch all in one draft! Introduction to 10 data visualization software

深度剖析 —— 文件操作

浅谈数仓的数据治理

UDF and analysis cases of sparksql, 220726,,

带你掌握 Makefile 分析

Leetcode-461. Hamming distance

Iptables learning

20 character short domain name bypass replication
随机推荐
Invest 2.2 billion dollars! Geke micro 12 inch CIS manufacturing project settled in Shanghai Lingang
已有6名员工确诊!三星第三度关闭龟尾手机工厂!
Build your own website (22)
Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
`What is the difference between SSH -y` (trusted X11 forwarding) and 'SSH -x` (untrusted X11 forwarding)?
Complete Guide to IOT architecture
Motorola v. hytera: hytera was awarded 5.3 billion yuan
Markdown extended syntax
2022年五大网络管理趋势
Feed流应用重构-架构篇
DP traceability problem
20字符短域名绕过复现
Brief explanation of noi 2018
Metersphere financial company landing experience sharing
MeterSphere金融公司落地经验分享
[cloud native] deploy redis cluster in k8s
2022/5/31考试总结
Do you want to be dismissed? Let's take a look at the "exit tips" of programmers
深度剖析 —— 文件操作
Exam summary on May 13, 2022