forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformSynchronizer.cs
142 lines (125 loc) · 4.76 KB
/
TransformSynchronizer.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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
using UnityEngine;
using HoloToolkit.Unity;
using HoloToolkit.Sharing.SyncModel;
namespace HoloToolkit.Sharing
{
/// <summary>
/// Synchronizer to update and broadcast a transform object through our data model.
/// </summary>
public class TransformSynchronizer : MonoBehaviour
{
protected Vector3Interpolated Position;
protected QuaternionInterpolated Rotation;
protected Vector3Interpolated Scale;
private SyncTransform transformDataModel;
/// <summary>
/// Data model to which this transform synchronization is tied to.
/// </summary>
public SyncTransform TransformDataModel
{
private get { return transformDataModel; }
set
{
if (transformDataModel != value)
{
if (transformDataModel != null)
{
transformDataModel.PositionChanged -= OnPositionChanged;
transformDataModel.RotationChanged -= OnRotationChanged;
transformDataModel.ScaleChanged -= OnScaleChanged;
}
transformDataModel = value;
if (transformDataModel != null)
{
// Set the position, rotation and scale to what they should be
transform.localPosition = transformDataModel.Position.Value;
transform.localRotation = transformDataModel.Rotation.Value;
transform.localScale = transformDataModel.Scale.Value;
// Initialize
Initialize();
// Register to changes
transformDataModel.PositionChanged += OnPositionChanged;
transformDataModel.RotationChanged += OnRotationChanged;
transformDataModel.ScaleChanged += OnScaleChanged;
}
}
}
}
private void Start()
{
Initialize();
}
private void Initialize()
{
if (Position == null)
{
Position = new Vector3Interpolated(transform.localPosition);
}
if (Rotation == null)
{
Rotation = new QuaternionInterpolated(transform.localRotation);
}
if (Scale == null)
{
Scale = new Vector3Interpolated(transform.localScale);
}
}
private void Update()
{
// Apply transform changes, if any
if (Position.HasUpdate())
{
transform.localPosition = Position.GetUpdate(Time.deltaTime);
}
if (Rotation.HasUpdate())
{
transform.localRotation = Rotation.GetUpdate(Time.deltaTime);
}
if (Scale.HasUpdate())
{
transform.localScale = Scale.GetUpdate(Time.deltaTime);
}
}
private void LateUpdate()
{
// Determine if the transform has changed locally, in which case we need to update the data model
if (transform.localPosition != Position.Value ||
Quaternion.Angle(transform.localRotation, Rotation.Value) > 0.2f ||
transform.localScale != Scale.Value)
{
transformDataModel.Position.Value = transform.localPosition;
transformDataModel.Rotation.Value = transform.localRotation;
transformDataModel.Scale.Value = transform.localScale;
// The object was moved locally, so reset the target positions to the current position
Position.Reset(transform.localPosition);
Rotation.Reset(transform.localRotation);
Scale.Reset(transform.localScale);
}
}
private void OnDestroy()
{
if (transformDataModel != null)
{
transformDataModel.PositionChanged -= OnPositionChanged;
transformDataModel.RotationChanged -= OnRotationChanged;
transformDataModel.ScaleChanged -= OnScaleChanged;
}
}
private void OnPositionChanged()
{
Position.SetTarget(transformDataModel.Position.Value);
}
private void OnRotationChanged()
{
Rotation.SetTarget(transformDataModel.Rotation.Value);
}
private void OnScaleChanged()
{
Scale.SetTarget(transformDataModel.Scale.Value);
}
}
}