当前位置:网站首页>Unity shader overview
Unity shader overview
2022-07-27 06:14:00 【YF Yunfei】

Preface
Shader, The Chinese name is shader , For many developers, it is ⼀ A mysterious existence , Want to learn but don't know how to learn , Or I learned for a while and found Get Can't reach its point , Always feel free , Can't enlighten . Then this article will talk about how to get started from zero foundation Shader, The purpose is to let beginners Shader Programmers or artists can quickly enter this colorful world .
Text
What is? Shader?
Although it is simply said above Shader It's a shader , But it is still necessary to elaborate ⼀ What exactly is Shader.
Shader In fact, it is specially used to render graphics ⼀ Technology , adopt shader, We can customize the algorithm of graphics card rendering , Make the picture achieve the effect we want . Small to every ⼀ Pixels , Up to the whole screen , For example, the following two common effects in the game :


Shader There are two kinds of :
1. The vertices Shader(3D The graph is made up of ⼀ Composed of triangular patches , The vertices Shader Is to calculate the vertices on each triangular patch , And prepare for the final pixel rendering )
2. Pixels Shader, seeing the name of a thing one thinks of its function , In pixels , Calculate the light 、 Color ⼀ Series algorithm .
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. To put it bluntly ,Shader It 's just a piece of code , The purpose of this code is to tell GPU How to draw the color of each vertex of the model and the final color of each pixel .
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 :
1. be based on OpenGL Of OpenGL Shading Language, abbreviation GLSL.
2. be based on DirectX Of High Level Shading Language, abbreviation HLSL.
3. also NVIDIA The company's C for Graphic, abbreviation Cg Language .
GLSL And HLSL The difference is based on OpenGL and Direct3D The interface of , You can't mix the two . and Cg Language is used for graphics C Language , This actually shows the original intention of the designers at that time :
Make programming based on graphics hardware and C Language programming is just as convenient 、 free , Make programming based on graphics hardware and C Language programming is just as convenient 、 free .( just as C++ and Java Our grammar is based on C Of ,Cg Language itself is based on C Linguistic . If you have used C、C++、Java Any one of them , that Cg It's easy to master the grammar of )
Cg The language is strongly reserved C Most of the semantics of language , Trying to free developers from hardware details ,Cg At the same time, it has the advantages of high-level language , Such as code reusability , High readability .Cg Language is Microsoft and NVIDIA Collaboration has resulted in agreement on the syntax and semantics of standard hardware lighting languages , therefore ,HLSL and Cg It's actually the same language .
( The basic grammar can be moved to unity shader Foundation 5 Basic grammar _TO_ZRG The blog of -CSDN Blog )
Unity Shader Strictly speaking, it is not traditional Shader, It is Unity An easy to write after self encapsulation Shader, Also known as Shader Lab.
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
among Fixed Function Shaders Has been eliminated , There is no need to study at all .
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 .
One Unity Shader The basic structure of is as follows :
Shader "ShaderName"
{
Properties {// attribute }
SubShader {// The graphics card A Sub shaders used }
SubShader {// The graphics card B Sub shaders used }
}
and Unity We will compile these structures into real code and Shader file .
Materials and Unity Shader The bridge
Properties The semantic block contains a series of attributes (Property), These attributes will appear in the materials panel .
Properties { Name {"Display name", PropertyType} } = DefaultValue
among Name yes Shader Call the required properties in ,display name Is the name that appears in the material panel mountain ,PropertyType Is the type of attribute .

Answer questions and solve doubts
Unity Shader and CG/HLSL The relationship between
Unity Shader Yes, it is Shader Lab language-written , But for surface shaders and vertices / Chip shader , We can do it in ShaderLab Nested inside CG/HLSL Language to write shader code .
Usually CG The code block is located in Pass Inside the semantic block , As shown below :

GLSL
Unity Shader It also supports GLSL Compiling , But the products written can only be supported on the shelves OpenGL The platform of .
Reference documents
NVIDIA Provided CG file
NVIDIA Series of tutorials provided
Unity The official manual provides Shader Teaching documents
边栏推荐
猜你喜欢
随机推荐
Pzk's first understanding of pointer in learning C language
C语言-动态内存管理
Cmder的基础文件操作
Unity 引擎开始从 Mono 迁移到 .NET CoreCLR
Solving binary tree with force deduction (8)
Socket programming 1: using fork() to realize the most basic concurrency mode
所有常用排序的代码实现和介绍
5G网络身份识别---详解5G-GUTI
如何选择正确的服务器备份方法
Code implementation and introduction of all commonly used sorting
1 semi automatic crawler
SQL novice
Unity Shader 概述
编程学习记录——第6课【函数】
力扣每日一题leetcode 513. 找树左下角的值
【第一篇博客-展望】
Remote sensing image recognition misclassification under multi class recognition
【动态规划----钢条切割问题】
Dynamic programming for solving problems (1)
How to distinguish an independent server from a VPS host?





![[5.20 special] MATLAB, I'm confessing to you](/img/ce/ea8697db3e10a216efc9ac5e0fad8d.png)



