可能是由于UGUI有自己单独的渲染管线,所以UGUI的shader的代码不用顾及Unity单枪所使用的渲染管线。不知道这么理解对不对,如果不对,请大神批评指正。下面是Unity大部分的UGUI的Shader代码。
UI/Default
Shader "UI/Default"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
float4 mask : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
float _UIMaskSoftnessX;
float _UIMaskSoftnessY;
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
float4 vPosition = UnityObjectToClipPos(v.vertex);
OUT.worldPosition = v.vertex;
OUT.vertex = vPosition;
float2 pixelSize = vPosition.w;
pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
OUT.mask = float4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
OUT.color = v.color * _Color;
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
const half alphaPrecision = half(0xff);
const half invAlphaPrecision = half(1.0/alphaPrecision);
IN.color.a = round(IN.color.a * alphaPrecision)*invAlphaPrecision;
half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
#ifdef UNITY_UI_CLIP_RECT
half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
color.a *= m.x * m.y;
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
color.rgb *= color.a;
return color;
}
ENDCG
}
}
}
UI/Lit/Bumped
Shader "UI/Lit/Bumped"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Specular ("Specular Color", Color) = (0,0,0,0)
_MainTex ("Diffuse (RGB), Alpha (A)", 2D) = "white" {}
[NoScaleOffset] _MainBump ("Diffuse Bump Map", 2D) = "bump" {}
[PowerSlider(5.0)] _Shininess ("Shininess", Range(0.01, 1.0)) = 0.2
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
LOD 400
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
CGPROGRAM
#pragma surface surf PPL alpha noshadow novertexlights nolightmap nofog vertex:vert
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
float3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
float4 worldPosition;
};
sampler2D _MainTex;
sampler2D _MainBump;
fixed4 _Color;
fixed4 _Specular;
half _Shininess;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
void vert (inout appdata_t v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
v.vertex = o.worldPosition;
v.color = v.color * _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 col = (tex2D(_MainTex, IN.uv_MainTex) + _TextureSampleAdd) * IN.color;
half3 normal = UnpackNormal(tex2D(_MainBump, IN.uv_MainTex));
o.Albedo = col.rgb;
o.Normal = normalize(normal);
o.Specular = _Specular.a;
o.Gloss = _Shininess;
o.Alpha = col.a;
#ifdef UNITY_UI_CLIP_RECT
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (o.Alpha - 0.001);
#endif
}
half4 LightingPPL (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half shininess = s.Gloss * 250.0 + 4.0;
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, s.Normal)));
half diffuseFactor = max(0.0, dot(s.Normal, lightDir));
half specularFactor = pow(reflectiveFactor, shininess) * s.Specular;
half4 c;
c.rgb = (s.Albedo * diffuseFactor + _Specular.rgb * specularFactor) * _LightColor0.rgb;
c.rgb *= atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
Fallback "UI/Lit/Transparent"
}
UI/Lit/Detail
Shader "UI/Lit/Detail"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Specular ("Specular Color", Color) = (0,0,0,0)
_MainTex ("Diffuse (RGB), Alpha (A)", 2D) = "white" {}
_MainBump ("Diffuse Bump Map", 2D) = "bump" {}
_DetailTex ("Detail (RGB)", 2D) = "white" {}
_DetailBump ("Detail Bump Map", 2D) = "bump" {}
_Strength ("Detail Strength", Range(0.0, 1.0)) = 0.2
[PowerSlider(5.0)] _Shininess ("Shininess", Range(0.01, 1.0)) = 0.2
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
CGPROGRAM
#pragma surface surf PPL alpha noshadow novertexlights nolightmap vertex:vert nofog
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
float3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float2 uv_MainTex;
float2 uv2_DetailTex;
fixed4 color : COLOR;
float4 worldPosition;
};
sampler2D _MainTex;
sampler2D _MainBump;
sampler2D _DetailTex;
sampler2D _DetailBump;
float4 _DetailTex_TexelSize;
fixed4 _Color;
fixed4 _Specular;
half _Strength;
half _Shininess;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
void vert (inout appdata_t v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
v.vertex = o.worldPosition;
v.texcoord1.xy *= _DetailTex_TexelSize.xy;
v.color = v.color * _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex, IN.uv_MainTex) + _TextureSampleAdd;
fixed4 detail = tex2D(_DetailTex, IN.uv2_DetailTex);
fixed4 nmMain = tex2D(_MainBump, IN.uv_MainTex);
fixed4 nmDetail = tex2D(_DetailBump, IN.uv2_DetailTex);
fixed3 normal = UnpackNormal((nmMain + nmDetail) * 0.5);
col.rgb = lerp(col.rgb, col.rgb * detail.rgb, detail.a * _Strength);
col *= IN.color;
o.Albedo = col.rgb;
o.Normal = normal;
o.Specular = _Specular.a;
o.Gloss = _Shininess;
o.Alpha = col.a;
#ifdef UNITY_UI_CLIP_RECT
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (o.Alpha - 0.001);
#endif
}
half4 LightingPPL (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half shininess = s.Gloss * 250.0 + 4.0;
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, s.Normal)));
half diffuseFactor = max(0.0, dot(s.Normal, lightDir));
half specularFactor = pow(reflectiveFactor, shininess) * s.Specular;
half4 c;
c.rgb = (s.Albedo * diffuseFactor + _Specular.rgb * specularFactor) * _LightColor0.rgb;
c.rgb *= atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
Fallback "UI/Lit/Transparent"
}
UI/Lit/Refraction
Shader "UI/Lit/Refraction"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Specular ("Specular Color", Color) = (0,0,0,0)
_MainTex ("Diffuse (RGB), Alpha (A)", 2D) = "white" {}
_MainBump ("Diffuse Bump Map", 2D) = "bump" {}
_Mask ("Mask (Specularity, Shininess, Refraction)", 2D) = "black" {}
[PowerSlider(5.0)] _Shininess ("Shininess", Range(0.01, 1.0)) = 0.2
_Focus ("Focus", Range(-100.0, 100.0)) = -100.0
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
LOD 400
GrabPass
{
Name "BASE"
Tags { "LightMode" = "Always" }
}
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
CGPROGRAM
#pragma target 3.0
#pragma surface surf PPL alpha noshadow novertexlights nolightmap vertex:vert nofog
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord1 : TEXCOORD0;
fixed4 color : COLOR;
float3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float4 texcoord1;
float4 proj;
fixed4 color : COLOR;
float4 worldPosition;
};
sampler2D _GrabTexture;
sampler2D _MainTex;
sampler2D _MainBump;
sampler2D _Mask;
float4 _MainTex_ST;
float4 _MainBump_ST;
float4 _Mask_ST;
half4 _GrabTexture_TexelSize;
fixed4 _Color;
fixed4 _Specular;
half _Shininess;
half _Focus;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
void vert (inout appdata_t v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
v.vertex = o.worldPosition;
v.color = v.color * _Color;
o.texcoord1.xy = TRANSFORM_TEX(v.texcoord1, _MainTex);
o.texcoord1.zw = TRANSFORM_TEX(v.texcoord1, _MainBump);
#if UNITY_UV_STARTS_AT_TOP
o.proj.xy = (float2(v.vertex.x, -v.vertex.y) + v.vertex.w) * 0.5;
#else
o.proj.xy = (float2(v.vertex.x, v.vertex.y) + v.vertex.w) * 0.5;
#endif
o.proj.zw = v.vertex.zw;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex, IN.texcoord1.xy) + _TextureSampleAdd;
half3 normal = UnpackNormal(tex2D(_MainBump, IN.texcoord1.zw));
half3 mask = tex2D(_Mask, IN.texcoord1.xy);
float2 offset = normal.xy * _GrabTexture_TexelSize.xy * _Focus;
IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
half4 ref = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
col.rgb = lerp(col.rgb, ref.rgb, mask.b);
col *= IN.color;
o.Albedo = col.rgb;
o.Normal = normalize(normal);
o.Specular = _Specular.a * mask.r;
o.Gloss = _Shininess * mask.g;
o.Alpha = col.a;
#ifdef UNITY_UI_CLIP_RECT
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (o.Alpha - 0.001);
#endif
}
half4 LightingPPL (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half3 nNormal = normalize(s.Normal);
half shininess = s.Gloss * 250.0 + 4.0;
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, nNormal)));
half diffuseFactor = max(0.0, dot(nNormal, lightDir));
half specularFactor = pow(reflectiveFactor, shininess) * s.Specular;
half4 c;
c.rgb = (s.Albedo * diffuseFactor + _Specular.rgb * specularFactor) * _LightColor0.rgb;
c.rgb *= atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
Fallback "UI/Lit/Transparent"
}
UI/Lit/Refraction Detail
Shader "UI/Lit/Refraction Detail"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Specular ("Specular Color", Color) = (0,0,0,0)
_MainTex ("Diffuse (RGB), Alpha (A)", 2D) = "white" {}
_MainBump ("Diffuse Bump Map", 2D) = "bump" {}
_Mask ("Mask (Specularity, Shininess, Refraction)", 2D) = "white" {}
_DetailTex ("Detail (RGB)", 2D) = "white" {}
_DetailBump ("Detail Bump Map", 2D) = "bump" {}
_DetailMask ("Detail Mask (Spec, Shin, Ref)", 2D) = "white" {}
[PowerSlider(5.0)] _Shininess ("Shininess", Range(0.01, 1.0)) = 0.2
_Focus ("Focus", Range(-100.0, 100.0)) = -100.0
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
// SM 3.0
SubShader
{
LOD 400
GrabPass
{
Name "BASE"
Tags { "LightMode" = "Always" }
}
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
CGPROGRAM
#pragma target 3.0
#pragma surface surf PPL alpha noshadow novertexlights nolightmap vertex:vert nofog
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord1 : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
float3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float4 texcoord1;
float4 texcoord2;
float2 texcoord3;
float4 proj;
fixed4 color : COLOR;
float4 worldPosition;
};
sampler2D _GrabTexture;
sampler2D _MainTex;
sampler2D _MainBump;
sampler2D _Mask;
sampler2D _DetailTex;
sampler2D _DetailBump;
sampler2D _DetailMask;
float4 _MainTex_ST;
float4 _MainBump_ST;
float4 _Mask_ST;
float4 _DetailTex_ST;
float4 _DetailBump_ST;
float4 _DetailMask_ST;
float4 _DetailTex_TexelSize;
float4 _DetailBump_TexelSize;
float4 _DetailMask_TexelSize;
half4 _GrabTexture_TexelSize;
fixed4 _Color;
fixed4 _Specular;
half _Shininess;
half _Focus;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
void vert (inout appdata_t v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
v.vertex = o.worldPosition;
v.color = v.color * _Color;
o.texcoord1.xy = TRANSFORM_TEX(v.texcoord1, _MainTex);
o.texcoord1.zw = TRANSFORM_TEX(v.texcoord1, _MainBump);
o.texcoord2.xy = TRANSFORM_TEX(v.texcoord2 * _DetailTex_TexelSize.xy, _DetailTex);
o.texcoord2.zw = TRANSFORM_TEX(v.texcoord2 * _DetailBump_TexelSize.xy, _DetailBump);
o.texcoord3 = TRANSFORM_TEX(v.texcoord2 * _DetailMask_TexelSize.xy, _DetailMask);
#if UNITY_UV_STARTS_AT_TOP
o.proj.xy = (float2(v.vertex.x, -v.vertex.y) + v.vertex.w) * 0.5;
#else
o.proj.xy = (float2(v.vertex.x, v.vertex.y) + v.vertex.w) * 0.5;
#endif
o.proj.zw = v.vertex.zw;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex, IN.texcoord1.xy) + _TextureSampleAdd;
fixed4 detail = tex2D(_DetailTex, IN.texcoord2.xy);
half3 normal = UnpackNormal(tex2D(_MainBump, IN.texcoord1.zw)) +
UnpackNormal(tex2D(_DetailBump, IN.texcoord2.zw));
half3 mask = tex2D(_Mask, IN.texcoord1.xy) *
tex2D(_DetailMask, IN.texcoord3);
float2 offset = normal.xy * _GrabTexture_TexelSize.xy * _Focus;
IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
half4 ref = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
col.rgb = lerp(col.rgb, ref.rgb, mask.b);
col.rgb = lerp(col.rgb, col.rgb * detail.rgb, detail.a);
col *= IN.color;
o.Albedo = col.rgb;
o.Normal = normalize(normal);
o.Specular = _Specular.a * mask.r;
o.Gloss = _Shininess * mask.g;
o.Alpha = col.a;
#ifdef UNITY_UI_CLIP_RECT
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (o.Alpha - 0.001);
#endif
}
half4 LightingPPL (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half3 nNormal = normalize(s.Normal);
half shininess = s.Gloss * 250.0 + 4.0;
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, nNormal)));
half diffuseFactor = max(0.0, dot(nNormal, lightDir));
half specularFactor = pow(reflectiveFactor, shininess) * s.Specular;
half4 c;
c.rgb = (s.Albedo * diffuseFactor + _Specular.rgb * specularFactor) * _LightColor0.rgb;
c.rgb *= atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
Fallback "UI/Lit/Detail"
}
UI/Lit/Transparent
Shader "UI/Lit/Transparent"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_Specular ("Specular Color", Color) = (0,0,0,0)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
LOD 400
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
CGPROGRAM
#pragma surface surf PPL alpha noshadow novertexlights nolightmap nofog vertex:vert
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
float3 normal : NORMAL;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
float4 worldPosition;
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _Specular;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
void vert (inout appdata_t v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.worldPosition = v.vertex;
v.vertex = o.worldPosition;
v.color = v.color * _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 col = (tex2D(_MainTex, IN.uv_MainTex) + _TextureSampleAdd) * IN.color;
o.Albedo = col.rgb;
o.Alpha = col.a;
#ifdef UNITY_UI_CLIP_RECT
o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (o.Alpha - 0.001);
#endif
}
half4 LightingPPL (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half3 nNormal = normalize(s.Normal);
half shininess = s.Gloss * 250.0 + 4.0;
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half reflectiveFactor = max(0.0, dot(-viewDir, reflect(lightDir, nNormal)));
half diffuseFactor = max(0.0, dot(nNormal, lightDir));
half specularFactor = pow(reflectiveFactor, shininess) * s.Specular;
half4 c;
c.rgb = (s.Albedo * diffuseFactor + _Specular.rgb * specularFactor) * _LightColor0.rgb;
c.rgb *= atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
}
Overdraw
Shader "Hidden/UI/Overdraw"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest Always
Blend One One
ColorMask [_ColorMask]
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float4 worldPosition : TEXCOORD1;
};
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.worldPosition = IN.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.color = float4(0.1, 0.04, 0.02, 1);
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
half4 color = IN.color;
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
return color;
}
ENDCG
}
}
}
Hidden/UI/CompositeOverdraw
Shader "Hidden/UI/CompositeOverdraw"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
// No culling or depth
Cull Off ZWrite Off ZTest Always Blend SrcAlpha OneMinusSrcAlpha
Fog { Mode off }
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float v = col.r;
// during overdraw pass, each draw call adds 0.1 on the red channel
if(v < 0.05) // nothing
col = fixed4(0,0,0,0);
else if(v < 0.15) // drawn once
col = fixed4(0.9,0.9,0.91, 1);
else if(v < 0.25) // twice
col = fixed4(0.81,0.81,1,1);
else if(v < 0.35) // thrice
col = fixed4(0.82,1,0.82, 1);
else if(v < 0.45)
col = fixed4(1, 0.75, 0.75, 1);
else // too many times
col = fixed4(1-(0.5-v/2), 0.5, 0.5, 1);
return col;
}
ENDCG
}
}
}
UI/Unlit/Detail
Shader "UI/Unlit/Detail"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
_DetailTex ("Detail (RGB)", 2D) = "white" {}
_Strength ("Detail Strength", Range(0.0, 1.0)) = 0.2
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
float4 worldPosition : TEXCOORD2;
fixed4 color : COLOR;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
sampler2D _DetailTex;
float4 _MainTex_ST;
float4 _DetailTex_ST;
float4 _DetailTex_TexelSize;
fixed4 _Color;
fixed _Strength;
fixed4 _TextureSampleAdd;
bool _UseClipRect;
float4 _ClipRect;
bool _UseAlphaClip;
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.worldPosition = v.vertex;
o.vertex = UnityObjectToClipPos(o.worldPosition);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.texcoord2 = TRANSFORM_TEX(v.texcoord2 * _DetailTex_TexelSize.xy, _DetailTex);
o.color = v.color * _Color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 color = (tex2D(_MainTex, i.texcoord) + _TextureSampleAdd) * i.color;
fixed4 detail = tex2D(_DetailTex, i.texcoord2);
color.rgb = lerp(color.rgb, color.rgb * detail.rgb, detail.a * _Strength);
color = color * _Color;
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
return color;
}
ENDCG
}
}
}
UI/Unlit/Text Detail文章来源:https://www.toymoban.com/news/detail-596489.html
Shader "UI/Unlit/Text Detail"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
_DetailTex ("Detail (RGB)", 2D) = "white" {}
_Strength ("Detail Strength", Range(0.0, 1.0)) = 0.2
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
FallBack "UI/Unlit/Detail"
}
十、UI/Unlit/Transparent文章来源地址https://www.toymoban.com/news/detail-596489.html
Shader "UI/Unlit/Transparent"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
FallBack "UI/Default"
}
到了这里,关于Unity的UGUI的Shader代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!