210 lines
6.7 KiB
C#
210 lines
6.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Collections;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.TextCore.Text;
|
|
|
|
|
|
namespace FreeD
|
|
{
|
|
[RequireComponent(typeof(Camera))]
|
|
[ExecuteInEditMode]
|
|
public class FreeDReceiver : MonoBehaviour
|
|
{
|
|
|
|
public enum CameraModel
|
|
{
|
|
UE150,
|
|
UE100
|
|
}
|
|
|
|
[Header("Camera Model")]
|
|
public CameraModel cameraModel = CameraModel.UE150;
|
|
|
|
[Header("Zoom Calibration")]
|
|
// ZOOM KURVA
|
|
public AnimationCurve zoomCurve = AnimationCurve.Linear(0f, 1f, 1f, 1f);
|
|
//
|
|
public float freedZoomMin = 1365f;
|
|
public float freedZoomMax = 4049f;
|
|
public float focalLengthMin = 16.99f;
|
|
public float focalLengthMax = 328.99f;
|
|
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 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;
|
|
SetUpZoomCurve();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (server == null) return;
|
|
server.received -= OnPacketReceived;
|
|
server.Stop();
|
|
}
|
|
|
|
// ZOOM SAKER
|
|
void OnValidate()
|
|
{
|
|
SetUpZoomCurve();
|
|
}
|
|
|
|
void SetUpZoomCurve()
|
|
{
|
|
zoomCurve = new AnimationCurve();
|
|
|
|
if (cameraModel == CameraModel.UE150)
|
|
{
|
|
// Kamera 1 och 2 - lägg till mätpunkter här
|
|
zoomCurve.AddKey(0.000f, 16.99f); // zoom 1365
|
|
zoomCurve.AddKey(0.018f, 17.997f); // zoom 1413
|
|
zoomCurve.AddKey(0.119f, 26.2f); // zoom 1688
|
|
zoomCurve.AddKey(0.238f, 36.75f); // zoom 2016
|
|
zoomCurve.AddKey(0.428f, 56.95f); // zoom 2535
|
|
zoomCurve.AddKey(0.479f, 64.0f); // zoom 2671
|
|
zoomCurve.AddKey(0.533f, 74.19f); // zoom 2810
|
|
zoomCurve.AddKey(1.000f, 328.99f); // zoom 4094
|
|
}
|
|
else if (cameraModel == CameraModel.UE100)
|
|
{
|
|
// Kamera 3
|
|
zoomCurve.AddKey(0.000f, 3.98f); // zoom 1365
|
|
zoomCurve.AddKey(0.085f, 5.25f); // zoom 1596
|
|
zoomCurve.AddKey(0.233f, 8.27f); // zoom 2000
|
|
zoomCurve.AddKey(0.416f, 13.19f); // zoom 2499
|
|
zoomCurve.AddKey(0.550f, 17.80f); // zoom 2866
|
|
zoomCurve.AddKey(0.575f, 18.98f); // zoom 2934
|
|
zoomCurve.AddKey(0.786f, 32.54f); // zoom 3510
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
//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) camera.fieldOfView = (packet.Zoom * ( (19f-169f) / (63f-21f) ) ) + 100f;
|
|
// camera.focusDistance = -packet.Focus / 1000f;
|
|
// ÄNDRA ZOOM
|
|
|
|
if (packet.Zoom > 0)
|
|
{
|
|
//float t = (packet.Zoom - freedZoomMin) / (freedZoomMax - freedZoomMin);
|
|
//float focal = Mathf.Lerp(focalLengthMin, focalLengthMax, t);
|
|
//camera.focalLength = Mathf.Clamp(focal, focalLengthMin, focalLengthMax);
|
|
|
|
//zoom kurva
|
|
float t = (packet.Zoom - freedZoomMin) / (freedZoomMax - freedZoomMin);
|
|
camera.focalLength = zoomCurve.Evaluate(t);
|
|
}
|
|
Debug.Log($"RAW Zoom: {packet.Zoom}");
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
} |