当前位置:网站首页>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
}
}
}边栏推荐
- jvm组成及内存模型
- Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
- The epidemic has spread to 28 states in the United States: more than 100000 employees such as apple and Microsoft work from home, and iphone11 is almost out of stock!
- ConvNeXt:A ConvNet for the 2020s——模型简述
- Take byte offer in four rounds and answer the interview questions
- 视频人体行为检测
- BUUCTF刷题十一道(05)
- [cloud native] deploy redis cluster in k8s
- JVM composition and memory model
- Data warehouse project is never a technical project
猜你喜欢

Five network management trends in 2022

UDF and analysis cases of sparksql, 220726,,

多肽KC2S修饰白蛋白纳米粒/靶向肽GX1修饰人血清白蛋白纳米粒探针的研究制备

51单片机内部外设:实时时钟(SPI)

It's time to say goodbye gracefully to nullpointexception

Take byte offer in four rounds and answer the interview questions

BUUCTF刷题十一道(05)

深度剖析 —— 文件操作

Jumpserver learning

智能家居浪潮来袭,如何让机器看懂世界 【结尾有资料】
随机推荐
BUUCTF刷题十一道(05)
Buuctf brushes eleven questions (05)
已有6名员工确诊!三星第三度关闭龟尾手机工厂!
android 11 安全策略及权限管理
MeterSphere金融公司落地经验分享
技术生涯10年,那些让我心动的技术书
10 years of technical career, those technical books that make me excited
51单片机内部外设:实时时钟(SPI)
TFRecord的Shuffle、划分和读取
摩托罗拉诉海能达案一审结果出炉:海能达被判赔53亿元
2022/4/8 exam summary
PX4模块设计之十三:WorkQueue设计
Summary of exam on May 17, 2022
Fluorescence imaging of cle19 polypeptide in cells preparation of fluorescence quenching quantum dots of bovine serum albumin
MMU learning summary
联合省选2022复习计划
The ASML lithography machine purchased by SMIC international entered the factory smoothly, but it is not a non EUV lithography machine!
Window localstorage properties and location objects
Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
C语言详解系列——函数的认识(5)函数递归与迭代