簡體   English   中英

Unity3D中的剪輯區域着色器

[英]clip area shader in Unity3D

首先,我對着色器編程一無所知,但我的游戲需要它,因此我在互聯網上找到了一個剪貼區域着色器,然后嘗試對其進行修改並為其添加一些功能! 我在我的精靈上添加了着色器,並在PC和某些Android設備上對其進行了檢查,但是之后,我在HTC One(我的朋友手機)上對其進行了檢查,但是正在發生奇怪的問題!

在此處輸入圖片說明

如您所見,左側在外觀上沒有剪切。

我發現的第一個着色器具有用於裁剪精靈的寬度和長度,然后我從左側添加了寬度,從右側添加了寬度,並向着色器添加了色彩。

有一個代碼:

Shader "Sprites/ClipAreaWithAlpha"
{
    Properties
    {
        _Color ("Color Tint", Color) = (1,1,1,1)
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
        _Length ("Length", Range(0.0, 1.0)) = 1.0
        _WidthR("Width from right", Range(0.0, 1.0)) = 1.0
        _WidthL ("Width from left", Range(0.0, 1.0)) = 0.0
     }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Length;
            float _WidthR;
            float _WidthL;
            half4 _Color;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                half4 color : COLOR;
            };

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = v.texcoord;
                o.color = _Color;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                if ((IN.texcoord.x<_WidthL) || (IN.texcoord.x>_WidthR) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
                {
                    half4 colorTransparent = half4(0,0,0,0) ;
                    return colorTransparent;
                }
                else
                {
                    half4 tex = tex2D(_MainTex, IN.texcoord);
                    tex.a = IN.color.a;
                    return tex;
                }
            }
            ENDCG
        }
    }
}

正如我之前所說,在我檢查過的另一台Android設備上使用此着色器還可以,但是在HTC One上,它無法正常工作。 我不知道問題出在哪里! 我很感謝解決方案。

並且此着色器具有以下警告: MaterialPropertyBlock is used to modify these values

我曾經也有過一樣的問題。 嘗試將“ Generate Mip Maps的值設置為off,然后着色器將正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM