【问题标题】:Unity3D - A shader that will clip a texture on iOS deviceUnity3D - 将在 iOS 设备上剪辑纹理的着色器
【发布时间】:2013-05-06 10:42:39
【问题描述】:

我是第一次使用 Unity 着色器,但在制作只能绘制纹理的一部分的着色器时遇到了问题。下面的代码适用于 mac 独立版本,但不适用于 iOS 设备。我该如何解决?

Shader "Sprite/ClipArea"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
        _Length ("Length", Range(0.0, 1.0)) = 1.0
        _Width ("Width", Range(0.0, 1.0)) = 1.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 _Width;

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

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

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

            half4 frag (v2f IN) : COLOR
            {
                if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
                {
                    clip(-1);
                }
                return tex2D(_MainTex, IN.texcoord);
            }
            ENDCG
        }
    }
}

谢谢!

【问题讨论】:

  • 您的代码如何“不起作用”?不做任何剪辑,剪辑太多,太少,抖动等?
  • 它不会剪切纹理。

标签: ios unity3d shader clip


【解决方案1】:

在移动设备中,建议不要使用剪辑/丢弃方法,因为这是一项非常昂贵的操作。

我更新了片段着色器部分,没有使用剪辑/丢弃方法。在这里,不要剪裁,而是让您不想以透明颜色显示的部分,而对于其余部分,按原样呈现。

if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
{
    half4 colorTransparent = half4(0,0,0,0) ;
    return colorTransparent ;
}
else
    return tex2D(_MainTex, IN.texcoord) ;

【讨论】:

  • 谢谢,以后可能有用:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多