Unity Shader basic types and structures

Source: Internet
Author: User
Tags float double

Recently saw Siki Teacher's shader tutorial, thank Siki Teacher, lectures really good. Before reading some shader books, because there is no basis for the study of graphics, so look very painful. Then saw the Siki teacher's video, combined with the previous reading of the book suddenly clear.

Here I write down the code of notes and videos to make it easy for me to view later.

In this article, we mainly introduce the basic types of unity shader and the two built-in methods. The code has a lot of comments, if you want to learn more in-depth suggestions to see Siki teacher's video, speak very clearly. It's not a place to be.

//This specifies the name of the shader and does not require consistency with the file name.Shader"Tmoon/01-basic Shader" {    //properties that are similar to C # can be copied in the panel to modifyproperties{//Property name ("Panel display name", type of property) = (default value of the property)//Here is a basic example of shader all attributes (types)_color ("Color", Color) = (1,1,1,1)//Float Type_vector ("Vector", Vector) = (1,2,3,4)//Float Type_int ("Int", Int) =1234 //Float Type_float ("Float", Float) =1.2 //Float Type_range ("Range", Range (1, One)) =6 //Float Type_2d ("Texture", 2D) ="Red"{}//sampler2d Type_3d ("Texture", 3D) ="Black"{}//Sampler3d Type_cube ("Cube", Cube) =" White"{}//Samplercube Type example: Sky box, 6 sides    }    //Subshader can write a lot of graphics card running effect, starting from the first Subshader//if the effect in the first Subshader can be achieved, then the first subshader is used,//If the video card has some effect in this subshader, it will automatically run the next Subshadersubshader{//Unity-like Monobehaviour,shader all the code is put in the pass execution//so there's at least one pass .pass{//write the shader code hereCgprogram//the value of properties needs to be declared here again before it can be used in shader.//Here's a detail, like C # int float double type, they're different precision//Shader also has a different type of precision for numbers, float half fixed//float 32-bit half 16-bit fixed 11-bit (-2 to +2)//so the color can be stored with a fixedfixed4 _color;            FLOAT4 _vector; float_int; float_float;            Half _range;            Sampler2d _2d;            Sampler3d _3d;            Samplercube _cube; //a start update like Monobehaviour these built-in methods//just shader can let us customize these method parameters, return values, method names. //Vertex declares the vertex function vert as the function name//the fundamental role of vertex functions: the transition from model space to clipping space for vertex coordinates (from the game environment to the field of view camera screen)//Call Timing: How many vertices are called on the model            #pragmaVertex vert//fragment declare the frag of the slice function as the name of the function//element function Basic function: Returns the color value of each pixel on the screen corresponding to the model (display model)//Call Timing: How many pixels are called on the model, so the number of calls and details is greater than the vertex function            #pragmaFragment Frag//General parameter Transfer method (more direct) after the use of the structure to pass the value//tell the system through semantics, what do I do with this argument ?//like position is telling the system that I need to automatically pass in the vertex coordinates of the model//sv_position This semantics is used to explain the return value of the description, meaning that the return value is the vertex coordinate under the clipping spacefloat4 Vert (float4 v:position): sv_position{//transform the vertex coordinates of a model into a clipping space (knowledge of linear algebra, or remember that the function of a matrix is generally used to transform coordinates) through matrix transformations                returnMul (UNITY_MATRIX_MVP,V); } float4 Frag (): sv_target{//returns the color value for each pixel of the model                returnFIXED4 (0.5,0.5,1,1); } ENDCG}}//if all Subshader fail then use Vertexlit here is the system defaultFallback"Vertexlit"}
01-basic Shader

The writing of shader can be basically populated by this structure.

Unity Shader basic types and structures

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.