From e3184a3f50ac461bd7c0a86ad1de7c9e31a02361 Mon Sep 17 00:00:00 2001 From: hible <514366607@qq.com> Date: Sun, 9 Oct 2022 14:09:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81SRP=20Batcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化合批 --- .../Shaders/FairyGUI-Image-URP.shader | 193 ++++++++++++++++++ .../Shaders/FairyGUI-Image-URP.shader.meta | 10 + .../Shaders/FairyGUI-Text-URP.shader | 93 +++++++++ .../Shaders/FairyGUI-Text-URP.shader.meta | 3 + Assets/Scripts/Core/ShaderConfig.cs | 8 + 5 files changed, 307 insertions(+) create mode 100644 Assets/Resources/Shaders/FairyGUI-Image-URP.shader create mode 100644 Assets/Resources/Shaders/FairyGUI-Image-URP.shader.meta create mode 100644 Assets/Resources/Shaders/FairyGUI-Text-URP.shader create mode 100644 Assets/Resources/Shaders/FairyGUI-Text-URP.shader.meta diff --git a/Assets/Resources/Shaders/FairyGUI-Image-URP.shader b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader new file mode 100644 index 00000000..9760779a --- /dev/null +++ b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader @@ -0,0 +1,193 @@ +// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' +// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' + +Shader "FairyGUI/Image-URP" +{ + Properties + { + _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {} + + _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 + + _BlendSrcFactor ("Blend SrcFactor", Float) = 5 + _BlendDstFactor ("Blend DstFactor", Float) = 10 + } + + SubShader + { + LOD 100 + + Tags + { + "Queue" = "Transparent+2" + "IgnoreProjector" = "True" + "RenderType" = "Transparent" + } + + Stencil + { + Ref [_Stencil] + Comp [_StencilComp] + Pass [_StencilOp] + ReadMask [_StencilReadMask] + WriteMask [_StencilWriteMask] + } + + Cull Off + Lighting Off + ZWrite Off + Fog + { + Mode Off + } + Blend [_BlendSrcFactor] [_BlendDstFactor], One One + ColorMask [_ColorMask] + + Pass + { + HLSLPROGRAM + // #pragma multi_compile_instancing + #pragma multi_compile NOT_COMBINED COMBINED + #pragma multi_compile NOT_GRAYED GRAYED COLOR_FILTER + #pragma multi_compile NOT_CLIPPED CLIPPED SOFT_CLIPPED ALPHA_MASK + #pragma vertex vert + #pragma fragment frag + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + + struct appdata_t + { + float4 vertex : POSITION; + half4 color : COLOR; + float4 texcoord : TEXCOORD0; + // UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + struct v2f + { + float4 vertex : SV_POSITION; + half4 color : COLOR; + float4 texcoord : TEXCOORD0; + + #ifdef CLIPPED + float2 clipPos : TEXCOORD1; + #endif + + #ifdef SOFT_CLIPPED + float2 clipPos : TEXCOORD1; + #endif + // UNITY_VERTEX_INPUT_INSTANCE_ID + // UNITY_VERTEX_OUTPUT_STEREO + }; + + + #ifdef COMBINED + sampler2D _AlphaTex; + #endif + + float4 _ClipBox = float4(-2, -2, 0, 0); + #ifdef SOFT_CLIPPED + float4 _ClipSoftness = float4(0, 0, 0, 0); + #endif + + CBUFFER_START(UnityPerMaterial) + sampler2D _MainTex; + CBUFFER_END + + // UNITY_INSTANCING_BUFFER_START(UnityPerMaterial) + // UNITY_DEFINE_INSTANCED_PROP(float4, _BaseMap_ST) + // UNITY_INSTANCING_BUFFER_END(UnityPerMaterial) + + + #ifdef COLOR_FILTER + float4x4 _ColorMatrix; + float4 _ColorOffset; + float _ColorOption = 0; + #endif + + v2f vert(appdata_t v) + { + // UNITY_SETUP_INSTANCE_ID(v); + v2f o; + o.vertex = TransformWorldToHClip(TransformObjectToWorld(v.vertex.xyz)); + o.texcoord = v.texcoord; + #if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550) + o.color.rgb = GammaToLinearSpace(v.color.rgb); + o.color.a = v.color.a; + #else + o.color = v.color; + #endif + + #ifdef CLIPPED + o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy; + #endif + + #ifdef SOFT_CLIPPED + o.clipPos = mul(unity_ObjectToWorld, v.vertex).xy * _ClipBox.zw + _ClipBox.xy; + #endif + + // UNITY_TRANSFER_INSTANCE_ID(v, o); + return o; + } + + half4 frag(v2f i) : SV_Target + { + // UNITY_SETUP_INSTANCE_ID(i); + half4 col = tex2D(_MainTex, i.texcoord.xy / i.texcoord.w) * i.color; + + #ifdef COMBINED + col.a *= tex2D(_AlphaTex, i.texcoord.xy / i.texcoord.w).g; + #endif + + #ifdef GRAYED + col.rgb = dot(col.rgb, half3(0.299, 0.587, 0.114)); + #endif + + #ifdef SOFT_CLIPPED + float2 factor = float2(0,0); + if(i.clipPos.x<0) + factor.x = (1.0-abs(i.clipPos.x)) * _ClipSoftness.x; + else + factor.x = (1.0-i.clipPos.x) * _ClipSoftness.z; + if(i.clipPos.y<0) + factor.y = (1.0-abs(i.clipPos.y)) * _ClipSoftness.w; + else + factor.y = (1.0-i.clipPos.y) * _ClipSoftness.y; + col.a *= clamp(min(factor.x, factor.y), 0.0, 1.0); + #endif + + #ifdef CLIPPED + float2 factor = abs(i.clipPos); + col.a *= step(max(factor.x, factor.y), 1); + #endif + + #ifdef COLOR_FILTER + if (_ColorOption == 0) + { + half4 col2 = col; + col2.r = dot(col, _ColorMatrix[0]) + _ColorOffset.x; + col2.g = dot(col, _ColorMatrix[1]) + _ColorOffset.y; + col2.b = dot(col, _ColorMatrix[2]) + _ColorOffset.z; + col2.a = dot(col, _ColorMatrix[3]) + _ColorOffset.w; + col = col2; + } + else //premultiply alpha + col.rgb *= col.a; + #endif + + #ifdef ALPHA_MASK + clip(col.a - 0.001); + #endif + + return col; + } + ENDHLSL + } + } +} \ No newline at end of file diff --git a/Assets/Resources/Shaders/FairyGUI-Image-URP.shader.meta b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader.meta new file mode 100644 index 00000000..1611964a --- /dev/null +++ b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 2b6fa34b92064d8aa3d62474d3f39297 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Shaders/FairyGUI-Text-URP.shader b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader new file mode 100644 index 00000000..a79592a6 --- /dev/null +++ b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader @@ -0,0 +1,93 @@ +// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' +// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' + +Shader "FairyGUI/Text-URP" +{ + Properties + { + _MainTex ("Alpha (A)", 2D) = "white" {} + + _ColorMask ("Color Mask", Float) = 15 + + _BlendSrcFactor ("Blend SrcFactor", Float) = 5 + _BlendDstFactor ("Blend DstFactor", Float) = 10 + } + + SubShader + { + LOD 100 + + Tags + { + "Queue" = "Transparent+3" + "IgnoreProjector" = "True" + "RenderType" = "Transparent" + } + + + Cull Off + Lighting Off + ZWrite Off + Fog + { + Mode Off + } + Blend [_BlendSrcFactor] [_BlendDstFactor] + ColorMask [_ColorMask] + + Pass + { + HLSLPROGRAM + #pragma multi_compile NOT_GRAYED GRAYED + #pragma vertex vert + #pragma fragment frag + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + + struct appdata_t + { + float4 vertex : POSITION; + half4 color : COLOR; + float4 texcoord : TEXCOORD0; + }; + + struct v2f + { + float4 vertex : SV_POSITION; + half4 color : COLOR; + float4 texcoord : TEXCOORD0; + }; + + + sampler2D _MainTex; + + v2f vert(appdata_t v) + { + v2f o; + o.vertex = TransformWorldToHClip(TransformObjectToWorld(v.vertex.xyz)); + o.texcoord = v.texcoord; + #if !defined(UNITY_COLORSPACE_GAMMA) && (UNITY_VERSION >= 550) + o.color.rgb = GammaToLinearSpace(v.color.rgb); + o.color.a = v.color.a; + #else + o.color = v.color; + #endif + + return o; + } + + half4 frag(v2f i) : SV_Target + { + half4 col = i.color; + col.a *= half(tex2D(_MainTex, i.texcoord.xy).a); + + #ifdef GRAYED + col.rgb = dot(col.rgb, half3(0.299, 0.587, 0.114)); + #endif + + return col; + } + ENDHLSL + } + } +} \ No newline at end of file diff --git a/Assets/Resources/Shaders/FairyGUI-Text-URP.shader.meta b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader.meta new file mode 100644 index 00000000..32794c40 --- /dev/null +++ b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 64ad1e3d6f9d423eab32d6cfb15b3f5c +timeCreated: 1659701749 \ No newline at end of file diff --git a/Assets/Scripts/Core/ShaderConfig.cs b/Assets/Scripts/Core/ShaderConfig.cs index da77dd56..ec82bcc4 100644 --- a/Assets/Scripts/Core/ShaderConfig.cs +++ b/Assets/Scripts/Core/ShaderConfig.cs @@ -22,12 +22,20 @@ public static class ShaderConfig /// /// /// +#if UNITY_2019_1_OR_NEWER + public static string imageShader = "FairyGUI/Image-URP"; +#else public static string imageShader = "FairyGUI/Image"; +#endif /// /// /// +#if UNITY_2019_1_OR_NEWER + public static string textShader = "FairyGUI/Text-URP"; +#else public static string textShader = "FairyGUI/Text"; +#endif /// /// From 69c02facd7eaf576607af6db625f5a396dbb1c87 Mon Sep 17 00:00:00 2001 From: hible <514366607@qq.com> Date: Fri, 8 Sep 2023 17:11:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=BC=8F=E4=BC=A0=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Resources/Shaders/FairyGUI-Image-URP.shader | 7 ++++++- Assets/Resources/Shaders/FairyGUI-Text-URP.shader | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Assets/Resources/Shaders/FairyGUI-Image-URP.shader b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader index 9760779a..e5c27b46 100644 --- a/Assets/Resources/Shaders/FairyGUI-Image-URP.shader +++ b/Assets/Resources/Shaders/FairyGUI-Image-URP.shader @@ -69,6 +69,11 @@ Shader "FairyGUI/Image-URP" // UNITY_VERTEX_INPUT_INSTANCE_ID }; + half3 GammaToLinearSpace(half3 sRGB) + { + return sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h); + } + struct v2f { float4 vertex : SV_POSITION; @@ -99,7 +104,7 @@ Shader "FairyGUI/Image-URP" CBUFFER_START(UnityPerMaterial) sampler2D _MainTex; CBUFFER_END - + // UNITY_INSTANCING_BUFFER_START(UnityPerMaterial) // UNITY_DEFINE_INSTANCED_PROP(float4, _BaseMap_ST) // UNITY_INSTANCING_BUFFER_END(UnityPerMaterial) diff --git a/Assets/Resources/Shaders/FairyGUI-Text-URP.shader b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader index a79592a6..c74c814e 100644 --- a/Assets/Resources/Shaders/FairyGUI-Text-URP.shader +++ b/Assets/Resources/Shaders/FairyGUI-Text-URP.shader @@ -58,7 +58,12 @@ Shader "FairyGUI/Text-URP" float4 texcoord : TEXCOORD0; }; - + half3 GammaToLinearSpace(half3 sRGB) + { + return sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h); + } + + sampler2D _MainTex; v2f vert(appdata_t v) @@ -70,7 +75,7 @@ Shader "FairyGUI/Text-URP" o.color.rgb = GammaToLinearSpace(v.color.rgb); o.color.a = v.color.a; #else - o.color = v.color; + o.color = v.color; #endif return o;