Search Unity

Question How rotate Mask texture in shader?

Discussion in 'Shaders' started by LoopIssuer, Aug 9, 2022.

  1. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    Hi,
    I have this shader (not mine, I can't write shaders). How to rotate only Mask for i.e. 270 degrees?
    Code (CSharp):
    1. Shader "Unlit/Unmask Shader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "" {}
    6.         _MaskTex ("Mask Texture", 2D) = "blue" {}
    7.         _Width ("Mask Width", Int) = 0
    8.         _Height ("Mask Height", Int) = 0
    9.         _Threshold ("Threshold", Range(0.0, 1.0)) = 0.9
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Transparent" }
    15.         LOD 100
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             // make fog work
    23.             #pragma multi_compile_fog
    24.  
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 UNITY_FOG_COORDS(1)
    37.                 float4 vertex : SV_POSITION;
    38.             };
    39.  
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.  
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = UnityObjectToClipPos(v.vertex);
    47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    48.                 UNITY_TRANSFER_FOG(o,o.vertex);
    49.                 return o;
    50.             }
    51.  
    52.             sampler2D _MaskTex;
    53.  
    54.             int _Width;
    55.             int _Height;
    56.             float _Threshold;
    57.             uniform StructuredBuffer<float> _MaskBuffer;
    58.  
    59.             fixed4 frag (v2f i) : SV_Target
    60.             {
    61.                 // sample the texture
    62.                 fixed4 mainCol = tex2D(_MainTex, i.uv);
    63.                 fixed4 maskCol = tex2D(_MaskTex, i.uv);
    64.                 int idx = int(i.uv.y * _Height) * _Width + int(i.uv.x * _Width);
    65.                 float mask = _MaskBuffer[idx];
    66.                 fixed4 col = lerp(maskCol, lerp(maskCol, mainCol, mask), step(_Threshold, mask));
    67.  
    68.                 // apply fog
    69.                 UNITY_APPLY_FOG(i.fogCoord, col);
    70.                 return col;
    71.             }
    72.             ENDCG
    73.         }
    74.     }
    75. }
    76.  
    Thank you for help in advance!
     
    Last edited: Aug 9, 2022
  2. The3Loutrons

    The3Loutrons

    Joined:
    Apr 4, 2016
    Posts:
    7
    Hi

    It seems the _MaskBuffer is directly sent to the shader from the C# code: wouldn't it be easier to rotate it from there?

    Rgds
    The3Loutrons
     
  3. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    But how?
     
  4. The3Loutrons

    The3Loutrons

    Joined:
    Apr 4, 2016
    Posts:
    7
    Don’t you currently have an C# code that is working with this shader?
     
  5. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    Yeah, but it only sets a mask
    _material.SetTexture("_MaskTex", maskTexture)

    What I need is to rotate that Mask texture for 90 or 270 degrees when MainTexture stays as it is.
     
  6. The3Loutrons

    The3Loutrons

    Joined:
    Apr 4, 2016
    Posts:
    7
    Ok so this is this maskTexture in C# to work on: how is it declared and initialized?
     
  7. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    It could be declared from code as new Texture or from inspector.