From 4cda9bb7a10ad2b85257f9998d3cc1d978dcdcfd Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Sun, 25 Feb 2024 16:29:33 +0900 Subject: [PATCH] feat: add CompositeCanvasRenderer support --- .../CompositeCanvasRenderer Support.meta | 8 + ...ompositeCanvasRenderer-SoftMaskable.shader | 214 ++++++++++++++++++ ...iteCanvasRenderer-SoftMaskable.shader.meta | 10 + Packages/src/package.json | 5 + ProjectSettings/GraphicsSettings.asset | 1 + 5 files changed, 238 insertions(+) create mode 100644 Packages/src/Samples~/CompositeCanvasRenderer Support.meta create mode 100644 Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader create mode 100644 Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader.meta diff --git a/Packages/src/Samples~/CompositeCanvasRenderer Support.meta b/Packages/src/Samples~/CompositeCanvasRenderer Support.meta new file mode 100644 index 0000000..e895c6d --- /dev/null +++ b/Packages/src/Samples~/CompositeCanvasRenderer Support.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b8b09c5f5b7a14f0d95783ec9ba026f1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader b/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader new file mode 100644 index 0000000..2a0d9fd --- /dev/null +++ b/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader @@ -0,0 +1,214 @@ +Shader "Hidden/UI/CompositeCanvasRenderer (SoftMaskable)" +{ + Properties + { + [Header(Main)] + [PerRendererData] _MainTex ("Main Texture", 2D) = "white" {} + _Color ("Tint", Color) = (1,1,1,1) + + [Header(Detail)] + [Toggle(ENABLE_DETAIL)] _DETAIL ("Enable Detail", Float) = 0 + _DetailTex ("Detail Texture", 2D) = "white" {} + + [Header(UV Animation)] + [Toggle(ENABLE_UV_ANIMATION)] _UV_ANIMATION ("Enable UV Animation", Float) = 0 + _UvTex ("UV Animation Texture (RG)", 2D) = "black" {} + _UvSpeed("UV Animation Speed", Vector) = (0,0,0,0) + _UvModifier ("UV Modifier", Range(-0.1, 0.1)) = 0 + + [Header(Mask)] + [Toggle(ENABLE_MASK)] _MASK ("Enable Mask", Float) = 0 + _MaskTex ("Mask Texture", 2D) = "white" {} + _MaskSpeed("Mask Speed", Vector) = (0,0,0,0) + + [Header(Color Mode)] + [KeywordEnum(Multipry, Additive, Subtract, Fill)] Color_Mode ("Color Mode", Int) = 0 + + [Header(Blend Mode)] + [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("Src Blend Mode", Int) = 1 + [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("Dst Blend Mode", Int) = 10 + + [Header(Stencil)] + _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 [_SrcBlend] [_DstBlend] + ColorMask [_ColorMask] + + Pass + { + Name "Default" + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #pragma target 2.0 + + #include "UnityCG.cginc" + #include "UnityUI.cginc" + + #include "Packages/com.coffee.softmask-for-ugui/Shaders/UISoftMask.cginc" // Add for soft mask + #pragma multi_compile_local UI_SOFT_MASKABLE UI_SOFT_MASKABLE_EDITOR // Add for soft mask + #pragma multi_compile_local _ UI_SOFT_MASKABLE_STEREO // Add for soft mask (stereo) + + #pragma multi_compile_local _ UNITY_UI_CLIP_RECT + #pragma multi_compile_local _ UNITY_UI_ALPHACLIP + #pragma multi_compile_local _ COLOR_MODE_ADDITIVE COLOR_MODE_FILL COLOR_MODE_SUBTRACT + #pragma shader_feature_local ENABLE_DETAIL + #pragma shader_feature_local ENABLE_UV_ANIMATION + #pragma shader_feature_local ENABLE_MASK + + sampler2D _MainTex; + fixed4 _Color; + fixed4 _TextureSampleAdd; + float4 _ClipRect; + float4 _MainTex_ST; + float _UIMaskSoftnessX; + float _UIMaskSoftnessY; + int _UIVertexColorAlwaysGammaSpace; + sampler2D _UvTex; + float2 _UvSpeed; + float _UvModifier; + sampler2D _MaskTex; + float4 _MaskTex_ST; + float2 _MaskSpeed; + sampler2D _DetailTex; + float4 _DetailTex_ST; + + 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 + }; + + half4 applyColor(half4 color, half4 factor) + { + #if COLOR_MODE_FILL + color.rgb = factor.rgb * color.a; + #elif COLOR_MODE_ADDITIVE + color.rgb += factor.rgb * color.a; + #elif COLOR_MODE_SUBTRACT + color.rgb -= factor.rgb * color.a; + #else + color.rgb *= factor.rgb; + #endif + + return color *= factor.a; + } + + 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))); + + if (_UIVertexColorAlwaysGammaSpace) + { + if (!IsGammaSpace()) + { + v.color.rgb = UIGammaToLinear(v.color.rgb); + } + } + + OUT.color = v.color * _Color; + return OUT; + } + + fixed4 frag(v2f IN) : SV_Target + { + //Round up the alpha color coming from the interpolator (to 1.0/256.0 steps) + //The incoming alpha could have numerical instability, which makes it very sensible to + //HDR color transparency blend, when it blends with the world's texture. + const half alphaPrecision = half(0xff); + const half invAlphaPrecision = half(1.0 / alphaPrecision); + float2 uv = IN.texcoord; + + #if ENABLE_UV_ANIMATION + uv += (tex2D(_UvTex, uv + _Time.y * _UvSpeed).rg - 0.5) * _UvModifier; + #endif + + IN.color.a = round(IN.color.a * alphaPrecision) * invAlphaPrecision; + half4 color = tex2D(_MainTex, uv) + _TextureSampleAdd; + + #if ENABLE_DETAIL + color.rgb = tex2D(_DetailTex, uv).rgb; + #endif + + #if 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 + + color.a *= SoftMask(IN.vertex, mul(unity_ObjectToWorld, IN.worldPosition)); // Add for soft mask + + + #if UNITY_UI_ALPHACLIP + SoftMaskClip (color.a - 0.001); + #endif + + #if ENABLE_MASK + const float2 maskUv = TRANSFORM_TEX(IN.texcoord.xy, _MaskTex); + color *= tex2D(_MaskTex, maskUv + _Time.y * _MaskSpeed).a; + #endif + + return applyColor(color, IN.color); + } + ENDCG + } + } +} \ No newline at end of file diff --git a/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader.meta b/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader.meta new file mode 100644 index 0000000..6a32deb --- /dev/null +++ b/Packages/src/Samples~/CompositeCanvasRenderer Support/Hidden-UI-CompositeCanvasRenderer-SoftMaskable.shader.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: a2cf3f24551924985a2e9cedbd477958 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/package.json b/Packages/src/package.json index a9e35e6..0f3eb19 100644 --- a/Packages/src/package.json +++ b/Packages/src/package.json @@ -37,6 +37,11 @@ "description": "TextMeshPro Support v3.2 or v4.0", "path": "Samples~/TextMeshPro Support v3.2 or v4.0~" }, + { + "displayName": "CompositeCanvasRenderer Support", + "description": "CompositeCanvasRenderer Support", + "path": "Samples~/CompositeCanvasRenderer Support" + }, { "displayName": "UIParticle Support", "description": "UIParticle Support", diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index 9de8035..321ffc3 100644 --- a/ProjectSettings/GraphicsSettings.asset +++ b/ProjectSettings/GraphicsSettings.asset @@ -44,6 +44,7 @@ GraphicsSettings: - {fileID: 4800000, guid: 9839189d918374a318d397a86e90aa73, type: 3} - {fileID: 4800000, guid: 2933b413a51fc4ff3a83c7ef4177ae84, type: 3} - {fileID: 4800000, guid: 33e06cd3c50064f30a691b600fc62f3f, type: 3} + - {fileID: 4800000, guid: a2cf3f24551924985a2e9cedbd477958, type: 3} m_PreloadedShaders: [] m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}