Merge branch 'main' into handTracking

handTracking
Brook Bowers 2022-01-24 14:09:48 -05:00
commit 6204a248f9
11 changed files with 460 additions and 41 deletions

View File

@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VelNet;
@ -22,13 +20,15 @@ public class MouseDragger : MonoBehaviour
{
foreach (string draggableTag in draggableTags)
{
if (!hit.transform.CompareTag(draggableTag) && !hit.transform.parent?.CompareTag(draggableTag) == true) continue;
NetworkObject netObj = hit.transform.GetComponent<NetworkObject>();
netObj ??= hit.transform.GetComponentInParent<NetworkObject>();
if (netObj == null) break;
VelNetManager.TakeOwnership(netObj.networkId);
draggingObject = netObj;
break;
if (hit.transform.CompareTag(draggableTag) || hit.transform.parent == null || hit.transform.parent.CompareTag(draggableTag))
{
NetworkObject netObj = hit.transform.GetComponent<NetworkObject>();
netObj ??= hit.transform.GetComponentInParent<NetworkObject>();
if (netObj == null) break;
netObj.TakeOwnership();
draggingObject = netObj;
break;
}
}
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace VelNet
@ -36,19 +37,15 @@ namespace VelNet
{
foreach (KeyValuePair<string, NetworkObject> kvp in VelNetManager.instance.objects)
{
VelNetManager.TakeOwnership(kvp.Key);
kvp.Value.TakeOwnership();
}
}
if (Input.GetKeyDown(KeyCode.Backspace))
{
foreach (KeyValuePair<string, NetworkObject> kvp in VelNetManager.instance.objects)
foreach (KeyValuePair<string, NetworkObject> kvp in VelNetManager.instance.objects.Where(kvp => !kvp.Value.ownershipLocked))
{
// don't destroy player objects
if (!kvp.Value.ownershipLocked)
{
VelNetManager.NetworkDestroy(kvp.Key);
}
VelNetManager.NetworkDestroy(kvp.Key);
}
}
}

View File

@ -47,6 +47,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
ownershipLocked: 0
networkId:
sceneNetworkId: 0
prefabName:
isSceneObject: 0
syncedComponents:
@ -64,10 +65,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
networkObject: {fileID: 3951900052977689805}
serializationRateHz: 30
targetPosition: {x: 0, y: 0, z: 0}
targetRotation: {x: 0, y: 0, z: 0, w: 0}
smoothness: 0.1
serializationRateHz: 60
useLocalTransform: 0
--- !u!1 &8565720276181857625
GameObject:
m_ObjectHideFlags: 0

View File

@ -1954,6 +1954,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
ownershipLocked: 0
networkId:
sceneNetworkId: 2
prefabName:
isSceneObject: 1
syncedComponents:
@ -2252,6 +2253,71 @@ MonoBehaviour:
- {fileID: 9102273340480352682, guid: d4158ab9c4a204cdbba28d3273fc1fb3, type: 3}
sceneObjects: []
deletedSceneObjects: []
--- !u!1001 &1109094063
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: isSceneObject
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: sceneNetworkId
value: 3
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462453, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_Name
value: TestNetworkedGameObject (1)
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_RootOrder
value: 7
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.x
value: 1.721
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.y
value: 0.793
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
--- !u!1 &1154194181
GameObject:
m_ObjectHideFlags: 0
@ -2457,6 +2523,136 @@ MonoBehaviour:
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text:
--- !u!1001 &1240540017
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: isSceneObject
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: sceneNetworkId
value: 5
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462453, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_Name
value: TestNetworkedGameObject (3)
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_RootOrder
value: 9
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.x
value: -3.703
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.y
value: 3.063
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
--- !u!1001 &1266855292
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: isSceneObject
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: sceneNetworkId
value: 4
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462453, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_Name
value: TestNetworkedGameObject (2)
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_RootOrder
value: 8
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.x
value: -2.572
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.y
value: 0.1
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462455, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
--- !u!1 &1278634766
GameObject:
m_ObjectHideFlags: 0
@ -3961,6 +4157,10 @@ PrefabInstance:
propertyPath: isSceneObject
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: sceneNetworkId
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8565720275311462453, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
propertyPath: m_Name
value: TestNetworkedGameObject

View File

