当前位置:网站首页>Unity shader (to achieve a simple material effect with adjustable color attributes only)
Unity shader (to achieve a simple material effect with adjustable color attributes only)
2022-07-07 09:22:00 【heater404】
Unity Shader( Achieve a simple material effect that only the color attribute can be adjusted )
In a Shader in , There can be multiple SubShader And one. SubShader There can also be more than one Pass, But one Shader There must be at least one in the SubShader, And this SubShader There must also be at least one Pass.
Pass It means rendering the model once , How to render specifically requires us to Pass Add Cg/HLSL Code snippets to achieve , This code fragment is composed of CGPROGRAM Start , from ENDCG end . We have to add CGPROGRAM and ENDCG, Then write the code to achieve the effect among others .
Since it is a vertex fragment shader , Then you need to define vertex shaders and fragment shaders first , tell unity Where to implement them respectively .
#pragma vertex vert
// Define vertex shaders , Usually it will be named vert.
#pragma fragment frag
// Define fragment shaders , Usually it will be named frag
#pragma yes Unity Commands for built-in compilation instructions , stay Pass In, we use this command to declare the required vertex shaders and fragment shaders .
So the complete preparation is :
pass
{
CGPROGRAM
//#pragma yes Unity Commands for built-in compilation instructions , stay Pass In, we use this command to declare the required vertex shaders and fragment shaders .
#pragma vertex vert
// Define vertex shaders , Usually it will be named vert.
#pragma fragment frag
// Define fragment shaders , Usually it will be named frag
ENDCG
}
Vertex shader
float4 vert(float4 vertex : POSITION):SV_POSITION
{
return UnityObjectToClipPos(vertex);
}
- The name of the vertex shader function is the name we have previously defined
- among float4 vertex It is a four-dimensional vector defined by ourselves , Name is vertex( Names can be chosen at will ), Just defining a four-dimensional vector does not make it have the vertex information of our model , So here we need to specify a semantics for it –POSITION,POSITION It represents the vertex position information of the model , Now the variable vertex It means the vertex position of our model .
- The most important thing in vertex shaders is to convert vertices from model coordinates to clipping coordinates ( To put it bluntly, it is the matrix conversion that needs to be done when displaying the model on the two-dimensional display ). What if you don't know about matrix conversion , No problem ,unity Ready made orders have been prepared for us . Just call UnityObjectToClipPos that will do , Just add our vertex position variable in parentheses .
- so what , In the following fragment shader, we need the output result of vertex shader , So we need to return Converted vertex ,float4 It is used to define that what we return is a four-dimensional vector .
- The vertex position after conversion , We also need to use semantics to mark , So that the fragment shader can know which is the vertex position information output from the vertex shader . So we add :SV_POSITION.
Fragment shader
Fragment shaders are also called pixel shaders , It mainly deals with the pixel results finally displayed on the screen . After vertex shader processing , We have got the vertex matrix finally displayed on the screen , Interpolation calculation will be carried out automatically inside , To get all fragment pixels of the current model , Then each pixel will execute a fragment shader , Get the final color value of each pixel .
fixed4 _Color;
float4 frag():SV_TARGET0
{
return _Color;
}
- The function name of the fragment shader is still defined above , But this function has no parameters for the time being .
- stay cg/HLSL Use in Properties Before the variables in cg/hlsl Once again , The name should be consistent , This is the principle of death .float、half and fixed These three are the representatives of floating point numbers , It's just that the corresponding accuracy is different , This is mainly used for further optimization .
- Then go straight back _Color, That is to return directly to the color we defined in the material panel , This is also our expected effect .
- Again , The returned value is a four-dimensional vector , We use it float4 To express , If you want to optimize, use fixed4 To express .
- SV_TARGET0 Is a semantic , It means that the function returns the color value for the output of the next stage , That is, the value we finally output to the display .
The final complete code is as follows :
Shader "Unlit/NewUnlitShader"
{
Properties
{
_Color("I am Color", Color) = (1,1,1,1)// In turn RGBA(0-1)
}
SubShader
{
pass
{
CGPROGRAM
//#pragma yes Unity Commands for built-in compilation instructions , stay Pass In, we use this command to declare the required vertex shaders and fragment shaders .
#pragma vertex vert
// Define vertex shaders , Usually it will be named vert.
#pragma fragment frag
// Define fragment shaders , Usually it will be named frag
fixed4 _Color;
float4 vert(float4 vertex : POSITION):SV_POSITION
{
return UnityObjectToClipPos(vertex);
}
float4 frag():SV_TARGET0
{
return _Color;
}
ENDCG
}
}
FallBack "DIFFUSE"
}
summary
The execution of vertex shaders and fragment shaders is not 1:1 Of , For example , A triangular patch , There are only three vertices , Vertex shaders only need to execute 3 Time , The fragment shader is determined by the final number of pixels , It is normal to execute hundreds of thousands . So from the perspective of performance , We try to put the calculation into vertex shaders . Secondly, the algorithm should be simplified as much as possible in the fragment shader , Save money .
边栏推荐
- 数据在内存中的存储
- External interrupt to realize key experiment
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- MySQL master-slave delay solution
- C语言指针(下篇)
- Skill review of test engineer before interview
- How to pass the PMP Exam in a short time?
- Summary of PMP learning materials
- Three updates to build applications for different types of devices | 2022 i/o key review
- Unittest simple project
猜你喜欢
JVM 内存结构 详细学习笔记(一)
信息安全实验三 :PGP邮件加密软件的使用
How long does the PMP usually need to prepare for the exam in advance?
Jmeters use
H3C vxlan configuration
C语言指针(下篇)
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
Variable parameter of variable length function
MySQL common statements
MySQL master-slave delay solution
随机推荐
How to use Arthas to view class variable values
PMP certificate preparation experience sharing
四、机器学习基础
The use of recycling ideas
Where is the answer? action config/Interceptor/class/servlet
C语言指针(特别篇)
Schema-validation: wrong column type encountered in column XXX in table XXX
Jenkins modifies the system time
Port occupation troubleshooting
Port multiplexing and re imaging
JVM garbage collection detailed learning notes (II)
【Istio Network CRD VirtualService、Envoyfilter】
Chaosblade: introduction to chaos Engineering (I)
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
Selenium mouse sliding operation event
Postman setting environment variables
Analysis of Hessian serialization principle
Difference between interface iterator and iteratable
C language pointer (Part 2)
2020 year end summary