"Unity Shader"--Getting Started knowledge points

Source: Internet
Author: User
Tags float range


Shader declaration ("First name")
Shader "Shaderdiffuseexample"
{
First, attribute definition (function: external incoming parameter)
Attribute definition syntax: propname ("DisplayName", PropType) = defaultvalue[{options}]
Properties
{
PropName: Property name, which is the name of the external incoming data variable for the current shader.

The name of the parameter displayed in the Displayname:unity material editor, which can be used as the key when the script is passed to assign a value to the property.

PropType: The type of the property, Unityshader the available types are:
Color: Colors, with rgba four volume to define.
A second-order (2 n power pixel) size map that will be color-assigned by the model vertex Uvs after it is sampled.
Rect: A non-second-order map.
Cube: That is the cube map texture (cube texture); cube texture is a combination of 6 related 2D maps,
It is mainly used for reflection effects (such as sky boxes and dynamic reflections) and is also sampled according to corresponding coordinates.
Range (Min,max): A floating-point number that is between the minimum and the maximum
float: Any one floating point number
Vector: Any one-dimensional number

DefaultValue: Default initial value of the property (these values can also be re-assigned later in code)
The Color format is (1,1,1,1)
Rect 2D cube can be used by default to represent the Tini color of the string + {}: For example "White" "Black" "gray" "Bump"
Float Range is a floating-point value
Vector format is (X,Y,Z,W)

{Options}: only for Rect 2D cube will have {}, after their initial value must add a {}, when we have the feature option is written in {}
The options are: Objectliner eyeliner SphereMap Cubereflect Cubenormal (These are the Texgen patterns in OpenGL)
}

Second, child shader (can have more than one child shader)
(1) shader is divided into four kinds:
1. Fixed function shader fixing shader
2, Vertex shader and Fragment shader fixed-point shader and fragment shader
3. Surface shader Face Shader
4, Compute Shader This is the Unity3d new one. Look at the introduction of the Niang Encyclopedia
(2) The first three kinds of shader different points:
1, Vertex shader, Fragment shader, Surface shader can achieve the fixed function shader do not achieve advanced features
2, Vertex shader, Fragment shader, Fixed function shader shader in the main body pass{}; Serface Shader
The relevant content has been packaged in the light model, there is no pass{}.
3, Fixed function shader after each sentence code does not need to add ";" the other 2 kinds of shader must be added ";"
4, their core structure is different:
Fixed function Shader core structure is material{} and settexture[_maintex]{}
Surface Shader Core structure is
① uses Unity's own lighting model Lightmodel (such as Lambert), and does not vertex processing, only requires a function surf processing.
Cgprogram
#pragma surface surf Lambert
Endcg
② uses its own lighting model Lightmodel, and uses vertex processing functions vert
Cgproram
Surface finish function Lighting model vertex processing: functions
#pragma surface surf Lightmodel vertex:vert
Execution order: Vertex processing function, surface processing function, lighting model function, color value
Endcg
The core structure of Vertex shader and Fragment shader is
Cgprogram
#pragma vertex vert
#prgrma Fragment Frag
#include "Unity.cginc"
Endcg

Subshader
{
1, tags decoration: the hardware will determine the definition of these tags to determine when to call the shader. Tags actually imply the output of shader.
"Rendertype" = "opaque": called when rendering a non-transparent object
"Rendertype" = "opaque": called when rendering a transparent object
"Ignorprojector" = "True": Not affected by projector
"Forcenoshadowcasting" = "True": Never Produces a shadow
"Queue" = "XXX": Specifies the render queue (render priority), and the predefined queue is:
Backgroud = 1000 first called rendering (for Sky box or background)
Geometry = 2000 default value for rendering non-transparent objects (most of the scenes are with this)
Alphatest = 2450 is used to render alphatest pixels (a single queue for alphatest is considered for efficiency)
Transparent = 3000 to render a transparent object from a backward forward order
Overlay = 4000 is used to render the overlay effect, which is the final stage of rendering (for example, rendering a lens flare effect)
Attached: In addition to using the predefined values above, you can also define the queue value yourself (such as "queue" = Transparent + 100, which is called on the 100 queue after Transparen)

Example: tags{"rendertype" = "opaque"}

2, Lod value: It is the level of detail abbreviation, in fact, unity built diffuse shader set value is 200.
This value determines what kind of shader we can use. In Unity's quality settings we can set the maximum allowable Lod,
This subshader will not be available when the LOD set is less than the LOD specified by Subshader.

Unity built-in shader defines a set of LOD values that we can use as a reference to set our own LOD values when we implement our own shader.
This allows for more precise control when adjusting the picture quality according to the device's graphics performance.
Vertexlit and its series = 100
Decal, reflective vertexlit = 150
Diffuse = 200
Diffuse Detail, reflective bumped unlit, reflective bumped vertexlit = 250
bumped, specular = 300
Bumped specular = 400
Parallax = 500
Parallax Specular = 600

Example: LOD 200


3, the main body of CG program (must be included in the Cgprogram ... Between ENDCG)

(1) Surface shader: #pragma surface surfacefuction Lightmodel[optionalparams]
Declares that you want to write a surface shader, a surface shader function name, and a specified lighting model [can be supplied with parameters]
#pragma declaring shader keywords
Surface claim shader type is a face shader
Surfacefuction Surface shader Function name
Lighting models used by Lightmodel
#pragma target 3.0 for compiling the Unityshadermodel version


Cgprogram
#pragma surface surf Lambert
#pragma vertex vert
#pragma fragment Frag
Endcg

}

Rollback (rollback If none of the above child shaders can execute)

Example: FallBack "Diffuse"
}

"Unity Shader"--Getting Started knowledge points

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.