给材质加复选框[Toggle()]

作者:追风剑情 发布于:2018-9-24 11:07 分类:Shader

示例Shader


// Upgrade NOTE: upgraded instancing buffer 'MyProperties' to new syntax.

Shader "Unlit/Sphere1"
{
	Properties
	{
		_Color("Color", Color) = (1,1,1,1)
		//材质上加个复选框
		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma target 3.0
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#pragma multi_compile_fwdbase
			//加上这句后,材质球会多出一个Enable GPU Instancing勾选项
			#pragma multi_compile_instancing
			//生成多版本shader
			#pragma multi_compile __ UNITY_UI_ALPHACLIP
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				//只有当你需要在Fragment Shader中访问每个Instance独有的属性时才需要写这个宏。
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};
			
			//float4 _Color; //传统的声明方式不再需要了
			
			//新的声明方式,支持GPU Instancing
			UNITY_INSTANCING_BUFFER_START(MyProperties)
            UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
			#define _Color_arr MyProperties //这句代码是Unity自动插入的
            UNITY_INSTANCING_BUFFER_END(MyProperties)
			
			v2f vert (appdata v)
			{
				v2f o;
				//这个宏让Instance ID在Shader函数里能够被访问到
				UNITY_SETUP_INSTANCE_ID(v);
				//把Instance ID从输入结构拷贝至输出结构中。
				//只有当你需要在Fragment Shader中访问每个Instance独有的属性时才需要写这个宏。
				UNITY_TRANSFER_INSTANCE_ID(v,o);
				o.vertex = UnityObjectToClipPos(v.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				//这个宏让Instance ID在Shader函数里能够被访问到
				//要想访问独有属性必须调用这个宏
				UNITY_SETUP_INSTANCE_ID(i);
				//访问每个Instance独有的属性
				fixed4 col = UNITY_ACCESS_INSTANCED_PROP(_Color_arr, _Color);
				
				//判断Use Alpha Clip是否勾选上
				#ifdef UNITY_UI_ALPHACLIP
				clip (col.a - 0.001);
				#endif
				
				return col;
			}
			ENDCG
		}
	}
}


55555.png

//翻转图像
Shader "Custom/FlipImageShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Toggle(UNITY_UI_FLIP_X)] _FlipX("Flip X", float) = 0
        [Toggle(UNITY_UI_FLIP_Y)] _FlipY("Flip Y", float) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma multi_compile __ UNITY_UI_FLIP_X UNITY_UI_FLIP_Y
            #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)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                #if defined(UNITY_UI_FLIP_X) && defined(UNITY_UI_FLIP_Y)
                i.uv = float2(1 - i.uv.x, 1 - i.uv.y);
                #elif UNITY_UI_FLIP_X
                i.uv = float2(1 - i.uv.x, i.uv.y);
                #elif UNITY_UI_FLIP_Y
                i.uv = float2(i.uv.x, 1 - i.uv.y);
                #endif

                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号