当前位置:网站首页>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 .
边栏推荐
- NVIC中断优先级管理
- Schema-validation: wrong column type encountered in column XXX in table XXX
- Mysql数据库-锁-学习笔记
- PMP experience learning and sharing process
- MySQL master-slave delay solution
- Systick tick timer
- Confitest of fixture py
- Idea development environment installation
- STM32 serial port register library function configuration method
- Expérience de port série - simple réception et réception de données
猜你喜欢

Skill review of test engineer before interview

Serial port experiment - simple data sending and receiving

Expérience de port série - simple réception et réception de données

Cesium load vector data

串口實驗——簡單數據收發

Do you have any certificates with high gold content?

MySql数据库-事务-学习笔记

C language pointer (Part 2)

C语言指针(上篇)

十二、排序
随机推荐
STM32 clock system
C语言指针(特别篇)
Difference between interface iterator and iteratable
Interface test API case, data and interface separation
信息安全实验一:DES加密算法的实现
Systick tick timer
MySql数据库-事务-学习笔记
Huawei hcip datacom core_ 03day
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
【Istio Network CRD VirtualService、Envoyfilter】
Pycharm importing third-party libraries
External interrupt to realize key experiment
Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
Jenkins+ant+jmeter use
C language pointer (special article)
NVIC interrupt priority management
JVM 内存结构 详细学习笔记(一)
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
What are the conditions for applying for NPDP?
Cesium load vector data