Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Pattern tiling based on a mask (or conditional tiling)

Discussion in 'Shaders' started by Cray_Gor, Aug 12, 2023.

  1. Cray_Gor

    Cray_Gor

    Joined:
    May 15, 2023
    Posts:
    4
    I have an hex image 20x20pixel.
    I have a black texture 2000x2000 pixels.
    I have a "mask" pattern that is black and white texture with dimension 100x100 pixels that will say how the pattern is made (white pixel = yes, black pixel = no).
    In this example, I will copy/paste the hex image 100 times in width and 100 times in height.

    I want to produce an image using Graphics.blit.

    What I want to do, is basically recreate the "Repeat" function in photoshop but with conditionnal pattern :
    upload_2023-8-12_9-27-20.png


    upload_2023-8-12_9-27-39.png

    For the moment I'm good for the scaling that I can choose, but the pixel art only appear as a "mask" like when you multiply two image, and I can't handle space between pattern.
    I have also difficulty to add a row shift when row is even (or odd).

    upload_2023-8-12_9-25-1.png

    I want to use Shader or ShaderGraph.


    Here my shader that is not working :

    Code (CSharp):
    1. Shader "Custom/PatternShader"
    2. {
    3.     Properties
    4.     {
    5.         _Pattern("Pattern Texture", 2D) = "white" {}
    6.         _Result("Result Texture", 2D) = "white" {}
    7.         _PatternCondition("Pattern Condition", 2D) = "white" {}
    8.         _Scale("Scale", Range(1, 100)) = 1
    9.         _Offset("Offset", Range(0, 1)) = 0.5
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float2 uvPatternCondition : TEXCOORD1;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             sampler2D _Pattern;
    35.             sampler2D _Result;
    36.             sampler2D _PatternCondition;
    37.  
    38.             float _Scale;
    39.             float _Offset;
    40.  
    41.             v2f vert(appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.  
    46.                 o.uvPatternCondition = v.uv * _Scale;
    47.                 float rowIndex = floor(o.uvPatternCondition.y);
    48.                 float xOffset = (rowIndex % 2) * _Offset;
    49.                 o.uv = float2(v.uv.x + xOffset, v.uv.y);
    50.  
    51.                 return o;
    52.             }
    53.  
    54.             half4 frag(v2f i) : SV_Target
    55.             {
    56.                 half4 patternColor = tex2D(_Pattern, i.uv);
    57.                 half4 resultColor = tex2D(_Result, i.uv);
    58.                 half condition = tex2D(_PatternCondition, i.uvPatternCondition).r;
    59.  
    60.                 if (condition > 0.5)
    61.                     return patternColor;
    62.                 else
    63.                     return resultColor;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    69.