Unity's Shader Learning notes (i)

Source: Internet
Author: User
Tags parent directory split

began to learn unity also has one months, before you want to learn things summed up, recorded to facilitate their own later review, but work too busy always forget this thing, recently finally have time, intend to learn the things recorded every day. There are two reasons to write this article: 1. Many of the ideas are thought to be at school, but they have been forgotten for some time. 2. Used to look at other people's blog, and then suddenly found no other people's blogs, and then some content re-find feel very troublesome, then simply put their own ideas recorded, convenient for later review.

Shader is one of the more important things in unity, the output of shader and textures (texture) will be a material (material), the material will be given to an object, an object can see the effect we want.

Create a new shader file, select Standard surface shader,

Double-click the open file to see what's Inside:


Shader "Custom/newsurfaceshader" {
Properties {
_color ("Color", color) = (1,1,1,1)
_maintex ("Albedo (RGB)", 2D) = "White" {}
_glossiness ("Smoothness", Range (0,1)) = 0.5
_metallic ("Metallic", Range (0,1)) = 0.0
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200

Cgprogram
Physically based standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows


Use Shader Model 3.0 target, to get nicer looking lighting
#pragma target 3.0

Sampler2d _maintex;

struct Input {
FLOAT2 Uv_maintex;
};

Half _glossiness;
Half _metallic;
Fixed4 _color;

void Surf (Input in, InOut surfaceoutputstandard o) {
Albedo comes from a texture tinted by color
Fixed4 C = tex2d (_maintex, In.uv_maintex) * _COLOR;
O.albedo = C.rgb;
Metallic and smoothness come from slider variables
O.metallic = _metallic;
o.smoothness = _glossiness;
O.alpha = C.A;
}
Endcg
}
FallBack "Diffuse"
}

The outermost frame of the code in the file is
Shader "" {}
In fact, "" in quotation marks can enter any content, the input is your custom shader name, the name and file name does not have any relationship, the name can be used/split, if used/split, then/the previous content is the file's parent directory, this can be used for classification management.
The contents of the curly braces: divided into properties, Subshader and fallback three chunks
Properties{}
The properties of the shader defined in this module, all of which are declared within the module, and the variables declared can be seen in the Inspector panel. The property types and properties of shader are defined in the following ways:


Property definition Structure
Property name (name, property type) default value displayed on the Properties panel
Property type

Color colors

_color ("Color", color) = (1,1,1,1)

Vector Vectors (default four-dimensional vector)

_vector ("vector", vector) = (1,1,1,1)

int integer type

_int ("int", int) = 2

Float Decimal Type

_float ("float", float) = 4.5 range Type parentheses are the maximum and minimum values and are automatically adjusted to the maximum number in parentheses if the value exceeds the maximum value

_range ("Range", Range (1,11)) = 6

The picture type "" Inside can give the color of the worth 2D did not make a picture when it will show "" inside the color set {} Inside to make a specific texture

_2d ("Texture", 2D) = "" "{}

Cube cube texture Similar to skybox sky box texture

_cube ("Cube", cube) = "" "{}

3D Textures Use Less

_3d ("3DTexture", 3D) = "" "{}



Semantic block Subshader
This is the most important content in shader, because the grammar control inside the computer is how the GPU renders objects, which determines the effects of the objects we will see. A shader inside is a plurality of subshader, shader loading will load from top to bottom loading subshader content, first load the contents of the first Subshader, if the first subshader inside the effect can be achieved, Then use the first Subshader, if the video card found that the subshader inside a certain effect it does not implement, he will automatically run the next subshader. Subshader defines a series of passes (which can be thought of as a method function in a high-level language, and a subshader must contain at least one pass PS: It's a bit odd why there's no pass in the Subshader in the example, but shader is fine), Write specific rendering details in the pass.
pass{
Writing shader code here can also be used Hlslprogram
Cgprogram
Endcg
}


Pass can be named in the pass
For example, Name "FirstPass"
The named pass can be directly referenced using the use pass in another shader.
For example Usepass "Myshader/firstpass"


In addition to the custom pass, Unity has customized some special passes to facilitate code reuse, such as:
Usepass mentioned earlier, used to refer to a pass in another shader.
Grabpass The pass is responsible for capturing the screen result and storing it as a texture for subsequent pass processing


