Free : simple texture rotator shader

Hi guys,

i made this simple texture rotator for those who need a code example

Victor

Shader "Custom/texture_rotator" {
   Properties {
        _Color ("Diffuse Color", Color) = (1,1,1,1)
        _MainTex ("Base layer (RGB)", 2D) = "white" {}
        _TextureRot ("rotation (-180 + 180)", Range(-180, 180)) = 0
        _Xcenter ("X center (0 - 100)", Range(0, 100)) = 50
        _Ycenter ("Y center (0 - 100)", Range(0, 100)) = 50
    }
      SubShader {
  
        Tags{"Queue"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha
        //Lighting On
        ZWrite On
    
    Pass { //pass1
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"


         float _TextureRot;
         fixed _Xcenter;
         fixed _Ycenter;
         float4 _MainTex_ST;
         float4 _Color;
     
         sampler2D _MainTex;
 
         struct v2f {
              float4 pos : SV_POSITION;
              fixed4 color : COLOR;
              float2 uv : TEXCOORD0; // stores uv
          };
  
          v2f vert (appdata_full v)
          {
              v2f o;

            o.uv = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
           
              o.pos = mul (UNITY_MATRIX_MVP, v.vertex );
        
            //assign color and alpha
            o.color.xyz = _Color.xyz;
            o.color.w = 1;

            return o;
        }
    
        fixed4 frag (v2f i) : COLOR
        {
            fixed4 o;
             ///////////////////// begins here /////////////////////////
            float angle1, angle2;
            fixed2 center = (0.5,0.5); // center of rotation
            _TextureRot = radians(_TextureRot);
        
            float d;// distance between center and UV coord
        
            center.x = (_Xcenter/100); // center of rotation X
            center.y = (_Ycenter/100); // center of rotation X
        
             float2 shift=0;    // shift defines UV coords transformation
         
             // distance between center and UV coord based on center of rotation
             d=sqrt(pow(i.uv.x-center.x,2)+pow(i.uv.y-center.y,2));
         
             // calculate angle between center and UV coord
             angle1 = atan2 ( (i.uv.y-center.y)*d , (i.uv.x-center.x)*d );

             angle2 =  _TextureRot-angle1;

             shift.x = (cos(angle2)-cos(angle1))*d;
             shift.y = (sin(angle2)-sin(angle1))*d;
             //////////////////////////////////////
        
            fixed4 tex = tex2D (_MainTex, i.uv+shift ); // <<< shifting UVs
            o.rgb =tex;
            
            o.a = tex.a;
        
            return o;
          }
           
        ENDCG
    }//pass 1
  } //subshader
}//shader
1 Like

I also posted a shader that rotates texture coordinates a while ago. The version I posted simply computes UV’s inside the vertex shader and does not make use of sqrt/pow and atan2, so it should be cheaper.

You might want to take a look at it:

well that was just in case, i’m not a god of shaders tough
maybe you could post your code there, that woud help
thanks

also this one, an old thread. . . just in case Rotation of Texture/UVs directly from a shader - Unity Engine - Unity Discussions

1 Like