当前位置:网站首页>Unity shader (basic concept)
Unity shader (basic concept)
2022-07-07 09:24:00 【heater404】
Unity Shader( Basic concepts )
One 、 What is? Shader
shader, The Chinese name is shader .shader In fact, it is a technology specially used to render images , adopt shader, We can customize the algorithm of graphics card rendering , Make the picture achieve the effect we want .
shader There are two kinds of :
- The vertices shader(3D Graphics are composed of triangular patches , The vertices shader Is to calculate the vertices on each triangular patch , And prepare for the final pixel rendering ).
- Pixels shader, seeing the name of a thing one thinks of its function , In pixels , Calculate the light 、 A series of algorithms for color . Several different graphics API They all have their own shader Language , stay DirectX in , The vertices shader be called Vertex Shader , Pixels Shader be called Pixel Shader; stay OpenGL in , The vertices Shader It's also called Vertex Shader , But pixels Shader be called Fragment Shader, That's what we often say Shader Or yuan Shader.
Two 、Shader programing language
since Shader It's a piece of code , It must be written in a language , At present, there are three mainstream languages :
- be based on OpenGL Of OpenGL Shading Language, abbreviation GLSL.
- be based on DirectX Of High Level Shading Language, abbreviation HLSL.
- also NVIDIA The company's C for Graphic, abbreviation Cg Language
So we're in Unity What language should be used in writing Shader Well ?
The official suggestion is to use Cg/HLSL To write the , You can also use it GLSL, Mainly because Cg/HLSL Better cross platform , Prefer to use Cg/HLSL To write the Shader Program .
Unity Shader Strictly speaking, it is not traditional Shader, It is Unity An easy to write after self encapsulation Shader, Also known as ShaderLab.
3、 ... and 、 What is? Unity Shader
stay Unity There is 3 Kind of Shader( It's actually three different ways of writing ):
- Surface Shaders Surface shaders
- Vertex/Fragment Shaders The vertices / Fragment shader
- Fixed Function Shaders Fixed pipeline shader
Surface Shader In fact, that is Unity Yes Vertex/Fragment Shader Another layer of packaging , In order to make Shader The production method is more in line with human thinking mode , At the same time, it can complete the things to be considered under different lighting models and different platforms with very little code .
And in Unity2018 In the later version Unity Official own visualization Shader Tools (Shader Graph). From the generated code , It's all used Vertex/Fragment Shader, Can that be understood as in the future programmable rendering pipeline ,Unity I also abandoned Surface Shader, And all use Vertex/Fragment Shader?
Four 、Shader Templates
Standard Surface Shader
Standard surface shader , It is a physics based shading system ( Used Physically Based Rendering( abbreviation PBR) technology , That is, physics based rendering technology ), Simulate the relationship between materials and lights in a realistic way , It can easily show various metal reflective effects , At the same time, this shader The logic of writing is also more in line with human thinking patterns .
Unlit Shader
Vertex/Fragment Shader, That is, the most basic vertex fragment shader , Unaffected by light Shader, Mostly used for special effects 、UI Effect production on . This is also the simplest and most basic .
Image Effect Shader
It is also a vertex clip shader , It's just a customized template for post-processing , What is post-processing ?Bloom( There are also people called. Glow/ Floodlight / Glow, etc )、 Color matching 、 The depth of field 、 Fuzzy etc , These are reprocessed based on the final whole screen shader It's post-processing .
Compute Shader
Compute shader Is to run a program on the graphics card , Independent of the regular rendering pipeline , It can directly transfer GPU Use it as a parallel processor , So that GPU Not only has 3D Rendering capabilities , It also has other computing capabilities .
5、 ... and 、 Material and shader The relationship between
Create a new one Unlit Shader, because unity in shader It is a piece of code containing instructions running on the graphics card , So we want to create another material to associate it , In this way, we can assign materials to the objects in the scene to achieve the desired effect .
We select the new material , stay inspector You can see its relevant parameters in the panel , For example, which material is referenced shader, And the specific exposed parameters, etc .
Let's focus on shader This parameter , It represents which shader the current shader is with shader Bind associated . We can choose directly from the drop-down box , Or in project Drag directly in the panel shader To shader .
To sum up shader Relationship with material :
One shader It can be associated with countless materials
A material can only be associated with one at a time shader( Because we can dynamically change the associated material through code shader)
Materials can be assigned to models , however shader no way
The material is like shader An example of , Each material can have different effects with different parameters , But when shader When the change , All materials associated with it will change accordingly .
6、 ... and 、shader frame
We can do it in VS Install plug-ins in ( Search for shader that will do ) There will be shader Grammar tip . Open our new shader, Then we delete some frames that only remain .
Shader "Unlit/NewUnlitShader"
{
Properties
{
}
SubShader
{
pass
{
}
}
FallBack "DIFFUSE"
}
shader Name path
first line Shader The content behind the keyword is the specific path and name , Of course, we can modify it .
Properties
attribute , The maps and parameters displayed in the shader panel are here Properties Define settings in , The content must be written in Properties After {} Inside . Of course , If you don't need it, you can delete it all , Because it's optional .
There is a general format for writing attributes :
[Attribute]_Name ("Display Name",Type) = Default Value
Attribute: Attribute tags , To put it bluntly Unity Several built-in attribute tag keywords , It is used for some special processing of the current attribute , This tag is not required , Can not add , At the same time, there can also be multiple attribute tags on one attribute .
_Name: Name of property , That's variable name , Variable names should be in English and underlined
Display Name: The name that appears on the materials panel , It mainly plays the role of explanation .
Type: Type of attribute , There are several commonly used :
- Color: Color
- Int: integer
- Float: Floating point numbers
- Vector: Four dimensional number
- 2D:2D texture , The default values are :white、black、gray and bump
- 3D:3D texture
- Cube: Cubic texture
Properties { _Color("I am Color", Color) = (1,1,1,1)// In turn RGBA(0-1) [HDR]_Color("I am also Color", Color) = (1,1,0,1) _Int("I am Int", Int) = 1 _Float("I am Float", Float) = 0.5 _Float("I am also Float", Range( 0 , 1)) = 0.5 [PowerSlider(3)]_Float("I am another Float", Range( 0 , 1)) = 0.5 [IntRange]_Float("i am Float", Range( 0 , 1)) = 1 [Toggle]_Float("I am Toggle", Range( 0 , 1)) = 1 [Enum(UnityEngine.Rendering.CullMode)]_Float(" I am a Float", Float) = 1 _Vector(" I am a Vector", Vector) = (0,0,0,0) _MainTex(" I am a 2D texture ", 2D) = "white" {} [NoScaleOffset]_MainTex(" I am a 2D texture ", 2D) = "white" {} [Normal]_MainTex(" I am a 2D texture ", 2D) = "white" {} _MainTex(" I am a 3D texture ", 3d) = "" {} _MainCubeTex(" I am a Cube texture ", CUBE) = "" {} }
SubShader
In fact, every one Shader Can contain multiple SubShader, No, No , There must be at least one , Because the current Shader The core algorithm of is in SubShader To achieve . When loading Shader when ,Unity Will traverse all SubShader list , And finally choose the first one supported by the user's machine .
We all know that the performance of different hardware is different , In the game, the machine configuration is usually divided into three types: high, middle and low , If we make an effective Shader, However, it can only have better performance on high configuration machines , The middle and low end are too performance intensive , What to do at this time ?SubShader It can come in handy at this time , We can do it here Shader Make three inside SubShader, They correspond to different configurations of high, middle and low levels .
FallBack
Spare tire , It means spare tire . Sometimes we write Shader It is inevitable that unsupported problems will appear on some machines ( The final display is pink ), At this time, we just need to add FallBack, And other words are written in the double quotation marks behind Shader Valid path name for , When encountering unsupported hardware, this Shader Will automatically switch to FallBack Internal Shader, If FallBack Internal Shader I don't support it ? Then continue from FallBack Internal Shader Find again in FallBack
It is shown in pink ), At this time, we just need to add FallBack, And other words are written in the double quotation marks behind Shader Valid path name for , When encountering unsupported hardware, this Shader Will automatically switch to FallBack Internal Shader, If FallBack Internal Shader I don't support it ? Then continue from FallBack Internal Shader Find again in FallBack
边栏推荐
- STM32 clock system
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- 二叉树高频题型
- 2021 year end summary
- Leetcode刷题记录(数组)组合总和、组合总和 II
- Leetcode question brushing record (array) combination sum, combination sum II
- Record of structured interview
- 数据在内存中的存储
- [istio introduction, architecture, components]
- Schema-validation: wrong column type encountered in column XXX in table XXX
猜你喜欢
Jenkins task grouping
STM32 and motor development (from stand-alone version to Networking)
Yapi test plug-in -- cross request
What are the conditions for applying for NPDP?
Integer or int? How to select data types for entity classes in ORM
How to pass the PMP Exam in a short time?
Mysql数据库-锁-学习笔记
Huawei hcip datacom core_ 03day
Install pyqt5 and Matplotlib module
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
随机推荐
Record of structured interview
Common short chain design methods
Redis common commands
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
Huawei hcip datacom core_ 03day
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
Cesium load vector data
Hard core sharing: a common toolkit for hardware engineers
stm32和电机开发(从单机版到网络化)
Variable parameter of variable length function
Add new item after the outbound delivery order of SAP mm sto document is created?
Implementation of corner badge of Youmeng message push
Serializer & modelserializer of DRF serialization and deserialization
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
[cloud native] Devops (I): introduction to Devops and use of code tool
四、机器学习基础
Expérience de port série - simple réception et réception de données
What is MD5
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
Leetcode刷题记录(数组)组合总和、组合总和 II