Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Translating and Rotating textures/ UVs

Discussion in 'Shaders' started by Kacti, Feb 9, 2023.

  1. Kacti

    Kacti

    Joined:
    Apr 14, 2016
    Posts:
    2
    Hi there,

    So, I am very new to shaders and all that stuff and I have been trying to work on rotating and translating my texture/UVs via user input. Currently, the user can rotate the texture but when it comes to translation the texture will move in the newly rotated direction, not along a world space direction.

    What I want is very similar to how in Unity you can rotate an object but can still move it around with the world space coordinates and for that object to travel along XY world coordinates.

    I have tried in Unity's shader language and Unity's shader graph.

    Thanks for any help.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    Move first, then rotate.
     
  3. Kacti

    Kacti

    Joined:
    Apr 14, 2016
    Posts:
    2
    Hi there, So when I translate first my pivot point becomes a translation point instead. Sorry, it's a little hard to explain to me when I'm pretty unfamiliar.

    Code (CSharp):
    1. Shader "Unlit/TRSHLSL"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Angle ("Angle", Float) = 0.0
    7.        _Pivot ("Pivot", Vector) = (0.5,0.5,0,0)
    8.        _Translate("Translate", Vector) = (0,0,0,0)
    9.  
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 100
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             float _Angle;
    39.             float2 _Pivot;
    40.             float2 _Translate;
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 o.uv.xy += _Translate;
    48.                 //o.vertex = mul(UNITY_MATRIX_VP, fixed4(worldPosition, 1.0f))
    49.                 //rotate uvs around axis
    50.                 //_Pivot = (0.5,0.5);
    51.  
    52.              
    53.                 o.uv -= _Pivot.xy;
    54.                
    55.                 // Rotation Matrix
    56.                 float s = sin(_Angle);
    57.                 float c = cos(_Angle);
    58.                 float4x4 m = float4x4(c,-s,0,0,
    59.                                     s,c,0,0,
    60.                                     0,0,1,0,
    61.                                     0,0,0,1);
    62.                
    63.                 float2 newUV = o.uv - _Pivot.xy;
    64.                 o.uv = mul(m,o.uv);
    65.                 o.uv += _Pivot.xy;
    66.                
    67.  
    68.                 return o;
    69.             }
    70.  
    71.             fixed4 frag (v2f i) : SV_Target
    72.             {
    73.                 // sample the texture
    74.                 fixed4 col = tex2D(_MainTex, i.uv);
    75.                 return col;
    76.             }
    77.             ENDCG
    78.         }
    79.     }
    80. }