Tanoda
pb_Transform.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using System.Runtime.Serialization;
5
6namespace GILES
7{
8 [System.Serializable]
9 public class pb_Transform : System.IEquatable<pb_Transform>, ISerializable
10 {
11 // If the matrix needs rebuilt, this will be true. Used to delay expensive
12 // matrix construction til necessary (since t/r/s can change a lot before a
13 // matrix is needed).
14 private bool dirty = true;
15
16 [SerializeField] private Vector3 _position;
17 [SerializeField] private Quaternion _rotation;
18 [SerializeField] private Vector3 _scale;
19 [SerializeField] private Vector3 _offset;
20 [SerializeField] private Vector3 _rotoffset;
21 [SerializeField] private Vector3 _leftoffset;
22 [SerializeField] private Vector3 _leftrotoffset;
23 [SerializeField] private bool _static;
24 [SerializeField] private bool _restArea;
25 [SerializeField] private bool _twoHand;
26 [SerializeField] private bool _tool;
27 [SerializeField] private bool _enabled;
28
29 private Matrix4x4 matrix;
30
31 public Vector3 position { get { return _position; } set { dirty = true; _position = value; } }
32 public Quaternion rotation { get { return _rotation; } set { dirty = true; _rotation = value; } }
33 public Vector3 scale { get { return _scale; } set { dirty = true; _scale = value; } }
34 public Vector3 offset { get { return _offset; } set { dirty = true; _offset = value; } }
35 public Vector3 leftoffset { get { return _leftoffset; } set { dirty = true; _leftoffset = value; } }
36 public Vector3 rotoffset { get { return _rotoffset; } set { dirty = true; _rotoffset = value; } }
37 public Vector3 leftrotoffset { get { return _leftrotoffset; } set { dirty = true; _leftrotoffset = value; } }
38 public bool isRestArea { get { return _restArea; } set { dirty = true; _restArea = value; } }
39 //public bool isTwoHandGrab { get { return _twoHand; } set { dirty = true; _twoHand = value; } }
40 public bool isStatic { get { return _static; } set { dirty = true; _static = value; } }
41 public bool isTool { get { return _tool; } set { dirty = true; _tool = value; } }
42 public bool isEnabled { get { return _enabled; } set { dirty = true; _enabled = value; } }
43
44 public static readonly pb_Transform identity = new pb_Transform(Vector3.zero, Quaternion.identity, Vector3.one);
45
46 public pb_Transform()
47 {
48 this.position = Vector3.zero;
49 this.rotation = Quaternion.identity;
50 this.scale = Vector3.one;
51 this.offset = Vector3.zero;
52 this.leftoffset = Vector3.zero;
53 this.rotoffset = Vector3.zero;
54 this.leftrotoffset = Vector3.zero;
55 this.matrix = Matrix4x4.identity;
56 this.isStatic = true;
57 this.isRestArea = false;
58 //this.isTwoHandGrab = false;
59 this.isTool = false;
60 this.isEnabled = true;
61 this.dirty = false;
62 }
63
64 public pb_Transform(Vector3 position, Quaternion rotation, Vector3 scale)
65 {
66 this.position = position;
67 this.rotation = rotation;
68 this.scale = scale;
69 this.offset = Vector3.zero;
70 this.rotoffset = Vector3.zero;
71 this.isStatic = true;
72 this.isRestArea = false;
73 //this.isTwoHandGrab = false;
74
75 this.matrix = Matrix4x4.TRS(position, rotation, scale);
76 this.dirty = false;
77 }
78
79 public pb_Transform(Transform transform)
80 {
81 this.position = transform.position;
82 this.rotation = transform.localRotation;
83 this.scale = transform.localScale;
84 this.offset = Vector3.zero;
85 this.leftoffset = Vector3.zero;
86 this.rotoffset = Vector3.zero;
87 this.leftrotoffset = Vector3.zero;
88 this.isStatic = true;
89 this.isRestArea = false;
90 //this.isTwoHandGrab = false;
91 this.isEnabled = transform.gameObject.activeSelf;
92
93 this.matrix = Matrix4x4.TRS(position, rotation, scale);
94 this.dirty = false;
95 }
96
97 public pb_Transform(pb_Transform transform)
98 {
99 this.position = transform.position;
100 this.rotation = transform.rotation;
101 this.scale = transform.scale;
102 this.offset = Vector3.zero;
103 this.leftoffset = Vector3.zero;
104 this.rotoffset = Vector3.zero;
105 this.leftrotoffset = Vector3.zero;
106 this.isStatic = true;
107 this.isRestArea = false;
108 //this.isTwoHandGrab = false;
109 this.isEnabled = transform.isEnabled;
110
111 this.matrix = Matrix4x4.TRS(position, rotation, scale);
112 this.dirty = false;
113 }
114
115 public void GetObjectData(SerializationInfo info, StreamingContext context)
116 {
117 info.AddValue("position", (Vector3)_position, typeof(Vector3));
118 info.AddValue("rotation", (Quaternion)_rotation, typeof(Quaternion));
119 info.AddValue("scale", (Vector3)_scale, typeof(Vector3));
120 info.AddValue("offset", (Vector3)_offset, typeof(Vector3));
121 info.AddValue("rotoffset", (Vector3)_rotoffset, typeof(Vector3));
122 info.AddValue("leftoffset", (Vector3)_leftoffset, typeof(Vector3));
123 info.AddValue("leftrotoffset", (Vector3)_leftrotoffset, typeof(Vector3));
124 info.AddValue("isStatic", (bool)_static, typeof(bool));
125 info.AddValue("isRestArea", (bool)_restArea, typeof(bool));
126 info.AddValue("isTwoHandGrab", (bool)_twoHand, typeof(bool));
127 info.AddValue("isTool", (bool)_tool, typeof(bool));
128 info.AddValue("isEnabled", (bool)_enabled, typeof(bool));
129 }
130
131 public pb_Transform(SerializationInfo info, StreamingContext context)
132 {
133 this._position = (Vector3) info.GetValue("position", typeof(Vector3));
134 this._rotation = (Quaternion) info.GetValue("rotation", typeof(Quaternion));
135 this._scale = (Vector3) info.GetValue("scale", typeof(Vector3));
136 try
137 {
138 this._static = info.GetBoolean("isStatic");
139 }
140 catch (Exception e)
141 {
142 // ignored
143 }
144 try
145 {
146 this._tool = info.GetBoolean("isTool");
147 }
148 catch (Exception e)
149 {
150 // ignored
151 }
152 try
153 {
154 this._enabled = info.GetBoolean("isEnabled");
155 }
156 catch (Exception e)
157 {
158 this._enabled = true;
159 }
160 try
161 {
162 this._restArea = info.GetBoolean("isRestArea");
163 }
164 catch (Exception e)
165 {
166 this._restArea = false;
167 }
168 try
169 {
170 this._twoHand = info.GetBoolean("isTwoHandGrab");
171 }
172 catch (Exception e)
173 {
174 this._twoHand = false;
175 }
176 try
177 {
178 this._offset = (Vector3) info.GetValue("offset", typeof(Vector3));
179 }
180 catch (Exception e)
181 {
182 this._offset = Vector3.zero;
183 }
184 try
185 {
186 this._rotoffset = (Vector3) info.GetValue("rotoffset", typeof(Vector3));
187 }
188 catch (Exception e)
189 {
190 this._rotoffset = Vector3.zero;
191 }
192 try
193 {
194 this._leftoffset = (Vector3) info.GetValue("leftoffset", typeof(Vector3));
195 }
196 catch (Exception e)
197 {
198 this._leftoffset = Vector3.zero;
199 }
200 try
201 {
202 this._leftrotoffset = (Vector3) info.GetValue("leftrotoffset", typeof(Vector3));
203 }
204 catch (Exception e)
205 {
206 this._leftrotoffset = Vector3.zero;
207 }
208 this.dirty = true;
209 }
210
211 public void SetTRS(Transform trs)
212 {
213 this.position = trs.position;
214 this.rotation = trs.localRotation;
215 this.scale = trs.localScale;
216 this.isEnabled = trs.gameObject.activeSelf;
217 this.dirty = true;
218 }
219
220 bool Approx(Vector3 lhs, Vector3 rhs)
221 {
222 return Mathf.Abs(lhs.x - rhs.x) < Mathf.Epsilon &&
223 Mathf.Abs(lhs.y - rhs.y) < Mathf.Epsilon &&
224 Mathf.Abs(lhs.z - rhs.z) < Mathf.Epsilon;
225 }
226
227 bool Approx(Quaternion lhs, Quaternion rhs)
228 {
229 return Mathf.Abs(lhs.x - rhs.x) < Mathf.Epsilon &&
230 Mathf.Abs(lhs.y - rhs.y) < Mathf.Epsilon &&
231 Mathf.Abs(lhs.z - rhs.z) < Mathf.Epsilon &&
232 Mathf.Abs(lhs.w - rhs.w) < Mathf.Epsilon;
233 }
234
235 public bool Equals(pb_Transform rhs)
236 {
237 return Approx(this.position, rhs.position) &&
238 Approx(this.rotation, rhs.rotation) &&
239 Approx(this.scale, rhs.scale);
240 }
241
242 public override bool Equals(System.Object rhs)
243 {
244 return rhs is pb_Transform && this.Equals( (pb_Transform) rhs );
245 }
246
247 public override int GetHashCode()
248 {
249 return position.GetHashCode() ^ rotation.GetHashCode() ^ scale.GetHashCode();
250 }
251
252 public Matrix4x4 GetMatrix()
253 {
254 if( !this.dirty )
255 {
256 return matrix;
257 }
258 else
259 {
260 this.dirty = false;
261 matrix = Matrix4x4.TRS(position, rotation, scale);
262 return matrix;
263 }
264 }
265
267 {
268 pb_Transform t = new pb_Transform();
269
270 t.position = lhs.position - rhs.position;
271 t.rotation = Quaternion.Inverse(rhs.rotation) * lhs.rotation;
272 t.scale = new Vector3( lhs.scale.x / rhs.scale.x,
273 lhs.scale.y / rhs.scale.y,
274 lhs.scale.z / rhs.scale.z);
275
276 return t;
277 }
278
280 {
281 pb_Transform t = new pb_Transform();
282
283 t.position = lhs.position + rhs.position;
284 t.rotation = lhs.rotation * rhs.rotation;
285 t.scale = new Vector3( lhs.scale.x * rhs.scale.x,
286 lhs.scale.y * rhs.scale.y,
287 lhs.scale.z * rhs.scale.z);
288
289 return t;
290 }
291
292 public static pb_Transform operator + (Transform lhs, pb_Transform rhs)
293 {
294 pb_Transform t = new pb_Transform();
295
296 t.position = lhs.position + rhs.position;
297 t.rotation = lhs.localRotation * rhs.rotation;
298 t.scale = new Vector3( lhs.localScale.x * rhs.scale.x,
299 lhs.localScale.y * rhs.scale.y,
300 lhs.localScale.z * rhs.scale.z);
301
302 return t;
303 }
304
305 public static bool operator == (pb_Transform lhs, pb_Transform rhs)
306 {
307 return System.Object.ReferenceEquals(lhs, rhs) || lhs.Equals(rhs);
308 }
309
310 public static bool operator != (pb_Transform lhs, pb_Transform rhs)
311 {
312 return !(lhs == rhs);
313 }
314
315 public Vector3 up { get { return rotation * Vector3.up; } }
316 public Vector3 forward { get { return rotation * Vector3.forward; } }
317 public Vector3 right { get { return rotation * Vector3.right; } }
318
319 public override string ToString()
320 {
321 return position.ToString("F2") + "\n" + rotation.ToString("F2") + "\n" + scale.ToString("F2");
322 }
323 }
324}
static pb_Transform operator+(pb_Transform lhs, pb_Transform rhs)
pb_Transform(Vector3 position, Quaternion rotation, Vector3 scale)
Definition: pb_Transform.cs:64
bool Equals(pb_Transform rhs)
void GetObjectData(SerializationInfo info, StreamingContext context)
Matrix4x4 GetMatrix()
Quaternion rotation
Definition: pb_Transform.cs:32
void SetTRS(Transform trs)
static readonly pb_Transform identity
Definition: pb_Transform.cs:44
static bool operator!=(pb_Transform lhs, pb_Transform rhs)
static bool operator==(pb_Transform lhs, pb_Transform rhs)
pb_Transform(Transform transform)
Definition: pb_Transform.cs:79
override bool Equals(System.Object rhs)
static pb_Transform operator-(pb_Transform lhs, pb_Transform rhs)
pb_Transform(pb_Transform transform)
Definition: pb_Transform.cs:97
pb_Transform(SerializationInfo info, StreamingContext context)
override string ToString()
override int GetHashCode()