1 module Engine.Transform;
2 
3 import Engine.Core;
4 import Engine.Component;
5 import std.stdio;
6 
7 
8 
9 class Transform  {
10     mixin ComponentBase;
11     @property ref vec3 Position()  {
12         updateInvert = true;
13         updatePos = true;
14         return position;
15     };
16 
17     @property ref vec3 Scale()() {
18         updateInvert = true;
19         updateScale = true;
20         return scale;
21     };
22 
23     
24     @property ref vec3 Rotation()() {
25         updateInvert = true;
26         updateRot = true;
27         return rotation;
28     };
29 
30     vec3 position = vec3(0,0,0);
31     vec3 rotation = vec3(0,0,0);
32     vec3 scale = vec3(1,1,1);
33 
34     package mat4 matrix;
35     package mat4 translateMatrix;
36     package mat4 scaleMatrix;
37     package mat4 rotateMatrix;
38     package mat4 invertMatrix;
39     package bool updateInvert = true;
40 
41     
42     package bool updatePos = true;
43     package bool updateRot = true;
44     package bool updateScale = true;
45 
46     this() {
47 
48     }
49 
50     public void OnComponentAdd() {
51         entity.transform_ = this;
52     }
53 
54     mat4 Matrix()() {	
55         bool recalculate = false;
56         if (updateRot) {
57             rotateMatrix = mat4.identity.rotatex(rotation.x).rotatey(rotation.y).rotatez(rotation.z);
58             recalculate = true;
59             updateRot = false;
60         }	
61         if (updateScale) {
62             scaleMatrix = mat4.identity.scale(scale.x, scale.y, scale.z);
63             recalculate = true;
64             updateScale = false;
65         }
66         if (updatePos) {
67             translateMatrix = mat4.identity.translation(position.x, position.y, position.z);
68             recalculate = true;
69             updatePos = false;
70         }
71         if (recalculate) {
72             matrix = translateMatrix*rotateMatrix*scaleMatrix;
73             updateInvert = true;
74         }
75         return matrix;
76     }
77 
78     mat4 InvertedMatrix()() {
79         if (updateInvert) {
80             invertMatrix = Matrix().inverse;
81         }
82         updateInvert = false;
83         return invertMatrix;
84     }
85 
86     mat4 Matrix2() {	
87         auto r = mat4.identity.rotatex(rotation.x).rotatey(rotation.y).rotatez(rotation.z);
88         auto s = mat4.identity.scale(scale.x, scale.y, scale.z);
89         auto t =  mat4.identity.translation(position.x, position.y, position.z);
90         return t*r*s;
91     }
92     
93 
94 }