[Rendersetup] status setting
By setting the value of the Rendersetup property we can set the various states of the video card, set the rendering status of the video card, and the common rendering state setting options are:
State name setting directives
Cull back| Font| Off set reject mode: Reject back/front/off
ZTest less greater| lequal| gequal| equal| notequal| Always set the function to use when setting the depth value test
Zwrite on| Off turn on/off depth write
Blend Blend Srcfactor Dstfactor turn on and set blending modes


Just set it up when you use it. For example, Zwrite on to turn on deep write


[tags] tab
Tags are used to tell the system how and when I want to render this object, tags can be used in the Subshader can also be used in the pass, but the two types are not the same
Subshader Types of tags
Queue is used to control which render queue the object belongs to, and the render queue can be customized, which is generally used to control all transparent objects rendered after opaque objects tags{"Queue" = "Trans"}
The rendertype is used to categorize shaders, such as classifying shaders as transparent and opaque shaders. tags{"Rendertype" = "Opaque"}
Disablebatching used to set whether batch operations are used on the Subshader tags{"disablebatching" = "True"}
Forcenoshadowcasting is used to control whether the object has a projection effect tags{"tagforcenoshadowcasting=" True "}
Ignoreprojector is used to control whether an object receives projector effects, usually for translucent objects tags{"Ignor Eprojector "=" True "}
Canusespriteatlas when the label is used for sprites, you need to set the label to False tags{"Canusespriteatlas" = "False"}
Previewtype is used to specify a preview shape for a material, the system is circular by default and can be changed to a type such as "Skybox" tags{"Previewtyp E "=" Plane "}


Pass's tags type
LIGHTMODE Specifies the pass's role in Unity's rendering pipeline tags{"Lightmode" = "Forwardbase"}


requireoptions specifies a value that renders the pass when the system satisfies the specified condition, whose value is a space-delimited string, and the currently supported options for unity are softvegetation tags{"requireoptions" = "so Ftvegetation "}


tags and rendersetup if the subshader inside the set, then all the pass will be affected, if set in the pass, then only the current pass will have an impact


Note that the two sentences Cgprogram and ENDCG cannot be forgotten, both of which mean that the code inside is using the CG syntax to write the shader, There are three kinds of syntax to write shader, one is OpenGL development, using the GLSL syntax to write shader, but this kind of more suitable for mobile phone development, the second is the development of DirectX, using HSSL syntax to write shader, This type of syntax only applies to PC-side development, and neither syntax can be used on another platform. The third is the development of shader's CG grammar, which has a good cross-platform.
in Subshader is not directly using the above-defined properties, in Subshader, we need to redefine the properties used, to convert the property to another variable, the specific conversion is as follows:
to use the property when you need to redefine the
Color   ==> float4
Vector  ==> float4
Int     ==> float
float   ==> float
Rang E   ==> float
2D      ==> sampler2d
Cube    ==> samplercube
  &nbs P  ==> Sampler3d

Note that any property that uses the float type can be replaced with half and fixed, such as
float  ==> half  ==> fixed
Float4 ==> half4 ==> fixed4
Difference:
Float uses 32 bits to store
Half   use 16 bits to store  
Fixed use 11-bit to store  
To save memory   Select the appropriate data type
  For example: colors are typically stored with fixed

Fixed4 _color;


FallBack fallback  
FallBack in the shader file is immediately behind the Subshader, The function of this semantic block is that none of the subshader of the current polygon is available, then the semantic block is executed, and the semantic block is defined as:
FallBack "Diffuse"  or FallBack Off


FallBack the contents of the string in the back of the general is a system-defined shader, so that when the previous subshader can not be used, it will invoke the Subshader property of the system, but if you are using FallBack Off, then the system It can be understood that if none of the above subshader can be used, then it is no longer possible for him to render the object.


It is important to note that if fallback specifies a system shader, if the shader of the system defines a property with the same property name that we have in properties, and uses that property, Even if we can see the property in the Inspector panel and adjust it, it doesn't affect the color of the object.


I am a newbie to learning unity, if there are any errors and incorrect places in the above article, please note. Write poorly, if sprayed, please lightly spray. ,。

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.