Advertisement
tonynogo

Demo 96 - UV rotation with matrix

Jul 6th, 2017
13,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/UV rotation"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Angle ("Angle", Range(-5.0,  5.0)) = 0.0
  7.     }
  8.     SubShader
  9.     {
  10.         Tags { "RenderType"="Opaque" }
  11.        
  12.         Pass
  13.         {
  14.             CGPROGRAM
  15.             #pragma vertex vert
  16.             #pragma fragment frag
  17.             #include "UnityCG.cginc"
  18.  
  19.             struct v2f {
  20.                 float4 pos : SV_POSITION;
  21.                 float2 uv : TEXCOORD0;
  22.             };
  23.  
  24.             float _Angle;
  25.  
  26.             v2f vert(appdata_base v)
  27.             {
  28.                 v2f o;
  29.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  30.  
  31.                 // Pivot
  32.                 float2 pivot = float2(0.5, 0.5);
  33.                 // Rotation Matrix
  34.                 float cosAngle = cos(_Angle);
  35.                 float sinAngle = sin(_Angle);
  36.                 float2x2 rot = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);
  37.  
  38.                 // Rotation consedering pivot
  39.                 float2 uv = v.texcoord.xy - pivot;
  40.                 o.uv = mul(rot, uv);
  41.                 o.uv += pivot;
  42.  
  43.                 return o;
  44.             }
  45.  
  46.             sampler2D _MainTex;
  47.  
  48.             fixed4 frag(v2f i) : SV_Target
  49.             {
  50.                 // Texel sampling
  51.                 return tex2D(_MainTex, i.uv);
  52.             }
  53.  
  54.             ENDCG
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement