AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
UNetSharedHologram.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 
6 using UnityEngine;
7 using UnityEngine.Networking;
8 
9 namespace HoloToolkit.Unity.SharingWithUNET
10 {
11  public class UNetSharedHologram : NetworkBehaviour, IInputClickHandler
12  {
16  [SyncVar(hook = "xformchange")]
17  private Vector3 localPosition;
18 
19  private void xformchange(Vector3 update)
20  {
21  Debug.Log(localPosition + " xform change " + update);
22  localPosition = update;
23  }
24 
25  // <summary>
28  [SyncVar]
29  private Quaternion localRotation;
30 
36  [Command]
37  public void CmdTransform(Vector3 postion, Quaternion rotation)
38  {
39  if (!isLocalPlayer)
40  {
41  localPosition = postion;
42  localRotation = rotation;
43  }
44  }
45 
46  private bool Moving;
47  private int layerMask;
48  private InputManager inputManager;
49  public Vector3 movementOffset = Vector3.zero;
50  private bool isOpaque = false;
51 
52  // Use this for initialization
53  private void Start()
54  {
55 #if UNITY_WSA
56 #if UNITY_2017_2_OR_NEWER
57  isOpaque = UnityEngine.XR.WSA.HolographicSettings.IsDisplayOpaque;
58 #else
59  isOpaque = !UnityEngine.VR.VRDevice.isPresent;
60 #endif
61 #endif
62  transform.SetParent(SharedCollection.Instance.transform, true);
63  if (isServer)
64  {
65  localPosition = transform.localPosition;
66  localRotation = transform.localRotation;
67  }
68 
69  layerMask = SpatialMappingManager.Instance.LayerMask;
70  inputManager = InputManager.Instance;
71 
72  }
73 
74  // Update is called once per frame
75  private void Update()
76  {
77 
78  if (Moving)
79  {
80  transform.position = Vector3.Lerp(transform.position, ProposeTransformPosition(), 0.2f);
81  }
82  else
83  {
84 
85  transform.localPosition = localPosition;
86  transform.localRotation = localRotation;
87  }
88  }
89 
90  private Vector3 ProposeTransformPosition()
91  {
92  // Put the model 3m in front of the user.
93  Vector3 retval = Camera.main.transform.position + Camera.main.transform.forward * 3 + movementOffset;
94  RaycastHit hitInfo;
95  if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hitInfo, 5.0f, layerMask))
96  {
97  retval = hitInfo.point + movementOffset;
98  }
99  return retval;
100  }
101 
102  public void OnInputClicked(InputClickedEventData eventData)
103  {
104  if (isOpaque == false)
105  {
106  Moving = !Moving;
107  if (Moving)
108  {
109  inputManager.AddGlobalListener(gameObject);
110  if (SpatialMappingManager.Instance != null)
111  {
112  SpatialMappingManager.Instance.DrawVisualMeshes = true;
113  }
114  }
115  else
116  {
117  inputManager.RemoveGlobalListener(gameObject);
118  if (SpatialMappingManager.Instance != null)
119  {
120  SpatialMappingManager.Instance.DrawVisualMeshes = false;
121  }
122  // Depending on if you are host or client, either setting the SyncVar (host)
123  // or calling the Cmd (client) will update the other users in the session.
124  // So we have to do both.
125  localPosition = transform.localPosition;
126  localRotation = transform.localRotation;
127  if (PlayerController.Instance != null)
128  {
129  PlayerController.Instance.SendSharedTransform(gameObject, localPosition, localRotation);
130  }
131  }
132 
133  eventData.Use();
134  }
135  }
136  }
137 }
This script exists as a stub to allow other scripts to find the shared world anchor transform...
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
Controls player behavior (local and remote).
void CmdTransform(Vector3 postion, Quaternion rotation)
Sets the localPosition and localRotation on clients.
void SendSharedTransform(GameObject target, Vector3 pos, Quaternion rot)
For sending transforms for holograms which do not frequently change.
Interface to implement to react to simple click input.
static PlayerController Instance
Instance of the PlayerController that represents the local player.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
void RemoveGlobalListener(GameObject listener)
Removes a global listener.
void AddGlobalListener(GameObject listener)
Adds a global listener that will receive all input events, regardless of which other game objects mig...
The SpatialMappingManager class allows applications to use a SurfaceObserver or a stored Spatial Mapp...
Describes an input event that involves a tap.
void OnInputClicked(InputClickedEventData eventData)