Unity3D中的剪辑区域着色器

时间:2015-09-21 09:53:28

标签: unity3d shader crop

首先,我不知道着色器编程,但我需要它来进行游戏,所以我在互联网上找到了一个剪辑区域着色器,然后我试图修改它并为它添加一些功能!我在我的精灵上添加了着色器,并在我的电脑和一些安卓设备上进行了检查,但之后我在HTC One(我的朋友手机)上检查了它,但是发生了奇怪的问题!

enter image description here

如您所见,左侧没有视觉剪裁。

我找到的第一个着色器只有宽度和长度用于剪切精灵,然后我从左边增加了宽度,从右边增加了宽度和颜色色调到着色器。

有一个代码:

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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。尝试将Generate Mip Maps的值设置为关闭,然后着色器正常工作。

相关问题