当前位置:网站首页>Unity shader learning (I) understanding the basic structure of unity shader
Unity shader learning (I) understanding the basic structure of unity shader
2022-06-27 11:18:00 【ToDoNothing】
I'm going to learn it seriously recently Unity Of Shader, It will be used in future projects , Although it is said to use other people's shader Very comfortable , But no matter how easy it is to use, it is also someone else's , I can't understand , So you have to understand it yourself . Let's get to know each other today shader Structure , And I am right. unity shader Simple understanding of rendering .
1. Simple understanding of the computer rendering process
First of all, learn shader, You must understand the rendering process of the computer , Here I recommend you to Baidu yourself , The following references :
Unity Technical art
Computer rendering process
Simple understanding of the computer rendering process
Conceptually, we generally divide the rendering process of a computer into several stages :
1. Application stage : Mainly from CPU Process light 、 Data such as models , Entity data forming point, line and plane .
2. Geometric stage : Processing point, line and plane information .
3. Grating stage : Add texture 、 Color, etc. .
( Be careful : The above stage is to simplify the process , The actual operation is more complicated )
2. know Unity Shader Structure
open Unity, Create a shader , You can see that the default shader is standard pbr shader Rendering mode
So let's create another one shader, Right click the mouse 》Shader》 Choose any one , Here we choose the one without light Unlit Shader, Create the following :
Double-click to open shader, What I'm using here is VScode To write Shader,Unity Of Shader It uses shaderlab Language , Encapsulate the underlying interface , Just call it directly , Compatible with all platforms .
Shader "Unlit/shader1"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {
}
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
The top one is Shader Naming operations for , Don't care
Shader "Unlit/shader1"
And then there was Properties block , Here are the predefined parameter variables , Can pass Unity Panel adjustment
Properties
{
_MainTex ("Texture", 2D) = "white" {
}
}
The main form of definition is
_Name (“Display Name”,Type) = Default Value
Type Types include :
Color Color
Int Integers
Float Floating point numbers
Vector Four dimensional number
2D texture
3D texture
Cube Cubic texture
SubShader List of similar renderings , You can provide a variety of image quality options for rendering
Tags: Set up special rendering methods , For example, for transparent objects 、 Rendering of translucent objects
LOD: Set the detail control of rendering , Processing refers to the processing of equipment performance , You can leave it at that
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass block , All the rendering is done here , from CGPROGRAM Start ,ENDCG end
Generally speaking , There can be multiple Pass
Pass
{
CGPROGRAM
ENDCG
}
#pragma vertex/fragment name They are the naming of vertex shader and slice shader respectively
Vertex shaders and slice shaders are defined , We need to deal with this ,
See the comments in the code for details
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// introduce UnityCG
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
// Vertex shader processing in the geometric phase , You need to input the data of an application stage appdata, That is, point, line, surface and other information
// Process after input , Returns the data used by a slice shader v2f
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
// Grating stage , Enter a structure that needs rasterization v2f, Return color information
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
The above stage , In summary :
vert Processed the information of points
frag Process the point information and output the color
I don't want to know the details , In the next section, let's write our own code
边栏推荐
- Mqtt protocol stack principle and interaction flow chart
- 21:第三章:开发通行证服务:4:进一步完善【发送短信,接口】;(在【发送短信,接口】中,调用阿里云短信服务和redis服务;一种设计思想:BaseController;)
- ECMAScript 6(es6)
- 如何在 Methodot 中部署 JupyterLab?
- 面试突击60:什么情况会导致 MySQL 索引失效?
- [tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction
- matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
- NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
- C語言0長度數組的妙用
- QStyle类用法总结(二)
猜你喜欢
C语言0长度数组的妙用
Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
Build the Internet of things system from scratch
Red envelope rain: a wonderful encounter between redis and Lua
Glide缓存机制
机器学习系统在生产中的挑战
[worthy of collection] centos7 installation MySQL complete operation command
Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company
政策关注 | 加快构建数据基础制度,维护国家数据安全
JSP自定义标签
随机推荐
0 basic understanding of how e-commerce systems connect with payment channels
Oracle-分组统计查询
[tcapulusdb knowledge base] Introduction to tcapulusdb system management
VPT模型视频讲解
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
杰理之IO 口中断使用注意事项【篇】
【Methodot 专题】什么样的低代码平台更适合开发者?
Imeta: a collection of imagegp+ video tutorials of high-value drawing websites, which has been cited 360 times (220625 updates)
Code for structural design of proe/creo household appliances - electric frying pan
QStyle实现自绘界面项目实战(一)
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
中科院微生物所招聘青年PI 20比特,2百萬安家費,千萬啟動經費(長期有效)
KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
Jerry's adding timer interrupt [chapter]
Jerry's constant feeding of dogs will cause frequent switch interruptions leading to timer [chapter]
Uniform Asymptotics by Alexei
LLVM系列(1)- LLVM简介
进程间通信详解
Leetcode 729. 我的日程安排表 I(提供一种思路)
What is the experience of telecommuting in a foreign company| Community essay solicitation