About the use of shader in unity

Source: Internet
Author: User
Tags pixel coloring

Shader, also known as shaders, is one of the more difficult points in unity.

1:shader is a program for GPU execution, in Chinese called shaders;
2: Shader is running on the graphics processing unit, can let the developer directly manipulate the graphics hardware rendering function;
3:shader can develop a lot of good effects, UV animation, water, fog and other special effects, these programs developed (CPU) more difficult, performance is not good;
4: Rendering pipeline, model projection, fixed-point coloring;
5:shader generally include: Fixed pipeline shader, vertex element shader, surface shader;
Fixed line shader (will be eliminated slowly);
Vertex shader: The shader of intervention model morphology;
Pixel shader: Shader of intervening pixel coloring;
6: When the model fixed-point operation, the vertex shader can be added to intervene the vertex position;
When vertex coloring, add pixel shader to intervene the color of pixel;

UV Animation: On the model and then affixed a picture, and constantly change the texture of the new map coordinates, resulting in a continuous scrolling effect, like the Cocos case inside the turtle swim in the water, the body is covered by the wave of the animation

Rendering pipeline: A 3D model to get the GPU inside the rendering, there is a work pipeline, precisely because of these lines, the GPU is exposed to some interface, convenient for programmers to insert their own code in the pipeline channel. That is to support users to write their own code into the pipeline inside, to achieve the special effects users need.

Vertex shader: After the model vertex comes in, we can transform the vertex into another shape by shader, for example, we have a grid, is the kind of straight, we can let shader intervene the vertex on this grid, let it become curved like the wave of the appearance, and then paste the texture.

Pixel shader: When we image up, we want to color the point, which point to paste which texture, so we need to paint them, color can also be used in shader processing, such as the turtle's UV animation, the other texture and the original texture combined.

1: What is Direct3D and OpenGL;
2: There are currently three main GPU-oriented programming languages:
The HLSL language is a shader program written by Direct3D and can only be used inside Direct3D;
Cg languages Nvidia and Microsoft cooperate to provide the language, similar to C, Direct3D and OpenGL support;
GLSL language supports OpenGL to write shader program;
3:unity uses the Shaderlab to write the coloring program, compiles the different platforms, and focuses on supporting CG language;

What does the video card support Direct3D ...

Direct3D and OPENGL:PC History, the PC is actually inherited in the operating system above, the PC has a variety of hardware in the development, such as graphics card, the graphics card will be more and more to put a lot of graphics units on the GPU above the operation, but my graphics card although support this calculation, but how to tell the program , so the function of the graphics card to make a set of APIs, published to the program use.

Now there are two ports, one is the operating system is os:windows,linux, one is the graphics card manufacturers NVIDIA,AMD, two graphics card manufacturers, how they want to unify standards, so Microsoft proposed a standard called DirectX, Graphics standards, which defines a lot of hardware acceleration interface , the graphics card manufacturers are responsible for supporting these interfaces in the graphics driver, so when DirectX is installed, our code uses the DirectX API, because the driver of the graphics card is connected to the implementation of the underlying API, so the API can use graphics acceleration to achieve good graphics performance, This is why the graphics card always has a driver, because it is installed to the driver after the graphic image of the interface unified to connect, so that through DirectX can access the graphics card hardware acceleration.

Microsoft DirectX is not open source, and Linux OpenGL is open source, Android, Linux,ios, all support OpenGL, but not DirectX, Microsoft also support OpenGL, everyone in Windows is accustomed to using DirectX, Get used to using OpenGL on Linux

And unity is a cross-platform game engine, so using DirectX on Windows, using OpenGL on Linux.

With these two standards, the graphics card manufacturers in the form of driving, the standard access to the standard library, so that the application as long as the call API can use the graphics card acceleration, if any.

DirectX and OpenGL also support the user directly plug the program, such as plug shader to the graphics card execution, so GPU graphics also has a programming language, there are three kinds of HLSL,CG,GLSL languages

Because Unity is a cross-platform game engine, it focuses on supporting CG language, but it does not directly use CG language, but instead of a Shaderlab language to do the coloring program to write, This allows us to use Unity's shaderlab syntax to generate different shader programs for a variety of platforms.

What we call shader is a shader program written using Unity's shaderlab.

Shader Lab Syntax Basics

1: Define a shader, each coloring program must have a shader
Shader "name" {//Name Shader name
Defines some properties that are defined here and will be displayed in the property viewer;
[Propeties]
Child shader list, a shader must have at least one child shader;
Subshaders: {...}
If the child shader video card does not support, it will be degraded, that is, fallback operation;
[Fallback]
}

