1 using System.Collections;
2 using System.Collections.Generic;
15 protected static Dictionary<XRNode, AVR_GenericXRDevice> nodeDevices =
new Dictionary<XRNode, AVR_GenericXRDevice>();
23 nodeDevices.TryGetValue(node, out var val);
37 public InputDevice inputDevice => _inputDevice.isValid ? _inputDevice : _inputDevice = InputDevices.GetDeviceAtXRNode(controllerNode);
46 public bool tracking =
true;
55 OnBeforeRenderAndUpdate,
70 public bool smoothing =
false;
76 public float smoothingFidelity = 0.3f;
79 private Vector3 prevPos = Vector3.zero;
80 private Quaternion prevRot = Quaternion.identity;
85 nodeDevices[controllerNode] =
this;
94 if (inputDevice.TryGetFeatureValue(CommonUsages.trackingState, out var trackingState))
96 if ((trackingState & InputTrackingState.Position) != 0 && inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out var devicePosition))
100 transform.localPosition = Vector3.Lerp(transform.localPosition, devicePosition, Time.deltaTime * smoothingFidelity * 90.0f);
104 transform.localPosition = devicePosition;
108 if ((trackingState & InputTrackingState.Rotation) != 0 && inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out var deviceRotation))
112 transform.localRotation = Quaternion.Lerp(transform.localRotation, deviceRotation, Time.deltaTime * smoothingFidelity * 90.0f);
116 transform.localRotation = deviceRotation;
128 if (!IsOwner)
return;
143 if (!IsOwner)
return;
156 nodeDevices[controllerNode] =
this;
157 Application.onBeforeRender += OnBeforeRender;
163 if(nodeDevices.ContainsKey(controllerNode) && nodeDevices[controllerNode] ==
this) nodeDevices.Remove(controllerNode);
164 Application.onBeforeRender -= OnBeforeRender;
170 public bool synchronizeTransform =
false;
172 [Unity.Netcode.ServerRpc(RequireOwnership =
false)]
173 private void syncServerRpc(InternalState state)
175 m_ReplicatedState.Value = state;
180 if (!synchronizeTransform)
return;
183 InternalState state =
new InternalState();
184 state.FromReference(
this);
185 syncServerRpc(state);
189 m_ReplicatedState.Value.ApplyState(
this);
193 private readonly Unity.Netcode.NetworkVariable<InternalState> m_ReplicatedState =
new Unity.Netcode.NetworkVariable<InternalState>(Unity.Netcode.NetworkVariableReadPermission.Everyone,
new InternalState());
195 private struct InternalState : IInternalState<AVR_GenericXRDevice>
197 public Vector3 trackingPos;
198 public Quaternion trackingRot;
200 public void FromReference(AVR_GenericXRDevice reference)
202 trackingPos = reference.transform.position;
203 trackingRot = reference.transform.rotation;
206 public void ApplyState(AVR_GenericXRDevice reference)
208 reference.transform.position = trackingPos;
209 reference.transform.rotation = trackingRot;
212 public void NetworkSerialize<T>(Unity.Netcode.BufferSerializer<T> serializer) where T : Unity.Netcode.IReaderWriter
214 serializer.SerializeValue(ref trackingPos);
215 serializer.SerializeValue(ref trackingRot);
Makes a property of an object only show in the Network-behaviour window. Also works for private/prote...
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
static AVR_GenericXRDevice getDeviceAtNode(XRNode node)
Returns the last AVR_GenericXRDevice enabled that has a given controllerNode value.
override void OnDisable()
XRNode controllerNode
Which XRNode this object represents.
Allows for simple hiding of properties in the UnityEditor depending on certain conditions. For instance, in the following example the "type" field will only be displayed in the inspector if the "tracking" field is set to true:
Represents some spatially tracked XR device. Provides functionality for spacial tracking. Can be used to represent hardware trackers, game controllers or other references.
void UpdateTracking()
Updates the controller position and rotation.
TrackingUpdateType
Defines when positional tracking should take place. Recommended setting is OnBeforeRenderAndUpdate ...
Base class for all arc-vr components. A component is typically a Monobehaviour that represents a virt...