-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathRTOTransform.cs
112 lines (94 loc) · 3.06 KB
/
RTOTransform.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
//Copyright SimBlocks LLC 2016-2022
//https://www.simblocks.io/
//The source code in this file is licensed under the MIT License. See the LICENSE text file for full terms.
using System;
using sbio.Core.Math;
using UnityEngine;
using sbio.owsdk.Unity.Extensions;
namespace sbio.owsdk.Unity
{
public class RTOTransform : IDisposable
{
public Transform Transform
{
get { return m_Transform; }
}
public Vec3LeftHandedGeocentric GlobalPosition
{
get { return m_GlobalPosition; }
set
{
m_GlobalPosition = value;
m_Transform.position = (value - m_LastWorldOrigin).ToVector3();
}
}
public Vector3 GlobalPositionf
{
get { return GlobalPosition.ToVector3(); }
set { GlobalPosition = value.ToVec3LeftHandedGeocentric(); }
}
public Vec3LeftHandedGeocentric RTOPosition
{
get { return (GlobalPosition - m_LastWorldOrigin); }
set
{
var diff = value - RTOPosition;
GlobalPosition += diff;
}
}
public Vector3 RTOPositionf
{
get { return (GlobalPosition - m_LastWorldOrigin).ToVector3(); }
set { RTOPosition = value.ToVec3LeftHandedGeocentric(); }
}
public void Dispose()
{
if (m_Disposed)
{
throw new ObjectDisposedException(ToString());
}
m_Disposed = true;
m_RTOContext.WorldOriginChanged -= UpdateWorldOrigin;
UpdateWorldOrigin(Vec3LeftHandedGeocentric.Zero);
}
/// <summary>
/// Construct an RTOTransform for a transform
/// </summary>
/// <param name="rtoContext">The RTO Context</param>
/// <param name="transform">The transform to keep updated</param>
public RTOTransform(IRTOContext rtoContext, Transform transform)
: this(rtoContext, transform, transform.position.ToVec3LeftHandedGeocentric())
{
}
/// <summary>
/// Construct an RTOTransform for a transform that is at the given position
/// </summary>
/// <param name="rtoContext"></param>
/// <param name="transform">The transform to keep updated</param>
/// <param name="position"></param>
public RTOTransform(IRTOContext rtoContext, Transform transform, Vec3LeftHandedGeocentric position)
{
m_RTOContext = rtoContext;
m_Transform = transform;
m_GlobalPosition = position;
UpdateWorldOrigin(rtoContext.WorldOrigin);
m_RTOContext.WorldOriginChanged += UpdateWorldOrigin;
}
private void UpdateWorldOrigin(Vec3LeftHandedGeocentric newWorldOrigin)
{
if (m_Transform != null)
{
m_Transform.position = (GlobalPosition - newWorldOrigin).ToVector3();
m_LastWorldOrigin = newWorldOrigin;
}
}
private readonly IRTOContext m_RTOContext;
private readonly Transform m_Transform;
private bool m_Disposed;
private Vec3LeftHandedGeocentric m_GlobalPosition;
private Vec3LeftHandedGeocentric m_LastWorldOrigin;
}
}
//Copyright SimBlocks LLC 2016-2022
//https://www.simblocks.io/
//The source code in this file is licensed under the MIT License. See the LICENSE text file for full terms.