实现一个涂抹擦除效果

 

涂抹还是满常见的效果。

要做涂抹,首先要存一张中间贴图作为mask。

然后需要两个shader,一个做mask一个做混合。

 

MaskShader:

复制代码
Shader "Unlit/MaskShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _MaskDecalTex("Mask Decal Texture", 2D) = "white" {}
        _MaskOffset("Mask Offset", vector) = (0,0,0,0)
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        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;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _MaskOffset;

            sampler2D _MaskDecalTex;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 r = tex2D(_MainTex, i.uv);

#if UNITY_UV_STARTS_AT_TOP
                float grabSign = -_ProjectionParams.x;
#else
                float grabSign = _ProjectionParams.x;
#endif

                half2 uv = float2(1, grabSign) * (i.uv - half2(0.5, 0.5)) / _MaskOffset.ww + half2(0.5, 0.5);

                fixed4 mask = tex2D(_MaskDecalTex, uv + _MaskOffset.xy);
                return r + mask;
            }
            ENDCG
        }
    }
}
复制代码

 

 

BlendShader:

复制代码
Shader "Unlit/BlendShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _SecondTex("Second Texture", 2D) = "white" {}
        _MaskTex("Mask Texture", 2D) = "white" {}
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        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;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _SecondTex;
            sampler2D _MaskTex;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col1 = tex2D(_MainTex, i.uv);
                fixed4 col2 = tex2D(_SecondTex, i.uv);

#if UNITY_UV_STARTS_AT_TOP
                float grabSign = -_ProjectionParams.x;
#else
                float grabSign = _ProjectionParams.x;
#endif

                half2 uv = float2(1, grabSign) * (i.uv - half2(0.5, 0.5)) + half2(0.5, 0.5);

                fixed4 mask = tex2D(_MaskTex, uv);
                return lerp(col1, col2, mask.a);
            }
            ENDCG
        }
    }
}
复制代码

 

脚本(核心部分):

复制代码
Vector4 viewPortPoint = Camera.main.WorldToViewportPoint(maskMappingPoint.position);

viewPortPoint -= new Vector4(0.5f, 0.5f);
viewPortPoint.w = maskMappingPoint.localScale.x;

if (mPersistMaskTex == null)
{
    mPersistMaskTex = new Texture2D(src.width, src.height, TextureFormat.RGBA32, false, true);
    for (int x = 0; x < mPersistMaskTex.width; x++)
        for (int y = 0; y < mPersistMaskTex.height; y++)
            mPersistMaskTex.SetPixel(x, y, new Color(0, 0, 0, 0));

    mPersistMaskTex.Apply();
}

maskMat.SetTexture("_MainTex", mPersistMaskTex);
maskMat.SetVector("_MaskOffset", viewPortPoint);
Graphics.Blit(mPersistMaskTex, maskRT, maskMat);

blendMat.SetTexture("_MainTex", src);
blendMat.SetTexture("_SecondTex", tempRT);
blendMat.SetTexture("_MaskTex", maskRT);
Graphics.Blit(src, des, blendMat);

var cacheActive = RenderTexture.active;

RenderTexture.active = maskRT;

mPersistMaskTex.ReadPixels(new Rect(0, 0, mPersistMaskTex.width, mPersistMaskTex.height), 0, 0);
mPersistMaskTex.Apply();

RenderTexture.active = cacheActive;
复制代码

 

2018/11/18补充:近期实现了对涂抹像素的判断操作,涂抹到一定程度后则完成擦除:

https://www.cnblogs.com/hont/p/9977401.html

posted @   HONT  阅读(5581)  评论(3编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2013-08-03 RhinoMock初探
点击右上角即可分享
微信分享提示
回到顶部