summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Utils/QuaternionUtility.cs
blob: d491b1feac332255cf0b708cd5a130cd815a17ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class QuaternionUtility 
{

	public static Matrix4x4 ToMatrix(ref this Quaternion q)
	{
		Matrix4x4 m = new Matrix4x4();

		// Precalculate coordinate products
		float x = q.x * 2.0F;
		float y = q.y * 2.0F;
		float z = q.z * 2.0F;
		float xx = q.x * x;
		float yy = q.y * y;
		float zz = q.z * z;
		float xy = q.x * y;
		float xz = q.x * z;
		float yz = q.y * z;
		float wx = q.w * x;
		float wy = q.w * y;
		float wz = q.w * z;

		// Calculate 3x3 matrix from orthonormal basis
		m[0] = 1.0f - (yy + zz);
		m[1] = xy + wz;
		m[2] = xz - wy;
		m[3] = 0.0F;

		m[4] = xy - wz;
		m[5] = 1.0f - (xx + zz);
		m[6] = yz + wx;
		m[7] = 0.0F;

		m[8] = xz + wy;
		m[9] = yz - wx;
		m[10] = 1.0f - (xx + yy);
		m[11] = 0.0F;

		m[12] = 0.0F;
		m[13] = 0.0F;
		m[14] = 0.0F;
		m[15] = 1.0F;

		return m;
	}


}