@ -25,6 +25,12 @@ namespace VelNet
/// </summary>
public string networkId;
/// <summary>
/// This is generated at editor time and used to generated the network id at runtime.
/// This is needed because finding all objects of type at runtime doesn't have a guaranteed order.
/// </summary>
public int sceneNetworkId;
/// <summary>
/// This may be empty if it's not a prefab (scene object)
/// </summary>
@ -118,6 +124,25 @@ namespace VelNet
}
}
// make the sceneNetworkId a new unique value
if (Application.isEditor && !Application.isPlaying && t.isSceneObject && t.sceneNetworkId == 0)
{
// find the first unused value
int[] used = FindObjectsOfType<NetworkObject>().Select(o => o.sceneNetworkId).ToArray();
int available = -1;
for (int i = 1; i <= used.Max()+1; i++)
{
if (!used.Contains(i))
{
available = i;
break;
}
}
t.sceneNetworkId = available;
PrefabUtility.RecordPrefabInstancePropertyModifications(t);
}
EditorGUILayout.Space();
DrawDefaultInspector();

View File

@ -1,4 +1,7 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace VelNet
@ -34,5 +37,29 @@ namespace VelNet
reader.ReadSingle()
);
}
/// <summary>
/// Compresses the list of bools into bytes using a bitmask
/// </summary>
public static byte[] GetBitmasks(this IEnumerable<bool> bools)
{
List<bool> values = bools.ToList();
List<byte> bytes = new List<byte>();
for (int b = 0; b < Mathf.Ceil(values.Count / 8f); b++)
{
byte currentByte = 0;
for (int bit = 0; bit < 8; bit++)
{
if (values.Count > b * 8 + bit)
{
currentByte |= (byte)((values[b * 8 + bit] ? 1 : 0) << bit);
}
}
bytes.Add(currentByte);
}
return bytes.ToArray();
}
}
}

View File

