AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
TransformSynchronizer.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using HoloToolkit.Unity;
7 
8 namespace HoloToolkit.Sharing
9 {
13  public class TransformSynchronizer : MonoBehaviour
14  {
18 
19  private SyncTransform transformDataModel;
20 
24  public SyncTransform TransformDataModel
25  {
26  private get { return transformDataModel; }
27  set
28  {
29  if (transformDataModel != value)
30  {
31  if (transformDataModel != null)
32  {
33  transformDataModel.PositionChanged -= OnPositionChanged;
34  transformDataModel.RotationChanged -= OnRotationChanged;
35  transformDataModel.ScaleChanged -= OnScaleChanged;
36  }
37 
38  transformDataModel = value;
39 
40  if (transformDataModel != null)
41  {
42  // Set the position, rotation and scale to what they should be
43  transform.localPosition = transformDataModel.Position.Value;
44  transform.localRotation = transformDataModel.Rotation.Value;
45  transform.localScale = transformDataModel.Scale.Value;
46 
47  // Initialize
48  Initialize();
49 
50  // Register to changes
51  transformDataModel.PositionChanged += OnPositionChanged;
52  transformDataModel.RotationChanged += OnRotationChanged;
53  transformDataModel.ScaleChanged += OnScaleChanged;
54  }
55  }
56  }
57  }
58 
59  private void Start()
60  {
61  Initialize();
62  }
63 
64  private void Initialize()
65  {
66  if (Position == null)
67  {
68  Position = new Vector3Interpolated(transform.localPosition);
69  }
70  if (Rotation == null)
71  {
72  Rotation = new QuaternionInterpolated(transform.localRotation);
73  }
74  if (Scale == null)
75  {
76  Scale = new Vector3Interpolated(transform.localScale);
77  }
78  }
79 
80  private void Update()
81  {
82  // Apply transform changes, if any
83  if (Position.HasUpdate())
84  {
85  transform.localPosition = Position.GetUpdate(Time.deltaTime);
86  }
87  if (Rotation.HasUpdate())
88  {
89  transform.localRotation = Rotation.GetUpdate(Time.deltaTime);
90  }
91  if (Scale.HasUpdate())
92  {
93  transform.localScale = Scale.GetUpdate(Time.deltaTime);
94  }
95  }
96 
97  private void LateUpdate()
98  {
99  // Determine if the transform has changed locally, in which case we need to update the data model
100  if (transform.localPosition != Position.Value ||
101  Quaternion.Angle(transform.localRotation, Rotation.Value) > 0.2f ||
102  transform.localScale != Scale.Value)
103  {
104  transformDataModel.Position.Value = transform.localPosition;
105  transformDataModel.Rotation.Value = transform.localRotation;
106  transformDataModel.Scale.Value = transform.localScale;
107 
108  // The object was moved locally, so reset the target positions to the current position
109  Position.Reset(transform.localPosition);
110  Rotation.Reset(transform.localRotation);
111  Scale.Reset(transform.localScale);
112  }
113  }
114 
115  private void OnDestroy()
116  {
117  if (transformDataModel != null)
118  {
119  transformDataModel.PositionChanged -= OnPositionChanged;
120  transformDataModel.RotationChanged -= OnRotationChanged;
121  transformDataModel.ScaleChanged -= OnScaleChanged;
122  }
123  }
124 
125  private void OnPositionChanged()
126  {
127  Position.SetTarget(transformDataModel.Position.Value);
128  }
129 
130  private void OnRotationChanged()
131  {
132  Rotation.SetTarget(transformDataModel.Rotation.Value);
133  }
134 
135  private void OnScaleChanged()
136  {
137  Scale.SetTarget(transformDataModel.Scale.Value);
138  }
139  }
140 }
Synchronizer to update and broadcast a transform object through our data model.
bool HasUpdate()
Returns whether there are further updates required to get the target value.
void Reset(Quaternion value)
Resets property to zero interpolation and set value.
Quaternion Value
Current value of the property.
This class implements the Transform object primitive for the syncing system. It does the heavy liftin...
bool HasUpdate()
Returns whether there are further updates required to get the target value.
Quaternion GetUpdate(float deltaTime)
Performs and fets the updated value.
void SetTarget(Vector3 targetValue)
Set a target for property to interpolate to.
Vector3 GetUpdate(float deltaTime)
Performs and gets the updated value.
Vector3 Value
Current value of the property.
Class to encapsulate an interpolating Quaternion property. TODO: Remove if redundant to InterpolatedQ...
Class to encapsulate an interpolating Vector3 property. TODO: Remove if redundant to InterpolatedVect...
void SetTarget(Quaternion targetValue)
Set a target for property to interpolate to.
void Reset(Vector3 value)
Resets property to zero interpolation and set value.