This article starting address: HTTP://98JY.NET/ARTICLE/17
More and more timely articles can be seen at the above address
I. Framework of Shader
Shader starts with the keyword SHader plus the string enclosed in double quotes, which can be used in the string Represents the classification that is displayed in the inspector. The entire shader code is wrapped in {} , which is later in this section. For example, a typical shader would be like this
Shader "Name" {} or Shader "Category/name" {}
Ii. attributes in shader (properties)
In unity, a shader attribute is important, because attributes allow the artist or user to add materials, change values, and alter the display of the shader without code. The Inspector property provides a visual, WYSIWYG implementation of modifications to related shader behavior by providing the relevant controls on the panel.
For convenience, we pasted the shader code from the last lesson below:
Shader "Cookbookshaders/basicdiffuse" {Properties{_maintex ("Base (RGB)", 2D) = "White" {}}subshader{tags {"Rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf lambertsampler2d _maintex;struct input{float2 Uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} Endcg}fallback "Diffuse"}
from the properties of line 3rd to line 6th, we call this block "Property code block". Currently, it has only one property: _maintex. If you apply our shader material in unity, you can see a control on the Inspector panel that allows you to select a picture, such as:
You can use the Select button in the lower right corner to select an image from the project, or you can drag the picture from the project panel directly into the box where the select is located. The control box here (which contains the base (RGB) of its left-hand side), is the code in the shader properties {_maintext ...} so that unity is automatically generated for us.
As a result, we can use this method of unity to pass the external "variables" shader needs as attributes to shader, thus reducing the amount of code churn.
Hands-On Labs
Open our first shader with Monodevelo and remove the original line from the properties:
_maintext ("Base (RGB)", 2D) = "White" {}
Then add the following code to save
_emissivecolor ("emissive color", color) = (1, 1, 1, 1)
Go back to unity and wait for unity to compile our shader. Please ensure that you have selected the application of our shader Material,inspector panel will become, click on the white part of the picture will pop up the dialog box for us to choose a different color.
Back in the shader code, let's add this line of code:
_somevalue ("Some Value", Range (0, 10)) = 2
The related panel will come out with a slider, allowing us to swipe left and right to determine a value within a certain range
The format of the code in properties
Each unity shader is set by unity in a certain format. The properties block is one of them. This block is designed to give shader programmers a handy way to get unity to help create a control that fits your code without the programmer having to care how it is displayed (Unity also provides a way for programmers to play freely if they want to). The code in the properties block needs to conform to the following format:
The name "variable" is referenced in other shader code to get the value of the variable.
"Inspector Display Name" is the name that our property displays on the Inspector panel.
"Type" is the kind of variable belonging to, and also determines the corresponding control of the variable on the Inspector panel.
The "Default value" is the initialization value of the variable, and its notation is also related to the type of the variable.
The following table describes the "types" that we can use in shader:
Range (min, max) |
Creates a slider bar that allows the user to select a floating-point value between Min and Max |
Color |
Create a palette selector that lets the user choose a color |
The |
Create a picture selection box to let the user select a decal |
Rect |
Create a non-power-of-2 map selection box, the function basically with 2D want to |
Cube |
Create a box to select Cubmap |
Float |
Create a field that lets users fill in floating-point numbers |
Vector |
Create 4 fields that allow the user to fill in the corresponding floating-point number, representing a Vecto4 |
Then look at the "default" notation:
- Range and float, a floating-point number
- Color and vector,4 A comma-separated number, wrapped in (). corresponding to color, these four are the normalized values of R, G, B, and a (values range from 0 to 1, representing the common range of color values from 0 to 255). Corresponds to the vector, which is the x, Y, Z, w values.
- A null string or a default map name inside Unity:"White", "Black", "gray" or "bump" .
Attention:
The default value of 2D, Rect, and Cube can be followed by {}, like this
Name ("Display Name", 2D) = "name" {options}
This section is optional. If desired, the choice here is two modes:
- TexgenTexgenmode: automatically generates the UV coordinates of the map. The Texgenmode parameter can be:Objectlinear, Eyelinear, SphereMap, Cubereflect, Cubenormal; correspond directly to OpenGL's Texgen mode. If the user uses a custom vertex method, then Texgen will be ignored by unity.
- Lightmapmode indicates unity, and our diagram here will come from each rerderer component instead of being specified directly in material.
Unity Shader Tutorial-Lesson two: Shader's framework and properties in detail