@ -0,0 +1,143 @@
using System.IO;
using UnityEngine;
namespace VelNet
{
/// <summary>
/// A simple class that will sync the position and rotation of a network object with a rigidbody
/// </summary>
[AddComponentMenu("VelNet/VelNet Sync Rigidbody")]
[RequireComponent(typeof(Rigidbody))]
public class SyncRigidbody : NetworkSerializedObjectStream
{
public bool useLocalTransform;
[Tooltip("0 to disable.")]
public float teleportDistance;
[Tooltip("0 to disable.")]
public float teleportAngle;
public bool syncKinematic;
public bool syncGravity;
public bool syncVelocity;
public bool syncAngularVelocity;
private Vector3 targetPosition;
private Quaternion targetRotation;
private float distanceAtReceiveTime;
private float angleAtReceiveTime;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
if (useLocalTransform)
{
targetPosition = transform.localPosition;
targetRotation = transform.localRotation;
}
else
{
targetPosition = transform.position;
targetRotation = transform.rotation;
}
}
/// <summary>
/// This gets called at serializationRateHz when the object is locally owned
/// </summary>
protected override void SendState(BinaryWriter writer)
{
if (useLocalTransform)
{
writer.Write(transform.localPosition);
writer.Write(transform.localRotation);
}
else
{
writer.Write(transform.position);
writer.Write(transform.rotation);
}
// writer.Write((new bool[] {rb.isKinematic, rb.useGravity}).GetBitmasks());
if (syncKinematic) writer.Write(rb.isKinematic);
if (syncGravity) writer.Write(rb.useGravity);
if (syncVelocity) writer.Write(rb.velocity);
if (syncAngularVelocity) writer.Write(rb.angularVelocity);
}
/// <summary>
/// This gets called whenever a message about the state of this object is received.
/// Usually at serializationRateHz.
/// </summary>
protected override void ReceiveState(BinaryReader reader)
{
targetPosition = reader.ReadVector3();
targetRotation = reader.ReadQuaternion();
if (syncKinematic) rb.isKinematic = reader.ReadBoolean();
if (syncGravity) rb.useGravity = reader.ReadBoolean();
if (syncVelocity) rb.velocity = reader.ReadVector3();
if (syncAngularVelocity) rb.angularVelocity = reader.ReadVector3();
// record the distance from the target for interpolation
if (useLocalTransform)
{
distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.localPosition);
angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.localRotation);
if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime)
{
transform.localPosition = targetPosition;
}
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{
transform.localRotation = targetRotation;
}
}
else
{
distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.position);
angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.rotation);
if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime)
{
transform.position = targetPosition;
}
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{
transform.rotation = targetRotation;
}
}
}
private void Update()
{
if (IsMine) return;
if (useLocalTransform)
{
transform.localPosition = Vector3.MoveTowards(
transform.localPosition,
targetPosition,
Time.deltaTime * distanceAtReceiveTime * serializationRateHz
);
transform.localRotation = Quaternion.RotateTowards(
transform.localRotation,
targetRotation,
Time.deltaTime * angleAtReceiveTime * serializationRateHz
);
}
else
{
transform.position = Vector3.MoveTowards(
transform.position,
targetPosition,
Time.deltaTime * distanceAtReceiveTime * serializationRateHz
);
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRotation,
Time.deltaTime * angleAtReceiveTime * serializationRateHz
);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 951f5c5e2245481d8f969b94f998c78b
timeCreated: 1642738174

View File

@ -1,16 +1,19 @@
using System.IO;
using UnityEngine;
namespace VelNet
{
/// <summary>
/// A simple class that will sync the position and rotation of a network object
/// </summary>
[AddComponentMenu("VelNet/VelNet Sync Transform")]
public class SyncTransform : NetworkSerializedObject
public class SyncTransform : NetworkSerializedObjectStream
{
public bool useLocalTransform;
[Tooltip("0 to disable.")]
public float teleportDistance;
[Tooltip("0 to disable.")]
public float teleportAngle;
private Vector3 targetPosition;
private Quaternion targetRotation;
@ -34,28 +37,18 @@ namespace VelNet
/// <summary>
/// This gets called at serializationRateHz when the object is locally owned
/// </summary>
/// <returns>The state of this object to send across the network</returns>
protected override byte[] SendState()
protected override void SendState(BinaryWriter writer)
{
using MemoryStream mem = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(mem);
writer.Write(transform.localPosition);
writer.Write(transform.localRotation);
return mem.ToArray();
}
/// <summary>
/// This gets called whenever a message about the state of this object is received.
/// Usually at serializationRateHz.
/// </summary>
/// <param name="message">The network state of this object</param>
protected override void ReceiveState(byte[] message)
protected override void ReceiveState(BinaryReader reader)
{
using MemoryStream mem = new MemoryStream(message);
using BinaryReader reader = new BinaryReader(mem);
targetPosition = reader.ReadVector3();
targetRotation = reader.ReadQuaternion();
@ -64,11 +57,27 @@ namespace VelNet
{
distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.localPosition);
angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.localRotation);
if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime)
{
transform.localPosition = targetPosition;
}
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{
transform.localRotation = targetRotation;
}
}
else
{
distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.position);
angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.rotation);
if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime)
{
transform.position = targetPosition;
}
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{
transform.rotation = targetRotation;
}
}
}

View File

@ -316,7 +316,11 @@ namespace VelNet
for (int i = 0; i < sceneObjects.Length; i++)
{
sceneObjects[i].networkId = -1 + "-" + i;
if (sceneObjects[i].sceneNetworkId == 0)
{
Debug.LogError("Scene Network ID is 0. Make sure to assign one first.", sceneObjects[i]);
}
sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId;
sceneObjects[i].owner = masterPlayer;
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
objects.Add(sceneObjects[i].networkId, sceneObjects[i]);
@ -727,13 +731,25 @@ namespace VelNet
public static bool TakeOwnership(string networkId)
{
// local player must exist
if (LocalPlayer == null) return false;
if (LocalPlayer == null)
{
Debug.LogError("Can't take ownership. No local player.");
return false;
}
// obj must exist
if (!instance.objects.ContainsKey(networkId)) return false;
if (!instance.objects.ContainsKey(networkId))
{
Debug.LogError("Can't take ownership. Object with that network id doesn't exist.");
return false;
}
// if the ownership is locked, fail
if (instance.objects[networkId].ownershipLocked) return false;
if (instance.objects[networkId].ownershipLocked)
{
Debug.LogError("Can't take ownership. Ownership for this object is locked.");
return false;
}
// immediately successful
instance.objects[networkId].owner = LocalPlayer;

View File

@ -1,7 +1,7 @@
{
"name": "edu.uga.engr.vel.velnet",
"displayName": "VelNet",
"version": "1.0.6",
"version": "1.0.7",
"unity": "2019.1",
"description": "A custom networking library for Unity.",
"keywords": [