A Shader to Duplicate and Flip a texture

I have a half of a symmetrical texture like this here:

I intend to use shader to duplicate and flip it horizontally with the end result like so:

I have written a shader that accomplishes the flipping part. But I’m not sure how to duplicate the effect before flipping.

Shader "Custom/CustomShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            float2 flippedUVs = IN.uv_MainTex;
            flippedUVs.x = flippedUVs.x;
            flippedUVs.y = 1.0 - flippedUVs.y;
            fixed4 c = tex2D (_MainTex, flippedUVs) * _Color;

            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

No need for a custom shader to do this. Set the texture itself to use Wrap Mode > Mirror and set the x Tiling on a material using the Standard shader to 2.

@codenamed52 did you figure it out? I’m trying to achieve the same result with UI Image. I have this shader but it doesn’t work with sprite atlas (it’s also better to replace if statements with clever math but I’m trying to get it to work first):

// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "UI/Mirror"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)

        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _ColorMask ("Color Mask", Float) = 15

        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp]
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

        Pass
        {
            Name "Default"
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
            #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

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

            struct v2f
            {
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            float4 _ClipRect;
            float4 _MainTex_ST;

            v2f vert(appdata_t v)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);

                OUT.worldPosition = v.vertex;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
                OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                OUT.color = v.color * _Color;
                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                 if (IN.texcoord.x < 0.5)
                     IN.texcoord.x *= 2;
                 else
                     IN.texcoord.x  = (1 - IN.texcoord.x) * 2;
             
                half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;

                #ifdef UNITY_UI_CLIP_RECT
                color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                #endif

                #ifdef UNITY_UI_ALPHACLIP
                clip (color.a - 0.001);
                #endif

                return color;
            }
            ENDCG
        }
    }
}

Any ideas?

Depending on the sprite you’re using with the UI Image component, it might not be possible to (easily) solve with a shader. For example if your sprite is packed in an atlas (either by you importing an atlas manually, or setting the texture to be combined into an atlas by Unity), there’s absolutely nothing in the shader can do here without a lot of manual help from you. The UV position within an atlas are now completely arbitrary, and Unity doesn’t pass any of that information to the shader automatically, so you can’t rely on 0.0 being the left side of the texture, or 1.0 being the right, or 0.5 to be the center of the UI Image mesh. You have to pass all of that information to the shader yourself, which in the case of the automatic atlasing means having to extract the Sprite’s UV range at runtime with a custom script and pass those values to the shader.

The easiest solution to this is to not atlas any sprite you want to do this with, and to make sure it takes up the full range of the texture, no transparent edge at least where you want to mirror. And once you do that you can set the texture’s wrap mode to mirror and avoid needing a custom shader, though setting the UV scaling on UI materials is a bit of a pain since it’s hidden by default, but is possible.

1 Like