Skip to content

Commit

Permalink
feat: add stereo mode to shader
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The implementation way of the 'SoftMaskable' shader has been changed. Please refer to the "Migrating from v1 to v2" section in the Readme for details.
  • Loading branch information
mob-sakai committed Feb 10, 2024
1 parent 39a33a7 commit e2e6733
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ Shader "Hidden/UI/Default (SoftMaskable)"
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]

Pass
{
Name "Default"
CGPROGRAM
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0

#include "UnityCG.cginc"
#include "UnityUI.cginc"

#pragma multi_compile __ UNITY_UI_CLIP_RECT
#pragma multi_compile __ UNITY_UI_ALPHACLIP
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP

#include "Packages/com.coffee.softmask-for-ugui/Shaders/SoftMask.cginc" // Add for soft mask
#pragma shader_feature __ SOFTMASK_EDITOR // Add for soft mask
#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)

struct appdata_t
{
Expand All @@ -73,6 +75,7 @@ Shader "Hidden/UI/Default (SoftMaskable)"
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
float4 mask : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};

Expand All @@ -81,37 +84,68 @@ Shader "Hidden/UI/Default (SoftMaskable)"
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
float _UIMaskSoftnessX;
float _UIMaskSoftnessY;
int _UIVertexColorAlwaysGammaSpace;

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 = UnityObjectToClipPos(OUT.worldPosition);

OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
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 202220 <= UNITY_VERSION
if (_UIVertexColorAlwaysGammaSpace)
{
if(!IsGammaSpace())
{
v.color.rgb = UIGammaToLinear(v.color.rgb);
}
}
#endif

OUT.color = v.color * _Color;

return OUT;
}

fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
//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);
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

color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
color.a *= SoftMask(IN.vertex, mul(unity_ObjectToWorld, IN.worldPosition)); // Add for soft mask

#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
clip (color.a - 0.001);
#endif

color.a *= SoftMask(IN.vertex, IN.worldPosition); // Add for soft mask
color.rgb *= color.a;

return color;
}
ENDCG
ENDCG
}
}
}
52 changes: 52 additions & 0 deletions Packages/src/Shaders/Hidden-UI-SoftMask.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Shader "Hidden/UI/SoftMask"
{
Properties
{
[Enum (UnityEngine.Rendering.BlendOp)] _BlendOp ("BlendOp", float) = 1
}

SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
}
LOD 100

Cull Off
ZWrite Off
Blend SrcColor One
BlendOp [_BlendOp]

Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma target 2.0

#include "UnityCG.cginc"

sampler2D _MainTex;
float _ThresholdMin;
float _ThresholdMax;
float4 _ColorMask;

float invLerp(const float from, const float to, const float value)
{
return saturate(max(0, value - from) / max(0.000000001, to - from));
}

fixed4 frag(v2f_img i) : SV_Target
{
const half maxValue = max(_ThresholdMin, _ThresholdMax);
const half minValue = min(_ThresholdMin, _ThresholdMax);
const half alpha = invLerp(minValue, maxValue, tex2D(_MainTex, i.uv).a);
return alpha * _ColorMask;
}
ENDCG
}
}
}
78 changes: 78 additions & 0 deletions Packages/src/Shaders/Hidden-UI-TerminalMaskingShape.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Shader "Hidden/UI/TerminalMaskingShape"
{
Properties
{
_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) = 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 Off
Blend Zero Zero
ColorMask 0

Pass
{
Name "Terminal"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct v2f
{
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};


v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.vertex = UnityObjectToClipPos(v.vertex);
return OUT;
}

fixed4 frag(v2f IN) : SV_Target
{
return fixed4(0, 0, 0, 0);
}
ENDCG
}
}
}

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

8 changes: 0 additions & 8 deletions Packages/src/Shaders/Resources.meta

This file was deleted.

33 changes: 0 additions & 33 deletions Packages/src/Shaders/Resources/SoftMask.shader

This file was deleted.

68 changes: 0 additions & 68 deletions Packages/src/Shaders/SoftMask.cginc

This file was deleted.

Loading

0 comments on commit e2e6733

Please sign in to comment.