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. Dismiss Notice

Rotation of Texture/UVs directly from a shader

Discussion in 'Shaders' started by rymbrant, Sep 9, 2012.

  1. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Don’t use a surface shader. Right click in the project view and create a new unlit shader. You can copy the contents of the above vert function into the start of the vertex shader (in addition to what’s already there), change texcoord to uv, and you’re done.
     
  2. Handman50

    Handman50

    Joined:
    Feb 12, 2018
    Posts:
    2
    That made it look just like the shader I want, but there's no rotation.

    Code (CSharp):
    1. Shader "Unlit/rotatingUNLIT"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _RotationSpeed ("Rotation Speed", Float) = 2.0
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Transparent" }
    11.         LOD 100
    12.  
    13.         ZWrite Off
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             // make fog work
    22.             #pragma multi_compile_fog
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 UNITY_FOG_COORDS(1)
    36.                 float4 vertex : SV_POSITION;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             struct Input {
    42.                 float2 uv_MainTex;
    43.             };
    44.            
    45.             float _RotationSpeed;
    46.            
    47.             v2f vert (appdata v)
    48.             {
    49.                 v2f o;
    50.                 o.vertex = UnityObjectToClipPos(v.vertex);
    51.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    52.                 UNITY_TRANSFER_FOG(o,o.vertex);
    53.                 v.uv.xy -=0.5;
    54.                 float s = sin ( _RotationSpeed * _Time );
    55.                 float c = cos ( _RotationSpeed * _Time );
    56.                 float2x2 rotationMatrix = float2x2( c, -s, s, c);
    57.                 rotationMatrix *=0.5;
    58.                 rotationMatrix +=0.5;
    59.                 rotationMatrix = rotationMatrix * 2-1;
    60.                 v.uv.xy = mul ( v.uv.xy, rotationMatrix );
    61.                 v.uv.xy += 0.5;
    62.                 return o;
    63.             }
    64.            
    65.             fixed4 frag (v2f i) : SV_Target
    66.             {
    67.                 // sample the texture
    68.                 fixed4 col = tex2D(_MainTex, i.uv);
    69.                 // apply fog
    70.                 UNITY_APPLY_FOG(i.fogCoord, col);
    71.                 return col;
    72.             }
    73.        
    74.             ENDCG
    75.         }
    76.     }
    77. }
    78.  
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    You're modifying v.uv after it's already been copied to o.uv, which is the actual value that gets passed from the vertex to the fragment. This means the rotation is ignored. Move that to the top of the vertex function, or at least before the "o.uv =" line. Alternatively change v.uv to o.uv in the code handling the rotation.
     
  4. ritamoreno

    ritamoreno

    Joined:
    Jan 28, 2021
    Posts:
    1
    how would i be able to apply this to one texture on a shader with multiple textures?
     
    xaldin-76 and Bdiebeak like this.