Cailean Finn
9 months ago
5 changed files with 271 additions and 159 deletions
@ -0,0 +1,210 @@ |
|||||
|
using Cinemachine; |
||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Xml.Serialization; |
||||
|
using UnityEngine; |
||||
|
|
||||
|
public class CameraManager : MonoBehaviour |
||||
|
{ |
||||
|
public static CameraManager _instance { get; private set; } |
||||
|
|
||||
|
private GameObject m_Brain; |
||||
|
|
||||
|
private Camera m_Camera; |
||||
|
|
||||
|
private GameObject m_ActiveCamera; |
||||
|
|
||||
|
private List<GameObject> m_CameraObjects = new List<GameObject>(); |
||||
|
|
||||
|
private int m_SameCameraTypeCount; |
||||
|
|
||||
|
|
||||
|
[Header("Layer Masks")] |
||||
|
public LayerMask m_Everything; |
||||
|
public LayerMask m_Agent; |
||||
|
public LayerMask m_Bodies; |
||||
|
public LayerMask m_Environment; |
||||
|
|
||||
|
[Header("Camera Settings")] |
||||
|
[Range(2, 20)] |
||||
|
public int m_CameraBlendMin; |
||||
|
[Range(2, 20)] |
||||
|
public int m_CameraBlendMax; |
||||
|
[Range(5, 180)] |
||||
|
public int m_CameraSwapMin; |
||||
|
[Range(5, 180)] |
||||
|
public int m_CameraSwapMax; |
||||
|
|
||||
|
private void Awake() |
||||
|
{ |
||||
|
if(_instance != null) |
||||
|
Destroy(this); |
||||
|
else |
||||
|
_instance = this; |
||||
|
} |
||||
|
|
||||
|
private void OnEnable() |
||||
|
{ |
||||
|
CreateCameraList(); |
||||
|
InitialCameraSetup(); |
||||
|
SetCullingMask(); |
||||
|
} |
||||
|
|
||||
|
private void Start() |
||||
|
{ |
||||
|
StartCoroutine(CameraSwitch()); |
||||
|
} |
||||
|
|
||||
|
IEnumerator CameraSwitch() |
||||
|
{ |
||||
|
SetNewCamera(); |
||||
|
yield return new WaitForSecondsRealtime(Random.Range(m_CameraSwapMin, m_CameraSwapMax)); |
||||
|
StartCoroutine(CameraSwitch()); |
||||
|
} |
||||
|
|
||||
|
private void SetCullingMask() |
||||
|
{ |
||||
|
m_Camera = m_Brain.GetComponent<Camera>(); |
||||
|
|
||||
|
string currentTag = m_ActiveCamera.tag; |
||||
|
|
||||
|
switch (currentTag) |
||||
|
{ |
||||
|
case "vc_Agent": |
||||
|
m_Camera.cullingMask = m_Agent; |
||||
|
break; |
||||
|
case "vc_Body": |
||||
|
m_Camera.cullingMask = m_Bodies; |
||||
|
break; |
||||
|
case "vc_Environment": |
||||
|
m_Camera.cullingMask = m_Bodies; |
||||
|
break; |
||||
|
default: |
||||
|
case "vc_Default": |
||||
|
m_Camera.cullingMask = m_Everything; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private void SetNewCamera() |
||||
|
{ |
||||
|
// Get all objects with 0 prio
|
||||
|
List<int> deactiveCameras = new List<int>(); |
||||
|
bool switchCameraType = false; |
||||
|
|
||||
|
if (m_SameCameraTypeCount >= 3) |
||||
|
switchCameraType = true; |
||||
|
|
||||
|
for(int i = 0; i < m_CameraObjects.Count; i++) |
||||
|
{ |
||||
|
GameObject t = m_CameraObjects[i]; |
||||
|
CinemachineVirtualCamera virtualCamera = t.GetComponent<CinemachineVirtualCamera>(); |
||||
|
if (virtualCamera != null && virtualCamera.Priority == 0) |
||||
|
{ |
||||
|
if (switchCameraType) |
||||
|
{ |
||||
|
if(t.tag != m_ActiveCamera.tag) |
||||
|
deactiveCameras.Add(i); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
deactiveCameras.Add(i); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int randomIndex = deactiveCameras[Random.Range(0, deactiveCameras.Count - 1)]; |
||||
|
GameObject selectedCamera = m_CameraObjects[randomIndex]; |
||||
|
|
||||
|
if (selectedCamera.tag == m_ActiveCamera.tag) |
||||
|
{ |
||||
|
m_SameCameraTypeCount++; |
||||
|
SetCamera(selectedCamera); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
m_SameCameraTypeCount = 0; |
||||
|
SetCamera(selectedCamera); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SetCamera(GameObject camera) |
||||
|
{ |
||||
|
SetCameraBlend(camera); |
||||
|
|
||||
|
// Set all camera to active, and set all priorities to 0, bar the first camera
|
||||
|
foreach (GameObject obj in m_CameraObjects) |
||||
|
{ |
||||
|
CinemachineVirtualCamera vCam = obj.GetComponent<CinemachineVirtualCamera>(); |
||||
|
vCam.Priority = 0; |
||||
|
} |
||||
|
|
||||
|
camera.GetComponent<CinemachineVirtualCamera>().Priority = 1; |
||||
|
|
||||
|
m_ActiveCamera = camera; |
||||
|
|
||||
|
SetCullingMask(); |
||||
|
} |
||||
|
|
||||
|
private void SetCameraBlend(GameObject pastCamera) |
||||
|
{ |
||||
|
// Check activate camera tag, and apply the necessary blend-mode
|
||||
|
CinemachineBrain cmBrain = m_Brain.GetComponent<CinemachineBrain>(); |
||||
|
float blendtime = (float)Random.Range(m_CameraBlendMin, m_CameraBlendMax); |
||||
|
|
||||
|
if (AreDifferentPairs(pastCamera.tag, m_ActiveCamera.tag, "vc_Body", "vc_Agent")) |
||||
|
blendtime = 0; |
||||
|
|
||||
|
cmBrain.m_DefaultBlend.m_Time = blendtime; |
||||
|
} |
||||
|
|
||||
|
private bool AreDifferentPairs(string a, string b, string pair1, string pair2) |
||||
|
{ |
||||
|
return (a == pair1 && b == pair2) || (a == pair2 && b == pair1); |
||||
|
} |
||||
|
|
||||
|
private void InitialCameraSetup() |
||||
|
{ |
||||
|
// Set all camera to active, and set all priorities to 0, bar the first camera
|
||||
|
foreach(GameObject obj in m_CameraObjects) |
||||
|
{ |
||||
|
CinemachineVirtualCamera vCam = obj.GetComponent<CinemachineVirtualCamera>(); |
||||
|
vCam.Priority = 0; |
||||
|
} |
||||
|
|
||||
|
m_CameraObjects[0].GetComponent<CinemachineVirtualCamera>().Priority = 1; |
||||
|
m_ActiveCamera = m_CameraObjects[0]; |
||||
|
} |
||||
|
|
||||
|
private void CreateCameraList() |
||||
|
{ |
||||
|
GameObject parentObject = GameObject.Find("CM List"); |
||||
|
|
||||
|
if(parentObject != null) |
||||
|
{ |
||||
|
foreach(Transform child in parentObject.transform) |
||||
|
{ |
||||
|
m_CameraObjects.Add(child.gameObject); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Debug.LogError("CM List <GameObject> not found."); |
||||
|
} |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
m_Brain = GameObject.Find("CM Brain"); |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
Debug.LogError("CM Brain <GameObject> could not be found."); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: c001ebec265d2834486167f0ecb3d40d |
||||
|
MonoImporter: |
||||
|
externalObjects: {} |
||||
|
serializedVersion: 2 |
||||
|
defaultReferences: [] |
||||
|
executionOrder: 0 |
||||
|
icon: {instanceID: 0} |
||||
|
userData: |
||||
|
assetBundleName: |
||||
|
assetBundleVariant: |
@ -1 +1 @@ |
|||||
{"count":1,"self":104.3282752,"total":107.71104799999999,"children":{"InitializeActuators":{"count":1,"self":0.0010256,"total":0.0010256,"children":null},"InitializeSensors":{"count":1,"self":0.0010049,"total":0.0010049,"children":null},"AgentSendState":{"count":6105,"self":0.0373762,"total":1.0367875,"children":{"CollectObservations":{"count":3053,"self":0.979634,"total":0.979634,"children":null},"WriteActionMask":{"count":3053,"self":0.0015037,"total":0.0015037,"children":null},"RequestDecision":{"count":3053,"self":0.018273599999999997,"total":0.018273599999999997,"children":null}}},"DecideAction":{"count":6105,"self":1.3827178,"total":1.3827178,"children":null},"AgentAct":{"count":6105,"self":0.9609070999999999,"total":0.9609070999999999,"children":null}},"gauges":{"DReCon-v0.CumulativeReward":{"count":4,"max":1010.40234,"min":79.1054459,"runningAverage":510.454529,"value":246.063034,"weightedAverage":369.397522}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712679548","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_3-Moloch\\2_Unity\\Expeirments\\Drecon2022 -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-caile -hubSessionId 8620dcf5-e9e3-4cd2-bcfa-e78675b590b7 -accessToken W8bcU9OX7gbO8pWddkbaY47YgOdJ_03h8gDPK3m_oOE005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1712679656"}} |
{"count":1,"self":150.3252224,"total":156.5476267,"children":{"InitializeActuators":{"count":1,"self":0.00050539999999999992,"total":0.00050539999999999992,"children":null},"InitializeSensors":{"count":1,"self":0.0010003,"total":0.0010003,"children":null},"AgentSendState":{"count":8983,"self":0.0698756,"total":2.1456021,"children":{"CollectObservations":{"count":4492,"self":2.0270064,"total":2.0270064999999997,"children":null},"WriteActionMask":{"count":4492,"self":0.0142121,"total":0.0142121,"children":null},"RequestDecision":{"count":4492,"self":0.0345079,"total":0.0345079,"children":null}}},"DecideAction":{"count":8983,"self":2.7714388,"total":2.7714387,"children":null},"AgentAct":{"count":8983,"self":1.3038522,"total":1.3038522,"children":null}},"gauges":{"DReCon-v0.CumulativeReward":{"count":8,"max":908.505,"min":32.20705,"runningAverage":211.35524,"value":225.457748,"weightedAverage":177.7208}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1712753603","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\drecon-unity -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-cail -hubSessionId 4874cbb0-3ab6-4370-af74-dc24d0dd5dbe -accessToken gtOVuQT275ZJTcNaM_jom0CkevszjNJVBq4tQAWtuSU005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1712753760"}} |
Loading…
Reference in new issue