MediaPipeUnityPlugin icon indicating copy to clipboard operation
MediaPipeUnityPlugin copied to clipboard

Selfie Segmentation + Edge effect

Open taeyeonlee opened this issue 1 year ago • 0 comments

Plugin Version or Commit ID

v0.11.0

Unity Version

2021.3.18f1

Your Host OS

Windows 10

Target Platform

Windows Standalone

Description

I want to add Edge effect on the Selfie Segmentation. I modified the UnmaskShader.shader, like below. It shows a strange image. Could you please help ?

Code to Reproduce the issue

D:\MediaPipeUnityPlugin-all\Packages\com.github.homuler.mediapipe\PackageResources\Shaders\UnmaskShader.shader Shader "Unlit/MediaPipe/Unmask Shader" { Properties { _MainTex ("Main Texture", 2D) = "" {} _MaskTex ("Mask Texture", 2D) = "blue" {} _Width ("Mask Width", Int) = 0 _Height ("Mask Height", Int) = 0 _Threshold ("Threshold", Range(0.0, 1.0)) = 0.9 [HideInInspector]_PixDistMulAdd("Pixel Dist, Mul, Adder", vector) = (2,-0.98,0.2001,0) //(1,1,1,0) _Color("Effect Color", Color) = (1,1,1,1) }

SubShader
{
    Tags { "RenderType"="Transparent" }
    Blend SrcAlpha OneMinusSrcAlpha
    LOD 100

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        // make fog work
        #pragma multi_compile_fog

        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
        };

        fixed4 _Color;
        sampler2D _MainTex;
        float4 _MainTex_ST;
        float4 _PixDistMulAdd;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            UNITY_TRANSFER_FOG(o,o.vertex);
            return o;
        }

        float4 ApplyKernelMatrix(float2 uv, float2 pixelDist)
   {
	float2 l_uv = uv;
	float4 l_fx = tex2D(_MainTex, uv) * 8;  
	l_uv.x -= pixelDist.x;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.y -= pixelDist.y;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.x = uv.x;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.x += pixelDist.x;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.y = uv.y;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.y += pixelDist.y;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.x = uv.x;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	l_uv.x -= pixelDist.x;
	l_fx += tex2D(_MainTex, l_uv) * -1;  
	return l_fx;
   }

        sampler2D _MaskTex;

        int _Width;
        int _Height;
        float _Threshold;
        uniform StructuredBuffer<float> _MaskBuffer;

        fixed4 frag (v2f i) : SV_Target
        {
            // sample the texture
            fixed4 mainCol = tex2D(_MainTex, i.uv);
            fixed4 maskCol = tex2D(_MaskTex, i.uv);
            int idx = int(i.uv.y * _Height) * _Width + int(i.uv.x * _Width);
            float mask = _MaskBuffer[idx];
            fixed4 col = lerp(maskCol, lerp(maskCol, mainCol, mask), step(_Threshold, mask));

            // apply fog

// UNITY_APPLY_FOG(i.fogCoord, col);

            // Edge detection
			float2 pixDist = _MainTex_ST.xy * _PixDistMulAdd.xy;
            float4 fx = col;
            fx = ApplyKernelMatrix(i.uv, pixDist);
			fx *= _PixDistMulAdd.z;
			fx += _PixDistMulAdd.w;
			_Color = (1,1,1,1);
			fixed4 res = fx * _Color;
			res.a = col.a;
            return res;
        }
        ENDCG
    }
}

}

Additional Context

No response

taeyeonlee avatar Jun 09 '23 13:06 taeyeonlee