Unity's GPU programming problem resolution

Source: Internet
Author: User
Tags mul

in the game Bull quiz often ask questions about the shader programming aspects of unity, GPU programming is to put the fixed pipeline of various matrix transformation into the GPU.

Here are some basic common sense:

we use it frequently in shader programming. Vertex & Fragment Shaders, by illustration:

struct Vert {float4 vertex:position;      FLOAT3 Normal:normal;  FLOAT4 texcoord:texcoord0;  };      Vert Input (Vert v) {Vert o;      O.pos = Mul (UNITY_MATRIX_MVP, V.vertex);      O.UV = Transform_tex (V.texcoord, _maintex);              O.color = Shadevertexlights (V.vertex, v.normal);  return o; }

The struct vert is the defined vertex program as input. Then parse the input function

O.pos = Mul (UNITY_MATRIX_MVP, V.vertex);

By multiplying the vertex position with a matrix UNITY_MATRIX_MVP (defined in unityshadervariables.cginc) defined by unity, the vertex position is converted from model space to projection Space We used the matrix multiplication operation Mul to perform this step.

O.UV = Transform_tex (V.texcoord, _maintex);

The texture calculates its UV coordinate, which calculates the corresponding position on the true texture based on the UV coordinates on the mesh. We use the macro Transform_tex in Unity.CG.cginc.

definition of macro Transform_tex : #define TRANSFORM_TEX (Tex,name) (TEX.XY * name# #_ST. XY + name# #_ST. ZW). We need to define some extra variables in shader, that is, you must define a name of _yourtexturename_st (that is, your texture is named with a _st suffix).

Unity uses three of data to define vertex lights:unity_lightposition,unity_lightatten and Unity_lightcolor. For example [0] represents the most important light source.

When writing a multi-pass lighting model, you only need to process a single light source at a time, in which case unity also defines a value named _worldspacelightpos0 to help us get its position, It also provides a very useful function, Objspacelightdir, which can calculate the direction of the light source. In order to get the color of the light source, we can include the "lighting.cginc" file in the program and then access it using _LIGHTCOLOR0.

Here's an explanation of the macros that Unity.CG.cginc contains:

Macro tangent_space_rotation(defined in unitycg.cginc) is used to create a matrix called ROTATION, and uses it to convert object space into TANGENT space.

transfer_vertex_to_fragment macro, which is defined in Autolight.cginc, calculates the specific values of the light source coordinates based on the type of light source (spot,point,or directional) processed by the pass, as well as the calculations associated with shadow.

Finally: When we write shader, we can define multiple passes, each of which handles a light source. A pass can define a series of tags for this stage.

This article is from the "Kaiyukan mobile" blog, so be sure to keep this source http://jxwgame.blog.51cto.com/943299/1602485

Unity's GPU programming problem resolution

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.