当前位置:网站首页>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 .
边栏推荐
- JVM garbage collection detailed learning notes (II)
- PMP Exam Preparation experience, seek common ground while reserving differences, and successfully pass the exam
- Postman setting environment variables
- MySql数据库-事务-学习笔记
- STM32 serial port register library function configuration method
- C语言指针(特别篇)
- Cesium load vector data
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- Confitest of fixture py
- Locust performance test 2 (interface request)
猜你喜欢
Several stages of PMP preparation study
Postman setting environment variables
Mysql database transaction learning notes
C语言指针(习题篇)
【SVN】SVN是什么?怎么使用?
超十万字_超详细SSM整合实践_手动实现权限管理
Huawei HCIP - datacom - Core 03 jours
Huawei hcip datacom core_ 03day
Pytest installation (command line installation)
STM32 serial port register library function configuration method
随机推荐
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
Interface test API case, data and interface separation
C language pointer (Part 2)
Regular matching starts with XXX and ends with XXX
PMP experience learning and sharing process
How can I apply for a PMP certificate?
Cmake command line use
Self awakening from a 30-year-old female programmer
Postman data driven
Systick滴答定时器
Do you have any certificates with high gold content?
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
Systick tick timer
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
Skill review of test engineer before interview
Original collection of hardware bear (updated on May 2022)
Mysql database transaction learning notes
Port occupation troubleshooting
Chaosblade: introduction to chaos Engineering (I)
What is the value of getting a PMP certificate?