moved all dissonance refs out of base components. Dissonnance is now implemented in a specialized networkobject

handTracking
Kyle Johnsen 2022-01-04 23:39:57 -05:00
parent 1830235130
commit 6c6c7c4833
25 changed files with 363 additions and 325 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 21c2212b420fd48a1b3552af8d219f80
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,109 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Dissonance;
using Dissonance.Extensions;
using Dissonance.Networking;
using UnityEngine;
public class VelCommsNetwork : MonoBehaviour, ICommsNetwork
{
public ConnectionStatus Status
{
get
{
return manager.connected?ConnectionStatus.Connected:ConnectionStatus.Disconnected;
}
}
public NetworkMode Mode
{
get
{
return NetworkMode.Client;
}
}
public event Action<NetworkMode> ModeChanged;
public event Action<string, CodecSettings> PlayerJoined;
public event Action<string> PlayerLeft;
public event Action<VoicePacket> VoicePacketReceived;
public event Action<TextMessage> TextPacketReceived;
public event Action<string> PlayerStartedSpeaking;
public event Action<string> PlayerStoppedSpeaking;
public event Action<RoomEvent> PlayerEnteredRoom;
public event Action<RoomEvent> PlayerExitedRoom;
ConnectionStatus _status = ConnectionStatus.Disconnected;
CodecSettings initSettings;
public string dissonanceId;
public DissonanceComms comms;
public NetworkManager manager;
public Action<ArraySegment<byte>> voiceQueued = delegate { }; //listen to this if you want to send voice
public void Initialize(string playerName, Rooms rooms, PlayerChannels playerChannels, RoomChannels roomChannels, CodecSettings codecSettings)
{
dissonanceId = playerName;
initSettings = codecSettings;
comms.ResetMicrophoneCapture();
}
public void voiceReceived(string sender,byte[] data)
{
uint sequenceNumber = BitConverter.ToUInt32(data, 0);
VoicePacket vp = new VoicePacket(sender, ChannelPriority.Default, 1, true, new ArraySegment<byte>(data,4,data.Length-4), sequenceNumber);
VoicePacketReceived(vp);
}
public void SendText(string data, ChannelType recipientType, string recipientId)
{
Debug.Log("sending text");
}
public void SendVoice(ArraySegment<byte> data)
{
voiceQueued(data);
}
// Start is called before the first frame update
void Start()
{
_status = ConnectionStatus.Connected;
comms = GetComponent<DissonanceComms>();
}
public void playerJoined(string id)
{
Debug.Log("dissonance player joined");
PlayerJoined(id, initSettings);
RoomEvent re = new RoomEvent();
re.Joined = true;
re.Room = "Global";
re.PlayerName = id;
PlayerEnteredRoom(re);
}
public void playerLeft(string id)
{
RoomEvent re = new RoomEvent();
re.Joined = false;
re.Room = "Global";
re.PlayerName = id;
PlayerExitedRoom(re);
PlayerLeft(id);
}
public void playerStartedSpeaking(string id)
{
PlayerStartedSpeaking(id);
}
public void playerStoppedSpeaking(string id)
{
PlayerStoppedSpeaking(id);
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2fc687ece57d64c7082cd32261ac438b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,219 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Dissonance;
using System.Text;
public class PlayerController : NetworkObject, Dissonance.IDissonancePlayer
{
VelCommsNetwork comms;
bool isSpeaking = false;
uint lastAudioId = 0;
public string dissonanceID="";
//required by dissonance for spatial audio
public string PlayerId => dissonanceID;
public Vector3 Position => transform.position;
public Quaternion Rotation => transform.rotation;
public NetworkPlayerType Type => this.owner.isLocal ? NetworkPlayerType.Local : NetworkPlayerType.Remote;
public bool IsTracking => true;
public Vector3 targetPosition;
public Quaternion targetRotation;
public byte[] getSyncMessage()
{
float[] data = new float[7];
for (int i = 0; i < 3; i++)
{
data[i] = transform.position[i];
data[i + 3] = transform.rotation[i];
}
data[6] = transform.rotation[3];
byte[] toReturn = new byte[sizeof(float) * data.Length];
Buffer.BlockCopy(data, 0, toReturn, 0, toReturn.Length);
return toReturn;
}
public override void handleMessage(string identifier, byte[] message)
{
switch (identifier)
{
case "s": //sync message
float[] data = new float[7];
Buffer.BlockCopy(message, 0, data, 0, message.Length);
for (int i = 0; i < 3; i++)
{
targetPosition[i] = data[i];
targetRotation[i] = data[i + 3];
}
targetRotation[3] = data[6];
break;
case "a": //audio data
{
if (isSpeaking)
{
comms.voiceReceived(dissonanceID, message);
}
break;
}
case "d": //dissonance id (player joined)
{
if (dissonanceID == "") //I don't have this yet
{
dissonanceID = Encoding.UTF8.GetString(message);
//tell the comms network that this player joined the channel
comms.playerJoined(dissonanceID); //tell dissonance
comms.comms.TrackPlayerPosition(this); //tell dissonance to track the remote player
}
break;
}
case "x": //speaking state
{
if (message[0]==0)
{
comms.playerStoppedSpeaking(dissonanceID);
isSpeaking = false;
}
else
{
Debug.Log("here");
comms.playerStartedSpeaking(dissonanceID);
isSpeaking = true;
}
break;
}
}
}
// Start is called before the first frame update
void Start()
{
//handle dissonance stuff
comms = GameObject.FindObjectOfType<VelCommsNetwork>();
if(comms != null)
{
if (owner.isLocal)
{
setDissonanceID(comms.dissonanceId);
comms.voiceQueued += sendVoiceData;
//we also need to know when other players join, so we can send the dissonance ID again
owner.manager.onPlayerJoined += (player) =>
{
byte[] b = Encoding.UTF8.GetBytes(dissonanceID);
owner.sendMessage(this, "d", b);
};
}
}
if (owner.isLocal)
{
StartCoroutine(syncBehavior());
}
}
void sendVoiceData(ArraySegment<byte> data)
{
//need to send it
if(owner != null && owner.isLocal)
{
byte[] toSend = new byte[data.Count+4];
byte[] lastAudioIdBytes = BitConverter.GetBytes(lastAudioId++);
Buffer.BlockCopy(lastAudioIdBytes, 0, toSend, 0, 4);
Buffer.BlockCopy(data.Array, data.Offset, toSend, 4, data.Count);
owner.sendMessage(this, "a", toSend);
}
}
public void setDissonanceID(string id) //this sort of all initializes dissonance
{
dissonanceID = id;
byte[] b = Encoding.UTF8.GetBytes(dissonanceID);
owner.sendMessage(this, "d", b);
comms.comms.TrackPlayerPosition(this);
}
void voiceInitialized(string id)
{
dissonanceID = id;
}
void OnDestroy()
{
comms.playerLeft(dissonanceID);
}
IEnumerator syncBehavior()
{
while (true)
{
owner.sendMessage(this, "s", getSyncMessage());
yield return new WaitForSeconds(.1f);
}
}
// Update is called once per frame
void Update()
{
if (owner != null && !owner.isLocal)
{
transform.position = Vector3.Lerp(transform.position, targetPosition, .1f);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, .1f);
}
else if(owner != null && owner.isLocal)
{
//handle dissonance comms
//if we're not speaking, and the comms say we are, send a speaking event, which will be received on other network players and sent to their comms accordingly
if (comms.comms.FindPlayer(dissonanceID).IsSpeaking != isSpeaking) //unfortunately, there does not seem to be an event for this
{
isSpeaking = !isSpeaking;
byte[] toSend = new byte[1];
toSend[0] = isSpeaking ? (byte)1 : (byte)0;
owner.sendMessage(this, "x", toSend);
if (!isSpeaking)
{
lastAudioId = 0;
}
}
Vector3 movement = new Vector3();
movement.x += Input.GetAxis("Horizontal");
movement.y += Input.GetAxis("Vertical");
movement.z = 0;
transform.Translate(movement * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
owner.networkInstantiate("TestNetworkedGameObject");
}
if (Input.GetKeyDown(KeyCode.BackQuote))
{
foreach(KeyValuePair<string,NetworkObject> kvp in owner.manager.objects)
{
owner.takeOwnership(kvp.Key);
}
}
if (Input.GetKeyDown(KeyCode.Backspace))
{
foreach (KeyValuePair<string, NetworkObject> kvp in owner.manager.objects)
{
owner.networkDestroy(kvp.Key);
}
}
}
}
}

View File

@ -1826,6 +1826,8 @@ MonoBehaviour:
playerPrefab: {fileID: 6139051692386484099, guid: d4158ab9c4a204cdbba28d3273fc1fb3, type: 3}
prefabs:
- {fileID: 8565720275311462452, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
sceneObjects: []
deletedSceneObjects: []
connected: 0
--- !u!1 &1154194181
GameObject:

View File

@ -3,10 +3,10 @@ using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System;
using Dissonance;
[RequireComponent(typeof(NetworkObject))]
public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
public class NetworkPlayer : MonoBehaviour
{
public NetworkObject myObject;
@ -20,22 +20,9 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
public int lastObjectId=0; //for instantiation
public Dissonance.VelCommsNetwork commsNetwork;
bool isSpeaking = false;
uint lastAudioId = 0;
public string dissonanceID;
//required by dissonance for spatial audio
public string PlayerId => dissonanceID;
public Vector3 Position => myObject.transform.position;
public Quaternion Rotation => myObject.transform.rotation;
public NetworkPlayerType Type => isLocal?NetworkPlayerType.Local:NetworkPlayerType.Remote;
public bool IsTracking => true;
bool isMaster = false;
public List<int> closePlayers = new List<int>(); //for testing audio communications
public bool changeClosePlayers = true;
void Start()
{
myObject.owner = this;
@ -45,24 +32,8 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
// Update is called once per frame
void Update()
{
//handle dissonance comms
if(isLocal)
{
//if we're not speaking, and the comms say we are, send a speaking event, which will be received on other network players and sent to their comms accordingly
if (commsNetwork.comms.FindPlayer(dissonanceID).IsSpeaking != isSpeaking) //unfortunately, there does not seem to be an event for this
{
isSpeaking = !isSpeaking;
manager.sendTo(NetworkManager.MessageType.OTHERS, "4," + (isSpeaking?1:0) + ";");
if (!isSpeaking)
{
lastAudioId = 0;
}
}
}
}
@ -111,43 +82,6 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
myObject.handleMessage(identifier, message);
break;
}
case "2": //audio data
{
if (isSpeaking)
{
byte[] data = Convert.FromBase64String(sections[1]);
uint sequenceNumber = uint.Parse(sections[2]);
commsNetwork.voiceReceived(dissonanceID, data, sequenceNumber);
}
break;
}
case "3": //dissonance id (player joined)
{
if (dissonanceID == "")
{
dissonanceID = sections[1];
//tell the comms network that this player joined the channel
commsNetwork.playerJoined(dissonanceID); //tell dissonance
commsNetwork.comms.TrackPlayerPosition(this); //tell dissonance to track the remote player
}
break;
}
case "4": //speaking state
{
if(sections[1] == "0")
{
commsNetwork.playerStoppedSpeaking(dissonanceID);
isSpeaking = false;
}
else
{
commsNetwork.playerStartedSpeaking(dissonanceID);
isSpeaking = true;
}
break;
}
case "5": //sync update for an object I may own
{
@ -218,30 +152,6 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
}
public void OnDestroy()
{
commsNetwork.playerLeft(dissonanceID);
}
public void sendAudioData(ArraySegment<byte> data)
{
if (changeClosePlayers)
{
manager.setupMessageGroup("close", closePlayers.ToArray());
changeClosePlayers = false;
}
string b64_data = Convert.ToBase64String(data.Array,data.Offset,data.Count);
//manager.sendTo(NetworkManager.MessageType.OTHERS, "2," + b64_data + "," + (lastAudioId++) + ";");
manager.sendToGroup("close", "2,"+b64_data + ","+ (lastAudioId++) +";");
}
public void setDissonanceID(string id) //this sort of all initializes dissonance
{
dissonanceID = id;
manager.sendTo(NetworkManager.MessageType.OTHERS, "3," + id+";");
commsNetwork.comms.TrackPlayerPosition(this);
}
public void setAsMasterPlayer()
{
isMaster = true;

View File

@ -1,100 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : NetworkObject
{
public Vector3 targetPosition;
public Quaternion targetRotation;
public byte[] getSyncMessage()
{
float[] data = new float[7];
for (int i = 0; i < 3; i++)
{
data[i] = transform.position[i];
data[i + 3] = transform.rotation[i];
}
data[6] = transform.rotation[3];
byte[] toReturn = new byte[sizeof(float) * data.Length];
Buffer.BlockCopy(data, 0, toReturn, 0, toReturn.Length);
return toReturn;
}
public override void handleMessage(string identifier, byte[] message)
{
switch (identifier)
{
case "s":
float[] data = new float[7];
Buffer.BlockCopy(message, 0, data, 0, message.Length);
for (int i = 0; i < 3; i++)
{
targetPosition[i] = data[i];
targetRotation[i] = data[i + 3];
}
targetRotation[3] = data[6];
break;
}
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(syncBehavior());
}
IEnumerator syncBehavior()
{
while (true)
{
if (owner != null && owner.isLocal)
{
owner.sendMessage(this, "s", getSyncMessage());
}
yield return new WaitForSeconds(.1f);
}
}
// Update is called once per frame
void Update()
{
if (owner != null && !owner.isLocal)
{
transform.position = Vector3.Lerp(transform.position, targetPosition, .1f);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, .1f);
}
else if(owner != null && owner.isLocal)
{
Vector3 movement = new Vector3();
movement.x += Input.GetAxis("Horizontal");
movement.y += Input.GetAxis("Vertical");
movement.z = 0;
transform.Translate(movement * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
owner.networkInstantiate("TestNetworkedGameObject");
}
if (Input.GetKeyDown(KeyCode.BackQuote))
{
foreach(KeyValuePair<string,NetworkObject> kvp in owner.manager.objects)
{
owner.takeOwnership(kvp.Key);
}
}
if (Input.GetKeyDown(KeyCode.Backspace))
{
foreach (KeyValuePair<string, NetworkObject> kvp in owner.manager.objects)
{
owner.networkDestroy(kvp.Key);
}
}
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: feb473f59814842848dd4183ddeb6d9e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,126 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Dissonance;
using Dissonance.Extensions;
using Dissonance.Networking;
using UnityEngine;
namespace Dissonance
{
public class VelCommsNetwork : MonoBehaviour, ICommsNetwork
{
public ConnectionStatus Status
{
get
{
return manager.connected?ConnectionStatus.Connected:ConnectionStatus.Disconnected;
}
}
public NetworkMode Mode
{
get
{
return NetworkMode.Client;
}
}
public event Action<NetworkMode> ModeChanged;
public event Action<string, CodecSettings> PlayerJoined;
public event Action<string> PlayerLeft;
public event Action<VoicePacket> VoicePacketReceived;
public event Action<TextMessage> TextPacketReceived;
public event Action<string> PlayerStartedSpeaking;
public event Action<string> PlayerStoppedSpeaking;
public event Action<RoomEvent> PlayerEnteredRoom;
public event Action<RoomEvent> PlayerExitedRoom;
ConnectionStatus _status = ConnectionStatus.Disconnected;
CodecSettings initSettings;
public string dissonanceId;
public DissonanceComms comms;
public NetworkManager manager;
NetworkPlayer myPlayer;
public void Initialize(string playerName, Rooms rooms, PlayerChannels playerChannels, RoomChannels roomChannels, CodecSettings codecSettings)
{
dissonanceId = playerName;
initSettings = codecSettings;
Debug.Log("Initializing dissonance");
manager.onJoinedRoom += (player) =>
{
//this is me joining a vel room
myPlayer = player;
myPlayer.commsNetwork = this;
myPlayer.setDissonanceID(playerName); //need to let that new player know my dissonance id (tell everyone again)
};
manager.onPlayerJoined += (player) =>
{
//this is someone else joining the vel room
myPlayer.setDissonanceID(playerName); //need to let that new player know my dissonance id (tell everyone again)
player.commsNetwork = this; //this will tell us when various things happen of importance
};
}
public void voiceReceived(string sender,byte[] data,uint sequenceNumber)
{
VoicePacket vp = new VoicePacket(sender, ChannelPriority.Default, 1, true, new ArraySegment<byte>(data,0,data.Length), sequenceNumber);
VoicePacketReceived(vp);
}
public void SendText(string data, ChannelType recipientType, string recipientId)
{
Debug.Log("sending text");
}
public void SendVoice(ArraySegment<byte> data)
{
myPlayer?.sendAudioData(data);
}
// Start is called before the first frame update
void Start()
{
_status = ConnectionStatus.Connected;
comms = GetComponent<DissonanceComms>();
}
public void playerJoined(string id)
{
Debug.Log("dissonance player joined");
PlayerJoined(id, initSettings);
RoomEvent re = new RoomEvent();
re.Joined = true;
re.Room = "Global";
re.PlayerName = id;
PlayerEnteredRoom(re);
}
public void playerLeft(string id)
{
RoomEvent re = new RoomEvent();
re.Joined = false;
re.Room = "Global";
re.PlayerName = id;
PlayerExitedRoom(re);
PlayerLeft(id);
}
public void playerStartedSpeaking(string id)
{
PlayerStartedSpeaking(id);
}
public void playerStoppedSpeaking(string id)
{
PlayerStoppedSpeaking(id);
}
}
}

View File

@ -6,6 +6,6 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/Scenes/test.unity
path: Assets/VelGameServer/Example/test.unity
guid: e4e43899246c941c78acfc59ce2f664a
m_configObjects: {}

View File

@ -12,8 +12,8 @@ PlayerSettings:
targetDevice: 2
useOnDemandResources: 0
accelerometerFrequency: 60
companyName: DefaultCompany
productName: TestVelGameServer
companyName: VEL
productName: VelNetUnity
defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0}
m_SplashScreenBackgroundColor: {r: 0.13725491, g: 0.12156863, b: 0.1254902, a: 1}
@ -146,7 +146,7 @@ PlayerSettings:
androidSupportedAspectRatio: 1
androidMaxAspectRatio: 2.1
applicationIdentifier:
Standalone: com.DefaultCompany.TestVelGameServer
Standalone: com.VEL.VelNetUnity
buildNumber:
Standalone: 0
iPhone: 0

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash
./TestVelGameServer/build/macbuild.app/Contents/MacOS/TestVelGameServer &
./TestVelGameServer/Build/macbuild.app/Contents/MacOS/TestVelGameServer &