161 lines
4.7 KiB
C#
161 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Collections;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace FreeD
|
|
{
|
|
[RequireComponent(typeof(Camera))]
|
|
[ExecuteInEditMode]
|
|
public class FreeDReceiver : MonoBehaviour
|
|
{
|
|
public int cameraId;
|
|
|
|
[Header("Listening on port")]
|
|
public int listeningPort = 40_000;
|
|
[Tooltip("Run the server in edit mode")]
|
|
public new bool runInEditMode = true;
|
|
[Tooltip("Show received messages in Unity console")]
|
|
public bool showPacketInConsole = true;
|
|
private string latestMessage;
|
|
[Tooltip("Apply position modification to Unity")]
|
|
public bool modifyPositionWithFreeD = false;
|
|
public bool useRestrictionsPanasonicAW_UE = true;
|
|
|
|
[Header("Debug")]
|
|
[Tooltip("Apply the values received to the gameObject")]
|
|
public bool apply = true;
|
|
|
|
FreeDServer server;
|
|
Packet packet;
|
|
public Packet lastPacket;
|
|
|
|
void OnEnable()
|
|
{
|
|
if (!Application.isPlaying && !runInEditMode) return;
|
|
server = FreeDServer.Get(listeningPort);
|
|
server.received += OnPacketReceived;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (server == null) return;
|
|
server.received -= OnPacketReceived;
|
|
server.Stop();
|
|
}
|
|
|
|
private void OnPacketReceived(Packet packet)
|
|
{
|
|
if (packet.Id != cameraId) return;
|
|
|
|
this.packet = packet;
|
|
if (showPacketInConsole)
|
|
latestMessage = $"Receiving Camera ID {packet.Id}: {packet.Id},{packet.Pan},{packet.Tilt},{packet.Roll},{packet.PosZ},{packet.PosX},{packet.PosY},{packet.Zoom},{packet.Focus}";
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Application.isPlaying && !runInEditMode) return;
|
|
if (packet == null) return;
|
|
lastPacket = packet;
|
|
if (!apply) return;
|
|
|
|
|
|
// Cameras don't need to respond to position
|
|
if(modifyPositionWithFreeD)
|
|
{
|
|
transform.localPosition = new Vector3(packet.PosX / 1000f, packet.PosY / 1000f, -packet.PosZ / 1000f);
|
|
}
|
|
|
|
transform.localRotation = Quaternion.identity;
|
|
transform.Rotate(Vector3.up, packet.Pan);
|
|
transform.Rotate(Vector3.up, 90f);
|
|
transform.Rotate(Vector3.right, -packet.Tilt);
|
|
transform.Rotate(Vector3.forward, -packet.Roll);
|
|
|
|
var camera = GetComponent<Camera>();
|
|
if (camera != null)
|
|
{
|
|
if (packet.Zoom > 0)
|
|
{
|
|
if(useRestrictionsPanasonicAW_UE)
|
|
{
|
|
float newFocalLength = (packet.Zoom / (4095f / 179f));
|
|
}
|
|
else
|
|
{
|
|
camera.focalLength = Mathf.Clamp(packet.Zoom / (4095f / 179f), 0.001f, 179f);
|
|
}
|
|
|
|
}
|
|
camera.focusDistance = -packet.Focus / 1000f;
|
|
}
|
|
|
|
|
|
packet = null;
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (!Application.isPlaying)
|
|
{
|
|
UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
|
|
UnityEditor.SceneView.RepaintAll();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
Rect rectPosOnScreen = new Rect(0, 10 + (cameraId * 20f), Screen.width, 0.05f * Screen.height);
|
|
if (showPacketInConsole)
|
|
{
|
|
GUIStyle textStyle = new GUIStyle(GUI.skin.label);
|
|
textStyle.normal.textColor = Color.green;
|
|
textStyle.fontSize = 10;
|
|
GUI.Label(rectPosOnScreen, latestMessage, textStyle);
|
|
}
|
|
else
|
|
{
|
|
GUI.Label(rectPosOnScreen, "");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
[CustomEditor(typeof(FreeDReceiver))]
|
|
public class FreeDReceiverEditor : Editor
|
|
{
|
|
public override void OnInspectorGUI()
|
|
{
|
|
|
|
FreeDReceiver myScript = (FreeDReceiver)target;
|
|
|
|
if (myScript.enabled)
|
|
{
|
|
if (GUILayout.Button($"Stop Receiving from Camera ID {myScript.cameraId}"))
|
|
{
|
|
myScript.enabled = false;
|
|
myScript.apply= false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (GUILayout.Button($"Start Receiving from Camera ID {myScript.cameraId}"))
|
|
{
|
|
myScript.enabled = true;
|
|
myScript.apply = true;
|
|
}
|
|
}
|
|
|
|
DrawDefaultInspector();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
} |