Unity Shader Getting Started

Source: Internet
Author: User
Tags dashed line

This article is my study of Bull's set of shader tutorial (HTTP://WWW.UNITYTRAIN.CN/COURSE/96) After the simple summary, personal feeling this tutorial is not to advanced shader programming for the purpose, more like the purpose of giving people to fish. Below I will be divided into three parts: shader brief introduction, the basis of graphics, CG introduction for everyone to introduce shader related content, also is to do a summary.

A: Shader Brief introduction

A. First of all, the difference between GPU and CPU, simply said: The GPU is primarily responsible for displaying related data processing, while the CPU is primarily responsible for operating systems and applications. Why not just hand over the display-related data to the CPU? The explanations in the tutorial are attached below:

B.shader classification. Shader Chinese translates to "shader", meaning: Programmable graphics pipeline. Mainly divided into: Vertex Shader and Fragment Shader, that is, fixed-point Shader and fragment Shader. There is a concept is "graphics pipeline", the simple explanation is: computer processing graphics display processing line.

C.shader's mainstream programming language. The main shader programming languages are HLSL, GLSL and CG. Here's a simple distinction: HLSL (High Level Shader Language) is a Microsoft DX-based work that can only be run on the Windows platform. GLSL (OpenGL shading Language), OpenGL coloring language, is the language used to color programming in OpenGL (OpenGL is a professional graphics program interface that defines a cross-programming language, a cross-platform programming Interface Specification), and is a cross-platform shader language. Here, we can already find that there is a more troublesome problem arises, that is, our underlying graphics drive limits the upper-level programming language, once you want to change the graphics drive library, it will have to rewrite the entire shader Files, when the CG came into being, CG has been further encapsulated on HLSL and GLSL, shielding the upper shader language from dependency on the underlying graphics library.

D.unity Shader. Shaderlab is actually a package of unity's syntax structure for Shader, which supports three shader:surface Shader, Vertex and Fragment Shader and fixed function Shader fixed Function shader is a relatively "conservative" shader (compatibility best), vertex and fragment shader can only be written in HLSL or GLSL or CG language areas, surface shader is vertex and Fragment is a grammatical wrapper that will eventually be translated into vertex and fragment Shader. (Refer to the official document Http://docs.unity3d.com/Manual/index.html for more specific information above)

Second: Fundamentals of Graphic Science

Personal feeling the content of this section is valuable for people who have not been exposed to graphics, and clears the previous blind spots for coordinate conversion and rendering processes in untiy. Here are two sections to describe the contents of this section.

A.3d mathematical basis. In fact, 3D mathematics is nothing more than a matrix of related operations, for students who have learned the line is not a problem, I will briefly introduce here.

1. coordinate system and vector. 3D is divided into left hand and right hand coordinate system, can refer to (http://www.cnblogs.com/mythou/p/3327046.html), as follows:

2. Vector-related things are not verbose, more important is the vector point multiplication and cross-multiplication, here is attached to the reference article (http://blog.csdn.net/augusdi/article/details/20037851).

3. Matrix-related. In 3D mathematics, matrices often represent a transformation, which is also the mathematical principle on which coordinate system transformations depend. We've all heard of the MVP matrix in Unity, and the MVP matrix is actually a way of transforming the coordinate system through the matrix operation. In unity, there are 3 of four coordinate systems: Model coordinate system, world coordinate system, camera coordinate system, and screen coordinate system. This is actually a process of 3D image display: From the model itself coordinates using _objecttoworld (i.e. M matrix) to the world coordinate system, and then using _worldtocamera (i.e. v matrix) from the world coordinate system to the camera coordinate system, and finally using _projection ( P matrix) to convert from the camera to the screen coordinate system, and finally display the 3D image on the screen, the following is attached to a Baidu library information (http://wenku.baidu.com/link?url= A3AGV805UK5RCSEJKAL1H6QJNXSKTVCSCYNJQAHVFE2CIHWXMAM6ZZH4GXBU_XB7JD7RIPXJD0ER51Q6CPT9XPXTIX3MEHTFAWKWEXBLZTI).

Several important operations of matrices are: determinant of matrix, transpose of matrix, matrix arithmetic, inverse matrix ... This knowledge is not verbose here, the following is a brief introduction of several common matrix transformations.

Rotate a matrix around an axis:

Scaling matrix:

Projection matrix

Translation matrix

The above is a few more commonly used matrices, more information on the degree of Niang and Google.

B. Here are a few simple graphical applications: illumination culling, diffuse reflection, and high-light implementation.

1. Illumination culling. Here we must understand the concept of "normal" (always perpendicular to a plane of the dashed line), our perspective is from the object to the camera's vector, if the normal n and the angle of view E is less than 90 degrees, then the observer should be about the front, and vice versa greater than 90 degrees should be on the opposite side of the face, At this point should be unable to observe the object (the normal of the use of vector difference multiplication, the angle calculation can use the vector point multiplication), this time it is necessary to remove it, the following two pictures simply explain:

2. Diffuse reflection (diffuse is the phenomenon of light reflected in various directions on the surface of several box bodies), which can be easily understood as the effect of light on the surface color of an object (the default shader in unity is the synthesis of diffuse and ambient light). So how do you calculate how much light affects the color of an object? At this point, we still need to use the normal, we use the normal and light vector (must be standardized) point multiplication as a factor affecting the color of the region, and then multiplied by the color information of the light source can be corresponding to the color affected by the light, the following illustrated with a diagram:

3. High-Gloss (specular light source to the object and then reflected into the eyes of the person, the brightest point on the object is the highlight), from the definition can be derived from the light and reflected light and the visual angle of interaction formed, the same we in the calculation of high light is also the use of the same principle: from the incident light to the reflection Then calculate the influence factor by the point multiplication of the reflected light and the apparent vector, and finally calculate the high light intensity, the diagram illustrates:

The above is three kinds of more common lighting-related knowledge, more information can only rely on the degree Niang ...

Unity Shader Getting Started

Related Article

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.