Search Unity

Question A Shader to Duplicate and Flip a texture

Discussion in 'Shaders' started by codenamed52, Jul 24, 2020.

  1. codenamed52

    codenamed52

    Joined:
    Apr 2, 2019
    Posts:
    4
    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.

    Code (CSharp):
    1. Shader "Custom/CustomShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         #pragma surface surf Standard fullforwardshadows
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.         };
    25.  
    26.         half _Glossiness;
    27.         half _Metallic;
    28.         fixed4 _Color;
    29.  
    30.         void surf (Input IN, inout SurfaceOutputStandard o)
    31.         {
    32.             float2 flippedUVs = IN.uv_MainTex;
    33.             flippedUVs.x = flippedUVs.x;
    34.             flippedUVs.y = 1.0 - flippedUVs.y;
    35.             fixed4 c = tex2D (_MainTex, flippedUVs) * _Color;
    36.  
    37.             o.Albedo = c.rgb;
    38.             o.Metallic = _Metallic;
    39.             o.Smoothness = _Glossiness;
    40.             o.Alpha = c.a;
    41.         }
    42.         ENDCG
    43.     }
    44.     FallBack "Diffuse"
    45. }
    46.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    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.
     
  3. SurprisedPikachu

    SurprisedPikachu

    Joined:
    Mar 12, 2020
    Posts:
    84
    @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):

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "UI/Mirror"
    4. {
    5.     Properties
    6.     {
    7.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    8.         _Color ("Tint", Color) = (1,1,1,1)
    9.  
    10.         _StencilComp ("Stencil Comparison", Float) = 8
    11.         _Stencil ("Stencil ID", Float) = 0
    12.         _StencilOp ("Stencil Operation", Float) = 0
    13.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    14.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    15.  
    16.         _ColorMask ("Color Mask", Float) = 15
    17.  
    18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    19.     }
    20.  
    21.     SubShader
    22.     {
    23.         Tags
    24.         {
    25.             "Queue"="Transparent"
    26.             "IgnoreProjector"="True"
    27.             "RenderType"="Transparent"
    28.             "PreviewType"="Plane"
    29.             "CanUseSpriteAtlas"="True"
    30.         }
    31.  
    32.         Stencil
    33.         {
    34.             Ref [_Stencil]
    35.             Comp [_StencilComp]
    36.             Pass [_StencilOp]
    37.             ReadMask [_StencilReadMask]
    38.             WriteMask [_StencilWriteMask]
    39.         }
    40.  
    41.         Cull Off
    42.         Lighting Off
    43.         ZWrite Off
    44.         ZTest [unity_GUIZTestMode]
    45.         Blend SrcAlpha OneMinusSrcAlpha
    46.         ColorMask [_ColorMask]
    47.  
    48.         Pass
    49.         {
    50.             Name "Default"
    51.             CGPROGRAM
    52.             #pragma vertex vert
    53.             #pragma fragment frag
    54.             #pragma target 2.0
    55.  
    56.             #include "UnityCG.cginc"
    57.             #include "UnityUI.cginc"
    58.  
    59.             #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
    60.             #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
    61.  
    62.             struct appdata_t
    63.             {
    64.                 float4 vertex : POSITION;
    65.                 float4 color : COLOR;
    66.                 float2 texcoord : TEXCOORD0;
    67.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    68.             };
    69.  
    70.             struct v2f
    71.             {
    72.                 float4 vertex : SV_POSITION;
    73.                 fixed4 color : COLOR;
    74.                 float2 texcoord : TEXCOORD0;
    75.                 float4 worldPosition : TEXCOORD1;
    76.                 UNITY_VERTEX_OUTPUT_STEREO
    77.             };
    78.  
    79.             sampler2D _MainTex;
    80.             fixed4 _Color;
    81.             fixed4 _TextureSampleAdd;
    82.             float4 _ClipRect;
    83.             float4 _MainTex_ST;
    84.  
    85.             v2f vert(appdata_t v)
    86.             {
    87.                 v2f OUT;
    88.                 UNITY_SETUP_INSTANCE_ID(v);
    89.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    90.  
    91.                 OUT.worldPosition = v.vertex;
    92.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
    93.                 OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    94.                 OUT.color = v.color * _Color;
    95.                 return OUT;
    96.             }
    97.  
    98.             fixed4 frag(v2f IN) : SV_Target
    99.             {
    100.                  if (IN.texcoord.x < 0.5)
    101.                      IN.texcoord.x *= 2;
    102.                  else
    103.                      IN.texcoord.x  = (1 - IN.texcoord.x) * 2;
    104.              
    105.                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    106.  
    107.                 #ifdef UNITY_UI_CLIP_RECT
    108.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    109.                 #endif
    110.  
    111.                 #ifdef UNITY_UI_ALPHACLIP
    112.                 clip (color.a - 0.001);
    113.                 #endif
    114.  
    115.                 return color;
    116.             }
    117.             ENDCG
    118.         }
    119.     }
    120. }
    Any ideas?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    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.
     
    SurprisedPikachu likes this.