Introduction of shader development of Unity3d

Source: Internet
Author: User

Shader is a piece of code that provides algorithms for specific processing stages in a rendering pipeline. The advent of shader is accompanied by a programmable rendering pipeline that enables game developers to control the rendering process and to have a greater creative space, so the emergence of shader can be seen as a revolution in real-time rendering techniques.

For the convenience of game developers, Unity offers a number of built-in shader, ranging from the simplest vertex lighting effects to the most commonly used material effects in high-light, normal, and reflective games.

The built-in shader can be divided into the following categories according to the Application object:

V normal normal Shader Family: for opaque objects.

V Transparent transparent Shader Family: for transparent objects.

V Transparent cutout effect transparent cutout Shader Family: A Translucent object that contains a fully transparent part.

V Spontaneous Light self-illuminated Shader Family: For objects that have a glow effect.

V-Reflective Reflective Shader Family: An opaque object that can reflect the environment cube map.

How to choose the most suitable shader in the shader of different effects is the problem that developers need to consider, after all, the better the visual effect of shader, the larger the general rendering overhead, and the higher the hardware requirements. Game developers need to strike a balance between game graphics and game performance.

The following are the different lighting effects from low to high computational overhead sorting;

Unlit: Applies only to texture colors and is not affected by lighting.

Vertexlit: Vertex illumination.

Diffuse: Diffuse reflection.

Specular: Increases the high-light calculation on the basis of diffuse reflection.

Normal mapped: Normal map, added a discovery map and several shader directives.

Normal mapped specular: with highlight normal map.

Parallax normal Mapped: Parallax normal mapping increases the computational overhead of parallax mapping.

Parallax normal Mapped specular: with a high-light parallax normal map.

three types of custom shader

In unity, developers can write 3 types of shader:

U surface shader surface Shaders: Typically users will use this shader, which can interact with lights, shadows, and projectors. The surface shader has a high level of abstraction, which makes it easy to implement complex shader effects in a concise manner. The surface shader works in both forward rendering and deferred rendering mode. The surface shader is written in CG/HLSL language.

ü Vertex and fragment shader vertex and fragment Shaders: You can use vertex and fragment shaders if you need a cool effect that some surface shaders can't handle, or if you're writing a shader that doesn't need to interact with the light, or if you want a full-screen image effect. This shader can be very flexible to achieve the desired effect, but it requires more code and is difficult to integrate seamlessly with Unity's rendering pipeline. Vertex and fragment shaders are also written in CG/HLSL language.

ü Fixed functional line shader fixed function Shaders: If the game is running on older hardware that does not support a programmable pipeline, you will need to write this shader. Fixed-function pipeline shaders can be used as an alternative to fragments or surface shaders, and when the hardware doesn't run those cool shader, you can also draw some basic content by fixing the functional pipeline shader. The fixed-function pipeline shader is written entirely in Shaderlab language, similar to Microsoft's effects or Nvidia's CGFX.

No matter what kind of shader code you write, you need to embed it in the Shaderlab code, and unity needs to organize the shader structure by Shaderlab code.

Shader Language currently has three languages: OpenGL- based GLSL, Direct3D-based HLSL, and Nvidia's CG language .

HLSL full name is "High level shading Language"

CG Full name is "C for Graphic"

GLSL full name is "OpenGL shading Language"

Shader Language principle

Programs written using shader language are called shader program coloring programs. The shader program is divided into two categories: vertex shader program vertex shader and fragment shader program fragment shader.

There are two components on the GPU: Programmable Vertex processor programmable Point processor, also known as vertex shader, Programmable fragment processor programmable fragment processor, also known as fragment shader.

Vertex and fragment processors have very powerful parallel computing capabilities and are very good at matrices (no higher than 4-step) calculations, and fragment processors can also query texture information at high speeds (the vertex processor is not yet, which is a development direction for the vertex processor).

The vertex shader controls the vertex coordinate conversion process, and the fragment shader controls the pixel color calculation process. The output of the former is the input of the latter.

The input/output of programmable graphics hardware at this stage. Input registers are stored in the INPUT element information, the output register is stored in the processed entity information, texture buffer is stored in texture data, most of the programmable graphics hardware is only supported by the Fragment processor processing texture; constants entered from external host programs are placed in the constant register Temporary registers hold temporary data generated by the coloring program during execution.

Shader language is positioned as a high-level language, but not hardware independent, but completely dependent on the GPU architecture, so the development of GPU programming technology is essentially the development of graphics hardware.

eg

1Shader"MyShader"{//name of the shader2 3 properties{4 5_mytexture ("My Texture", 2D) =" White"{}6 7 //Here you define the properties used in shader, such as colors, vectors, textures8 9 }Ten  One subshader{ A  - //write the implementation code for shader here -  the //CG/HLSL code including surface shader, vertex, and fragment shader -  - //or the Shaderlab code for the fixed functional pipeline shader. -  + } -  + subshader{ A  at //a simplified version of the alternative shader is implemented here to run on older hardware that does not support advanced shader features -  - } -  -}
Create shader

Before you start writing shader code in unity, you need to create a shader file in Project engineering, just as you would write a script.

Shaderlab Basic Syntax

A shader language called Shaderlab is provided in unity to write shader, which is similar to the CG language, which describes the various features required by a material and can be easily viewed and modified through Inspector views.

eg

1Shader"Simple colored lighting"{2 3properties{//defines a color property that is named Main color4 5_color ("Main Color", Color) = (1,0.5,0.5,1)6 7 }8 9subshader{//implementation code for the shaderTen  One pass{ A  - material{ -  the Diffuse[_color] -  - } -  + Lighting on -  + } A  at } -  -}

The example works as follows:

Note: The game body in unity cannot add shader to the default material. To use a custom shader, you first need to customize a material ball before you can use the custom shader.

Introduction of shader development of Unity3d

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.