Properties property
Shader syntax is case insensitive
basic data type
how to define a property
-
Properties are defined in the "Properties{}" code block
Properties{
_Color("Main Color",Color) = (1,1,1,1)
_Shininess("Shininess",range(0,8)) = 4
_MainTex("MainTex",2D) = ""{}
}
-
The composition of an attribute
_Color("Main Color",Color) = (1,1,1,1)
- _Color : attribute name (the name used when calling this attribute in the following code block)
- "Main Color" : In Unity, the name we see in the panel
- Color : data type
- (1,1,1,1) : the default value of the data
SubShader shader code block
There can be one or more "SubShader{}" in a Shader, but the graphics card can only select one SubShaders to execute each time it renders
The execution sequence is to process the top "SubShader{}" first. If the graphics card version does not support the first "SubShader{}" , the following "SubShader{}" will be executed sequentially.
When writing multiple "SubShader{}" , generally the first one is the most complex and has the highest requirements for the graphics card. Write "SubShader{}" that supports lower-version graphics cards in turn . The further down, the simpler the algorithm and instructions
pass channel
"Pass{}" is written in the SubShader{} code block
"Pass{}" contains a series of rendering state setting commands
Name
Use the Name command to set a name for the Pass channel, which can be accessed through a C# script
Pass{
Name "ExampleNamedPass"
}
Tag
Labels are key-value pairs of data that can be assigned to channels. Unity uses predefined tags and values to determine how and when to render a given pass. You can also create your own custom channel labels with custom values and access them from C# code.
Shader "Examples/SinglePass"
{
SubShader
{
Pass
{
Name "ExamplePassName"
Tags { "ExampleTagKey" = "ExampleTagValue" }
// ShaderLab commands go here.
// HLSL code goes here.
}
}
}
For example
- Set shader properties
- set lighting
- Set texture properties
- set transparency
wait
Material material
Diffuse[_Color] _color is a property defined in Properties
Material{
Diffuse[_Color] //材质球颜色
Ambient[_Ambient] //材质球环境光
Specular[_Specular] //高光 搭配 Separatespecular 使用
Shininess[_Shininess] //高光强度
Emission[_Emission] //自发光
}
Separatespecular On //独立的镜面的高光效果 打开
Lighting
Lighting On //The lighting effect is turned on (off is turned off)
Settexture map
Settexture[_MainTex] //设置贴图 Settexture只能设置一张贴图
{
combine texture * primary double //combine-混合 texture-贴图 primary-之前计算好的顶点光照 (double-数值翻倍,quad-数值×4)
}
Settexture[_SecondTex]
{
constantColor[_Constant] //生成 constant 的颜色信息
combine texture * previous double , texture * constant //previous-之前所有顶点光照贴图计算的数据 ,texture-在","添加texture,意思是使用贴图的透明通道信息 constant贴图再混合一个color属性调整透明度
}
full code
Shader "Custom/TestShader"
{
Properties{
_Color("Main Color",Color) = (1,1,1,1)
_Ambient("Ambient",Color) = (0.3,0.3,0.3,0.3)
_Specular("Specular",Color)= (1,1,1,1)
_Shininess("Shininess",range(0,8)) = 4
_Emission("Emission",Color) = (1,1,1,1)
_MainTex("MainTex",2D) = "white"{}
_SecondTex("SecondTex",2D) = ""{}
_Constant("ConstantColor",Color) = (1,1,1,0.3)
}
SubShader
{
Tags { "Queue"="Transparent" } //设置渲染顺序
Pass{
Blend SrcAlpha OneMinusSrcAlpha //混合透明通道
//Color(1,0,0,1)
//Color[_Color]
Material{
Diffuse[_Color] //材质球颜色
Ambient[_Ambient] //材质球环境光
Specular[_Specular] //高光 搭配 Separatespecular 使用
Shininess[_Shininess] //高光强度
Emission[_Emission] //自发光
}
Lighting On //光照效果 打开 (off关闭)
Separatespecular On //独立的镜面的高光效果 打开
Settexture[_MainTex] //设置贴图 Settexture只能设置一张贴图
{
combine texture * primary double //combine-混合 texture-贴图 primary-之前计算好的顶点光照 (double-数值翻倍,quad-数值×4)
}
Settexture[_SecondTex]
{
constantColor[_Constant] //生成 constant 的颜色信息
combine texture * previous double , texture * constant //previous-之前所有顶点光照贴图计算的数据 ,texture-在","添加texture,意思是使用贴图的透明通道信息 constant贴图再混合一个color属性调整透明度
}
}
}
}