What are the commonly used built-in Enum on the Shader panel
This article is to share with you about the built-in Enum commonly used on the Shader panel, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Today, I intend to expose the ZTest attribute of a shader to the material panel so that art can be easily adjusted, because a shader that is not very important does not bother to write a UI. I did not find it for a long time according to the ZTest keyword, and finally found that it was originally called CompareFunction.
So by the way, I sorted out some of the commonly used shader. (in addition, I found that the custom Enum can also be used now. I have the impression that the old version of unity is not supported. I don't know if I got it wrong or unity later supported it.)
The code is as follows
Shader "Mya/EnumTest" {Properties {_ MainTex ("Texture", 2D) = "white" {} [Header (Custom)] [Enum (CustomEnum)] _ CustomEnum ("CustomEnum", Float) = 1.0
[Header (Option)] [Enum (UnityEngine.Rendering.BlendOp)] _ BlendOp ("BlendOp", Float) = 1.0 [Enum (UnityEngine.Rendering.BlendMode)] _ SrcBlend ("SrcBlend", Float) = 1.0 [Enum (UnityEngine.Rendering.BlendMode)] _ DstBlend ("DstBlend", Float) = 0.0 [Enum (Off, 0, On, 1)] _ ZWriteMode ("ZWriteMode") Float) = 1 [Enum (UnityEngine.Rendering.CullMode)] _ CullMode ("CullMode", float) = 2 [Enum (UnityEngine.Rendering.CompareFunction)] _ ZTestMode ("ZTestMode", Float) = 2 [Enum (UnityEngine.Rendering.ColorWriteMask)] _ ColorMask ("ColorMask", Float) = 2
[Header (Stencil)] [Enum (UnityEngine.Rendering.CompareFunction)] _ StencilComp ("Stencil Comparison", Float) = 8 [IntRange] _ StencilWriteMask ("StencilWriteMask", Range (0255)) = 255 [IntRange] _ StencilReadMask ("StencilReadMask", Range (0255)) = 255 [IntRange] _ Stencil ("Stencil ID", Range (0255)) = 0 [Enum (UnityEngine.Rendering.StencilOp)] _ StencilPass ("StencilPass") Float) = 0 [Enum (UnityEngine.Rendering.StencilOp)] _ StencilFail ("StencilFail", Float) = 0 [Enum (UnityEngine.Rendering.StencilOp)] _ StencilZFail ("StencilZFail", Float) = 0
} SubShader {Tags {"RenderType" = "Opaque"} LOD 100
Pass {BlendOp [_ BlendOp] Blend [_ SrcBlend] [_ DstBlend] ZWrite [_ ZWriteMode] ZTest [_ ZTestMode] Cull [_ CullMode] ColorMask [_ ColorMask]
Stencil {Ref [_ Stencil] Comp [_ StencilComp] ReadMask [_ StencilReadMask] WriteMask [_ StencilWriteMask] Pass [_ StencilPass] Fail [_ StencilFail] ZFail [_ StencilZFail]} CGPROGRAM # Pragma vertex vert # pragma fragment frag # include "UnityCG.cginc"
Struct appdata {float4 vertex: POSITION; float2 uv: TEXCOORD0;}
Struct v2f {float2 uv: TEXCOORD0; float4 vertex: SV_POSITION;}
Sampler2D _ MainTex; float4 _ MainTex_ST
V2f vert (appdata v) {v2fo; o.vertex = UnityObjectToClipPos (v.vertex); o.uv = TRANSFORM_TEX (v.uv, _ MainTex); return o;}
Fixed4 frag (v2f I): SV_Target {/ / sample the texture fixed4 col = tex2D (_ MainTex, i.uv); return col;} ENDCG}
Customize the code for Enum
Public enum CustomEnum {Enum1 = 0, Enum2 = 1, Enum3 = 2}
The effect is as follows
ZWriteMode is not built-in, and in fact there are only two states: on and off, so it's OK to use Toogle. Here I declare a new custom Enum directly with [Enum (Off, 0, On, 1)].
[IntRange] I remember it didn't seem to exist before, and this can draw a slider with a plastic input.
If you want to know what other Attributes in unity can be used in shader, you can take a look at the file MaterialPropertyDrawer.cs, or write one yourself after inheriting MaterialPropertyDrawer.
The above are the commonly used built-in Enum on the Shader panel, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.