Properties definition

1:name ("Display name", type) = value;
Name refers to the name of the attribute, and unity begins _name with an underscore;
Display name is the name of the property inspector;
Type: Types of this property
Value: The default value for this property only;
2: Type:
Float, INT, Color (num, num, num, num) (0 ~ 1) vector (4-dimensional vector), Range (Start, end)
2d:2d Texture properties;
Rect: Rectangle Texture property;
Cube: Cube texture properties;
3d:3d Texture properties;
Name ("displayname", 2D) = "name" {options}
3:options: Texture Properties Options
Texgen: Texture generation mode, texture automatically generated texture coordinate mode, vertex shader will ignore this option;
Objectlinear, Eyelinear, SphereMap, Cubereflect cubenormal
LIGHTMAPMOD: Illumination Map Mode If you set this option, the texture will be affected by the renderer's light map.

Example

1: _range ("Range value", range (0, 1)) = 0.3; Define a range
2: _color ("Color", color) = (1, 1, 1, 1); Define a color
3: _floatvalue ("float value", float) = 1//define a floating point
4: _maintex ("Albedo", Cube) = "Skybox" {Texgen Cubereflect}//define a cubic map texture attribute;

Subshader child shader, I feel like a pipe connected to a channel

1:subshader {[Tags], [commonstate], Pass {}} child shader consists of tags (tags), general state, channel list, it defines a render channel list, and optional for all channels to initialize the required general state;

Pass is the channel, is the pipeline above one of the channels, the object model through a channel, and finally the passage of the objects processed by the drawing out.
When 2:subshader renders, it takes precedence to render an object defined by each channel.
3: Type of Channel: Regularpass, Usepass, Grabpass,
4: Define the state in the channel while the whole child shader is visible, then all the channels can share the state;

Subshader syntax

1:subshader {
Tags {"Queue", "Transparent"}
Pass {
Lighting off//off light
....
}
}

Tags

1:tags {"Label 1" = "value1" "key2" = "value2"}
2: Type of Label:
Queue tag queues tag;
Rendertype tag rendering type tag;
Disablebatching tag Disable the batch processing label;
forcenoshadowcasting tag force does not cast shadow tags;
Ignoreprojecttor ignoring the projection label;
Canusespriteatlas tag, using the wizard Atlas tag;
Previewtype tag Preview type tag;

Pass

1:subshader packaged a rendering scheme, which is executed by a channel (pass), Subshader can include many channel blocks, each Pass can render the geometry once;
2:pass Basic Syntax:
Pass {[Name and Tags] [rendersetup] [Texture Setup]}
The pass block name refers to this pass, which can be referenced in the pass block of other shaders, reducing the repetition, and the name command must be capitalized;

Regularpass Rendering Settings

1:lighting Light: Turn on off point lighting on/off
2:material{Material block}: a material that defines a material that uses a fixed-point light line;
3:colormaterial: The color set uses vertex color when calculating fixed-point illumination;
4:separatespecular: Turn state on or off vertex illumination related specular highlight color, on/off;
5:color sets the color used when the point light is turned off;
6:fog{Fog block}: Set fog parameters;
7:alphatest:alpha Test
8:ztest: Depth test mode;
9:zwrite: Deep write mode;
10:blend: Mixed Mode Sourceblendmode, Destblendmode, Alphasourcesblendmode, Alphadstblendmode;
11:colormask Color Matte: Sets the color matte, which can be set to 0 by RGB or a or 0 or r,g,b,a combination, to turn off all color channel rendering;
12:offset offset factor: set depth offset;

Special Channel Usepass

1:usepass: Inserts all channels from the given name of the other shader;
Usepass "Shader/nmae", name is the shader channel;
Usepass "Specular/base"//Insert channel for bass in specular;

Grabpass
1:grabpass {}: A special channel type that captures the contents of the screen at the location of the object and writes it to a texture that can be used to perform some advanced image effects on subsequent channels, and subsequent channels can be used
_grabtexture to visit;
2:grabpass{"Texture Name"} captures the contents of the screen into the specified texture, and subsequent channels can be accessed by the texture name;

Fallback

1: Downgrade: After all child shaders are defined, if no child shaders can run, try to downgrade;
2:fallback "Shader name";
3:fallback Off;
No downgrade, and no warning will be printed;

Category categories

1: Classification is a logical group of render commands. For example, shaders can have multiple child shaders, they all need to turn off fog effects, and mix
Shader "xxxx" {
Categroy {
Fog {Mode Off}
Subshader {...}
Subshader {...}
}
}

About the use of shader in unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.