Skip to content

Commit

Permalink
feat: add CompositeCanvasRenderer support
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 25, 2024
1 parent ba7e928 commit 4cda9bb
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Packages/src/Samples~/CompositeCanvasRenderer Support.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Packages/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 4cda9bb

Please sign in to comment.