当前位置:网站首页>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
}
}
}边栏推荐
猜你喜欢
随机推荐
Cache learning
Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
MySQL的B+Tree索引到底是咋回事?聚簇索引到底是如何长高的?
2022/3/11 考试总结
The follow-up is coming. Whether it's OK without reference, let's make it clear to everyone at once!
数据仓库项目从来不是技术项目
2022/5/18 exam summary
Five network management trends in 2022
云计算服务主要安全风险及应对措施
传英特尔明年将采用台积电6nm EUV工艺
Feed流应用重构-架构篇
Nodejs NPM common instructions summary
The ASML lithography machine purchased by SMIC international entered the factory smoothly, but it is not a non EUV lithography machine!
2022/3/11 exam summary
带你掌握 Makefile 分析
Memoirs of three years in junior high school
2022/6/5考试总结
美国官员建议特朗普阻止英飞凌收购赛普拉斯
Markdown extended syntax
Complete Guide to IOT architecture








