Skip to content

Instantly share code, notes, and snippets.

@dearenot
Created February 12, 2017 07:22
Show Gist options
  • Select an option

  • Save dearenot/6941f4f1108a9acdb8951d5d6e7a51b6 to your computer and use it in GitHub Desktop.

Select an option

Save dearenot/6941f4f1108a9acdb8951d5d6e7a51b6 to your computer and use it in GitHub Desktop.
Shader "Unlit/Unlit UV Rotation of multiple textures in vertex"
{
Properties
{
_MainTex ("Texture", 2D) = "" {}
_RotatedTexA ("Texture", 2D) = "" {}
_RotationA ("Rotation", Range(0,360)) = 0.0
_RotatedTexB ("Texture", 2D) = "" {}
_RotationB ("Rotation", Range(0,360)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
float2 rotateUV(float2 uv, float degrees)
{
// rotating UV
const float Deg2Rad = (UNITY_PI * 2.0) / 360.0;
float rotationRadians = degrees * Deg2Rad; // convert degrees to radians
float s = sin(rotationRadians); // sin and cos take radians, not degrees
float c = cos(rotationRadians);
float2x2 rotationMatrix = float2x2( c, -s, s, c); // construct simple rotation matrix
uv -= 0.5; // offset UV so we rotate around 0.5 and not 0.0
uv = mul(rotationMatrix, uv); // apply rotation matrix
uv += 0.5; // offset UV again so UVs are in the correct location
return uv;
}
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 uv2 : TEXCOORD1; // Addition additional UV to pass
UNITY_FOG_COORDS(2) // changed from 1 to 2 since uv2 is using TEXCOORD1 now
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _RotatedTexA;
float4 _RotatedTexA_ST;
float _RotationA;
sampler2D _RotatedTexB;
float4 _RotatedTexB_ST;
float _RotationB;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv2.xy = TRANSFORM_TEX(rotateUV(v.uv, _RotationA), _RotatedTexA);
o.uv2.zw = TRANSFORM_TEX(rotateUV(v.uv, _RotationB), _RotatedTexB);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// sample rotated textures
fixed4 colA = tex2D(_RotatedTexA, i.uv2.xy);
fixed4 colB = tex2D(_RotatedTexB, i.uv2.zw);
// adding the textures together just so you can see them all
col = (col + colA + colB) / 7;
// apply fog
// UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment