forked from icosa-foundation/open-brush
/
TransformApiWrapper.cs
220 lines (184 loc) · 9.89 KB
/
TransformApiWrapper.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using MoonSharp.Interpreter;
using UnityEngine;
namespace TiltBrush
{
[LuaDocsDescription("Represents a position, rotation and scale in one object")]
[MoonSharpUserData]
public class TransformApiWrapper
{
public TrTransform _TrTransform;
public TransformApiWrapper(Vector3 translation, Quaternion rotation, float scale = 1)
{
_TrTransform = TrTransform.TRS(translation, rotation, scale);
}
public TransformApiWrapper(Vector3 translation, float scale = 1)
{
_TrTransform = TrTransform.TRS(translation, Quaternion.identity, scale);
}
[LuaDocsDescription("The inverse of this transform")]
public TrTransform inverse => _TrTransform.inverse;
[LuaDocsDescription("A translation of 1 in the y axis")]
public Vector3 up => _TrTransform.up;
[LuaDocsDescription("A translation of -1 in the y axis")]
public Vector3 down => -_TrTransform.up;
[LuaDocsDescription("A translation of 1 in the x axis")]
public Vector3 right => _TrTransform.right;
[LuaDocsDescription("A translation of -1 in the x axis")]
public Vector3 left => -_TrTransform.right;
[LuaDocsDescription("A translation of 1 in the z axis")]
public Vector3 forward => _TrTransform.forward;
[LuaDocsDescription("A translation of -1 in the z axis")]
public Vector3 back => -_TrTransform.forward;
[LuaDocsDescription("Applies another transform to this transform")]
[LuaDocsExample("newTransform = myTransform:TransformBy(myOtherTransform)")]
[LuaDocsParameter("transform", "The transform to apply")]
public TrTransform TransformBy(TrTransform transform) => _TrTransform * transform;
[LuaDocsDescription("Applies a translation to this transform")]
[LuaDocsExample("newTransform = myTransform:TranslateBy(Vector3(1, 2, 3))")]
[LuaDocsParameter("translation", "The translation to apply")]
public TrTransform TranslateBy(Vector3 translation) => _TrTransform * TrTransform.T(translation);
[LuaDocsDescription("Applies a rotation to this transform")]
[LuaDocsExample("newTransform = myTransform:RotateBy(Rotation.New(0, 45, 0))")]
[LuaDocsParameter("rotation", "The rotation to apply")]
public TrTransform RotateBy(Quaternion rotation) => _TrTransform * TrTransform.R(rotation);
[LuaDocsDescription("Applies a scale to this transform")]
[LuaDocsExample("newTransform = myTransform:ScaleBy(2)")]
[LuaDocsParameter("scale", "The scale value to apply")]
public TrTransform ScaleBy(float scale) => _TrTransform * TrTransform.S(scale);
public TransformApiWrapper(TrTransform tr)
{
_TrTransform = tr;
}
[LuaDocsDescription("Creates a new translation, rotation and scale transform")]
[LuaDocsExample("myTransform = Transform:New(Vector3(1, 2, 3), Rotation.identity, 2)")]
[LuaDocsParameter("translation", "The translation amount")]
[LuaDocsParameter("rotation", "The rotation amount")]
[LuaDocsParameter("scale", "The scale amount")]
public static TransformApiWrapper New(Vector3 translation, Quaternion rotation, float scale)
{
var instance = new TransformApiWrapper(translation, rotation, scale);
return instance;
}
[LuaDocsDescription("Creates a new translation and rotation transform")]
[LuaDocsExample("myTransform = Transform:New(Vector3(1, 2, 3), Rotation.identity)")]
[LuaDocsParameter("translation", "The translation amount")]
[LuaDocsParameter("rotation", "The rotation amount")]
public static TransformApiWrapper New(Vector3 translation, Quaternion rotation)
{
var instance = new TransformApiWrapper(translation, rotation);
return instance;
}
[LuaDocsDescription("Creates a new translation transform")]
[LuaDocsExample("myTransform = Transform:New(Vector3(1, 2, 3))")]
[LuaDocsParameter("translation", "The translation amount")]
public static TransformApiWrapper New(Vector3 translation)
{
var instance = new TransformApiWrapper(translation, Quaternion.identity);
return instance;
}
[LuaDocsDescription("Creates a new translation and scale transform")]
[LuaDocsExample("myTransform = Transform:New(Vector3(1, 2, 3), 2)")]
[LuaDocsParameter("translation", "The translation amount")]
[LuaDocsParameter("scale", "The scale amount")]
public static TransformApiWrapper New(Vector3 translation, float scale)
{
var instance = new TransformApiWrapper(translation, Quaternion.identity, scale);
return instance;
}
[LuaDocsDescription("Creates a new translation transform")]
[LuaDocsExample("myTransform = Transform:Position(1, 2, 3)")]
[LuaDocsParameter("x", "The x translation amount")]
[LuaDocsParameter("y", "The y translation amount")]
[LuaDocsParameter("z", "The z translation amount")]
public static TransformApiWrapper Position(float x, float y, float z)
{
var instance = new TransformApiWrapper(TrTransform.T(new Vector3(x, y, z)));
return instance;
}
[LuaDocsDescription("Creates a new translation transform")]
[LuaDocsExample("myTransform = Transform:Position(myVector3)")]
[LuaDocsParameter("position", "The Vector3 position")]
public static TransformApiWrapper Position(Vector3ApiWrapper position)
{
var instance = new TransformApiWrapper(TrTransform.T(position._Vector3));
return instance;
}
[LuaDocsDescription("Creates a new rotation transform")]
[LuaDocsExample("myTransform = Transform:Rotation(1, 2, 3)")]
[LuaDocsParameter("x", "The x rotation amount")]
[LuaDocsParameter("y", "The y rotation amount")]
[LuaDocsParameter("z", "The z rotation amount")]
public static TransformApiWrapper Rotation(float x, float y, float z)
{
var instance = new TransformApiWrapper(TrTransform.R(Quaternion.Euler(x, y, z)));
return instance;
}
[LuaDocsDescription("Creates a new rotation transform")]
[LuaDocsExample("myTransform = Transform:Rotation(myRotation)")]
[LuaDocsParameter("rotation", "The rotation")]
public static TransformApiWrapper Rotation(RotationApiWrapper rotation)
{
var instance = new TransformApiWrapper(TrTransform.R(rotation._Quaternion));
return instance;
}
[LuaDocsDescription("Creates a new scale transform")]
[LuaDocsExample("myTransform = Transform:Scale(2)")]
[LuaDocsParameter("amount", "The scale amount")]
public static TransformApiWrapper Scale(float amount)
{
var instance = new TransformApiWrapper(TrTransform.S(amount));
return instance;
}
public override string ToString()
{
return $"TrTransform({_TrTransform.translation}, {_TrTransform.rotation}, {_TrTransform.scale})";
}
[LuaDocsDescription("Get or set the position of this transform")]
public Vector3ApiWrapper position
{
get => new Vector3ApiWrapper(_TrTransform.translation);
set => _TrTransform.translation = value._Vector3;
}
[LuaDocsDescription("Get or set the rotation of this transform")]
public RotationApiWrapper rotation
{
get => new RotationApiWrapper(_TrTransform.rotation);
set => _TrTransform.rotation = value._Quaternion;
}
[LuaDocsDescription("Get or set the scale of this transform")]
public float scale
{
get => _TrTransform.scale;
set => _TrTransform.scale = value;
}
[LuaDocsDescription("A transform that does nothing. No translation, rotation or scaling")]
public static TrTransform identity => TrTransform.identity;
// Operators
public static TrTransform operator *(TransformApiWrapper a, TransformApiWrapper b) => a._TrTransform * b._TrTransform;
[LuaDocsDescription(@"Combines another transform with this one (Does the same as ""TransformBy"")")]
[LuaDocsExample("newTransform = myTransform:Multiply(Transform.up)")]
[LuaDocsParameter("other", "The Transform to apply to this one")]
public TrTransform Multiply(TrTransform other) => _TrTransform * other;
[LuaDocsDescription("Is this transform equal to another?")]
[LuaDocsExample(@"if myTransform:Equals(Transform.up) then print(""Equal to Transform.up"")")]
[LuaDocsParameter("other", "The Transform to compare to this one")]
public bool Equals(TransformApiWrapper other) => Equals(other._TrTransform);
public override bool Equals(System.Object obj)
{
var other = obj as TransformApiWrapper;
return other != null && _TrTransform == other._TrTransform;
}
public override int GetHashCode() => 0; // Always return 0. Lookups will have to use Equals to compare
[LuaDocsDescription("Interpolates between two transforms")]
[LuaDocsExample("newTransform = Transform:Lerp(transformA, transformB, 0.25)")]
[LuaDocsParameter("a", "The first transform")]
[LuaDocsParameter("b", "The second transform")]
[LuaDocsParameter("t", "The value between 0 and 1 that controls how far between a and b the new transform is")]
[LuaDocsReturnValue("A transform that blends between a and b based on the value of t")]
public static TrTransform Lerp(TrTransform a, TrTransform b, float t) => TrTransform.TRS(
Vector3.Lerp(a.translation, b.translation, t),
Quaternion.Slerp(a.rotation, b.rotation, t),
Mathf.Lerp(a.scale, b.scale, t)
);
}
}