Skip to content

Instantly share code, notes, and snippets.

@smkplus
Created March 2, 2019 04:04
Show Gist options
  • Save smkplus/b0fdd86bc70c53886859505742b61a99 to your computer and use it in GitHub Desktop.
Save smkplus/b0fdd86bc70c53886859505742b61a99 to your computer and use it in GitHub Desktop.
Rotation Matrix Unity
Shader "Unlit/Rotation"
{
	Properties
	{
		_Rotation("Rotation",Vector) = (0,0,0,0)
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _Rotation;

			float4x4 rotationX(float angle ) {
			return float4x4(1.0,		0,			0,			0,
							0, 	cos(angle),	-sin(angle),		0,
							0, 	sin(angle),	 cos(angle),		0,
							0, 			0,			  0, 		1);
			}

			float4x4 rotationY( float angle ) {
			return float4x4(cos(angle),		0,		sin(angle),	0,
									0,		1.0,			 0,	0,
							-sin(angle),	0,		cos(angle),	0,
									0, 		0,				0,	1);
			}

			float4x4 rotationZ(float angle ) {
			return float4x4(cos(angle),		-sin(angle),	0,	0,
							sin(angle),		cos(angle),		0,	0,
									0,				0,		1,	0,
									0,				0,		0,	1);
			}

			
			v2f vert (appdata v)
			{
				v2f o;
				v.vertex = mul(v.vertex,rotationX(_Rotation.x/57.2958));
				v.vertex = mul(v.vertex,rotationY(_Rotation.y/57.2958));
				v.vertex = mul(v.vertex,rotationZ(_Rotation.z/57.2958));
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				return col;
			}
			ENDCG
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment