summaryrefslogtreecommitdiff
path: root/VRCSDK3Avatars/Assets/_PoiyomiShaders/Shaders/7.3/Includes/CGI_PoiMath.cginc
blob: 98af23644ee283d1f5a32df4b5e970c31e1f64e2 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef POI_MATH
    #define POI_MATH
    
    #ifndef pi
        #define pi float(3.14159265359)
    #endif
    
    float4 quaternion_conjugate(float4 v)
    {
        return float4(
            v.x, -v.yzw
        );
    }
    
    float4 quaternion_mul(float4 v1, float4 v2)
    {
        float4 result1 = (v1.x * v2 + v1 * v2.x);
        
        float4 result2 = float4(
            - dot(v1.yzw, v2.yzw),
            cross(v1.yzw, v2.yzw)
        );
        
        return float4(result1 + result2);
    }
    
    // angle : radians
    float4 get_quaternion_from_angle(float3 axis, float angle)
    {
        return float4(
            cos(angle / 2.0),
            normalize(axis) * sin(angle / 2.0)
        );
    }
    
    float4 quaternion_from_vector(float3 inVec)
    {
        return float4(0.0, inVec);
    }
    
    float degree_to_radius(float degree)
    {
        return(
            degree / 180.0 * pi
        );
    }
    
    float3 rotate_with_quaternion(float3 inVec, float3 rotation)
    {
        float4 qx = get_quaternion_from_angle(float3(1, 0, 0), degree_to_radius(rotation.x));
        float4 qy = get_quaternion_from_angle(float3(0, 1, 0), degree_to_radius(rotation.y));
        float4 qz = get_quaternion_from_angle(float3(0, 0, 1), degree_to_radius(rotation.z));
        
        #define MUL3(A, B, C) quaternion_mul(quaternion_mul((A), (B)), (C))
        float4 quaternion = normalize(MUL3(qx, qy, qz));
        float4 conjugate = quaternion_conjugate(quaternion);
        
        float4 inVecQ = quaternion_from_vector(inVec);
        
        float3 rotated = (
            MUL3(quaternion, inVecQ, conjugate)
        ).yzw;
        
        return rotated;
    }
    
    float4 transform(float4 input, float4 pos, float4 rotation, float4 scale)
    {
        input.rgb *= (scale.xyz * scale.w);
        input = float4(
            rotate_with_quaternion(input.xyz, rotation.xyz * rotation.w)
        + (pos.xyz * pos.w),
        input.w
        );
        return input;
    }
    
#endif