Shader_Properties parameter, switch related

ShaderProperties blockGenerally withinspection paneldeal with

There are three main types:

- Attributes:

_Name("Display Name", type) = defaultValue[{
    
    options}]
_Name("Display Name", type) = defaultValue[{
    
    options}]
_Name("Display Name", type) = defaultValue[{
    
    options}]
//解析:
_Name:供shader脚本使用的变量名
"Display Name":检视面板看到的名字
type:属性类型
常用类型和默认值:
	Color -01定义的rgba颜色,比如(1,1,1,1)2D/Cube - 对于贴图来说,默认值可以为一个代表默认tint颜色的字符串,可以是空字符串或者"white","black","bump"中的一个
	Float - 某个指定的浮点数,如1.0
	Range(01-表示在区间01中间的浮点数
	Vector - 一个4维数,写为 (x,y,z,w)
例:
_MainColor ("Main Color", Color) = (0,0,1,0.5) 
_Texture ("Texture", 2D) = "white" {
    
    } 
_Cub("CubMap",Cube)=""{
    
    }
_Rgnge("Range",Range(0,1))=0.5
_Float("Float",Float)=1
_Vector("Vector",Vector)=(0,0,0,0)

One line for each property, the equal sign needs to be followed by the default value, and the end of the linemust not have a closing symbol

There is also an {option}, which is only related to 2D, Rect or Cube textures. When writing input, we must at least write a pair of blank {} after the texture. When we need to open a specific option, we can Write it inside these curly braces. If you need to open multiple options at the same time, you can use blanks to separate them. Possible choices are ObjectLinear, EyeLinear, SphereMap, CubeReflect, CubeNormal, these are TexGen modes in OpenGL, generally not commonly used

- Control parameters:

[HideInInspector]: Hide this attribute in the inspection panel. Sometimes the properties in our shader need to be dynamically modified by the script without adjustment by the art students. Adding this control parameter can not be displayed in the material panel. When the art is adjusted, it can be ignored directly to facilitate the art students to adjust the material properties, especially in the material panel. When there are many attribute parameters.
[NoScaleOffset]: The inspection panel does not display UV offset and scaling options.
[Normal]: Indicates that the texture is a normal map.
[HDR]: Indicates that the texture is an HDR texture.
[Gamma]: Indicates that the float/vector variable is sRGB in the inspection panel.

- Switch control:

ToggleDrawer
EnumDrawer
KeywordEnumDrawer
switch

example:

Shader "Custom/Material Property Drawer Example"
{
    
    
	Properties
	{
    
    
	
		[Header(Material Property Drawer Example)]

		[Space(10)]

		_FirstColor("First Color", color) = (1,0,0,1)
		_SecondColor("Second Color", color) = (0,0,1,1)

		[Space(10)]

	
		[Toggle] _OneMinus("OneMinus color", Float) = 0
		[Toggle(ENABLE_HALF)] _Half("Half Color", Float) = 0
		[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend Mode", Float) = 1
		[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend Mode", Float) = 1
		[Enum(Off, 0, On, 1)] _ZWrite("ZWrite", Float) = 0
		[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
		[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 1
		[KeywordEnum(None, Add, Multiply)] _Overlay("Overlay ", Float) = 0


	}
		SubShader
		{
    
    
			Tags {
    
     "Queue" = "Transparent" "RenderType" = "Transparent" }
			Blend[_SrcBlend][_DstBlend]
			ZWrite[_ZWrite]
			ZTest[_ZTest]
			Cull[_Cull]

			Pass
			{
    
    
				CGPROGRAM

				#pragma shader_feature _ONEMINUS_ON

				#pragma shader_feature ENABLE_HALF

				#pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY

				#pragma vertex vert
				#pragma fragment frag

				#include "UnityCG.cginc"

				float4 _FirstColor;
		                float4 _SecondColor;
			
			

			struct appdata
			{
    
    
				float4 vertex : POSITION;
				
			};

			struct v2f
			{
    
    
				
				float4 vertex : SV_POSITION;
			};

			v2f vert(appdata v)
			{
    
    
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
    
    
				// sample the texture
				fixed4 col = _FirstColor;
			fixed4 col2 = _SecondColor;

			// Use #if, #ifdef or #if defined
			#if _ONEMINUS_ON
			col = 1 - col;
			#endif

			// Use #if, #ifdef or #if defined
			#if ENABLE_HALF
			col *= 0.5;
			#endif

			

			#if _OVERLAY_ADD
			col += col2;
			#elif _OVERLAY_MULTIPLY
			col *= col2;
			#endif

			

			return col;
		}
		ENDCG
	}
	}
}

insert image description here
One thing that needs to be explained, in the above code [Enum(UnityEngine.Rendering.BlendMode)] this life refers to the functions in UnityEngine, and lists all the situations in BlendMode as shown in the figure below. Of course, in some cases, we do not
insert image description here
want Enumerates all blend modes and leaves only a few for artists to use. This is also achievable, we only need to define the value and display of the enumeration ourselves. Just add the following code in front of the type that needs to be enumerated:

[Enum(One,1,SrcAlpha,5)] 

insert image description here

注意:枚举中的值不能随便定义,一定要跟融合的模型一一对应上,具体可以参看BlendMode中的定义:
Zero             = 0,
One              = 1,
DstColor         = 2,
SrcColor         = 3,
OneMinusDstColor = 4,
SrcAlpha         = 5,
OneMinusSrcColor = 6,
DstAlpha         = 7,
OneMinusDstAlpha = 8,
SrcAlphaSaturate = 9,
OneMinusSrcAlpha = 10

Original link

Guess you like

Origin blog.csdn.net/suixinger_lmh/article/details/125284601