Removed unecessary SupportCamera and just move the original one

This commit is contained in:
Jacopo Libè 2021-10-30 18:33:32 +02:00
parent 9ab8081058
commit 33a6cb1bc7
3 changed files with 13 additions and 141 deletions

View File

@ -32,12 +32,8 @@ namespace NomaiVR.EffectFixes
//Simulation Camera
Postfix<SimulationCamera>(nameof(SimulationCamera.Awake), nameof(Post_SimulationCamera_Awake));
Postfix<SimulationCamera>(nameof(SimulationCamera.OnPreRender), nameof(Post_SimulationCamera_OnPreRender));
Postfix<SimulationCamera>(nameof(SimulationCamera.OnEnable), nameof(Post_SimulationCamera_OnEnable));
Postfix<SimulationCamera>(nameof(SimulationCamera.OnDisable), nameof(Post_SimulationCamera_OnDisable));
Postfix<SimulationCamera>(nameof(SimulationCamera.DeallocateRenderTex), nameof(Post_SimulationCamera_DeallocateRenderTex));
Prefix<SimulationCamera>(nameof(SimulationCamera.AllocateRenderTex), nameof(Pre_SimulationCamera_AllocateRenderTex));
Prefix<SimulationCamera>(nameof(SimulationCamera.VerifyRenderTexResolution), nameof(Pre_SimulationCamera_VerifyRenderTexResolution));
}
public static void DisableScreenSpaceReflections(PostProcessingGameplaySettings __instance)
@ -143,21 +139,17 @@ namespace NomaiVR.EffectFixes
private static void Post_SimulationCamera_Awake(SimulationCamera __instance)
{
__instance._camera.stereoTargetEye = StereoTargetEyeMask.Left;
__instance._camera.cullingMask = LayerMask.GetMask("DreamSimulation", "UI");
var supportCamera = new GameObject("StereoSupportCamera");
supportCamera.transform.SetParent(__instance.transform, false);
supportCamera.transform.localPosition = Vector3.zero;
supportCamera.transform.localRotation = Quaternion.identity;
var simSupportCam = supportCamera.AddComponent<SupportSimulationCamera>();
simSupportCam.SetupSimulationCameraParent(__instance);
}
private static SimulationCamera _currentCamera;
private static void Post_SimulationCamera_OnEnable(SimulationCamera __instance)
{
if (__instance._targetCamera != null && __instance._targetCamera.mainCamera.stereoEnabled)
{
__instance.GetComponentInChildren<SupportSimulationCamera>().enabled = true;
_currentCamera = __instance;
__instance._camera.enabled = false;
__instance._targetCamera.onThisPreRender += RenderEye;
}
GlobalMessenger.FireEvent("SimulationEnter");
@ -167,34 +159,17 @@ namespace NomaiVR.EffectFixes
{
if (__instance._targetCamera != null && __instance._targetCamera.mainCamera.stereoEnabled)
{
__instance.GetComponentInChildren<SupportSimulationCamera>().enabled = false;
__instance._targetCamera.onThisPreRender -= RenderEye;
_currentCamera = null;
}
GlobalMessenger.FireEvent("SimulationExit");
}
private static void Post_SimulationCamera_OnPreRender(SimulationCamera __instance)
private static void RenderEye(OWCamera stereoCamera)
{
if (__instance._targetCamera == null)
{
return;
}
GraphicsHelper.ForceCameraToEye(__instance._camera, __instance._targetCamera.mainCamera, Valve.VR.EVREye.Eye_Left);
}
private static void Pre_SimulationCamera_VerifyRenderTexResolution(SimulationCamera __instance)
{
__instance.GetComponentInChildren<SupportSimulationCamera>().VerifyRenderTexResolution(__instance._targetCamera.mainCamera);
}
private static void Pre_SimulationCamera_AllocateRenderTex(SimulationCamera __instance)
{
__instance.GetComponentInChildren<SupportSimulationCamera>().AllocateTexture();
}
private static void Post_SimulationCamera_DeallocateRenderTex(SimulationCamera __instance)
{
__instance.GetComponentInChildren<SupportSimulationCamera>().DeallocateTexture();
GraphicsHelper.ForceCameraToEye(_currentCamera._camera, stereoCamera.mainCamera.transform, (Valve.VR.EVREye)stereoCamera.mainCamera.stereoActiveEye);
_currentCamera._camera.Render();
}
}
}

View File

@ -5,11 +5,11 @@ namespace NomaiVR.Helpers
{
public static class GraphicsHelper
{
public static void ForceCameraToEye(Camera targetCamera, Camera monoCamera, EVREye eye)
public static void ForceCameraToEye(Camera targetCamera, Transform head, EVREye eye)
{
targetCamera.transform.position = monoCamera.transform.TransformPoint(SteamVR.instance.eyes[(int)eye].pos);
targetCamera.transform.rotation = monoCamera.transform.rotation * SteamVR.instance.eyes[(int)eye].rot;
targetCamera.projectionMatrix = GetSteamVREyeProjection(monoCamera, eye);
targetCamera.transform.position = head.TransformPoint(SteamVR.instance.eyes[(int)eye].pos);
targetCamera.transform.rotation = head.rotation * SteamVR.instance.eyes[(int)eye].rot;
targetCamera.projectionMatrix = GetSteamVREyeProjection(targetCamera, eye);
}
public static Matrix4x4 GetSteamVREyeProjection(Camera cam, EVREye eye)

View File

@ -1,103 +0,0 @@
using NomaiVR.Assets;
using NomaiVR.Helpers;
using UnityEngine;
namespace NomaiVR.ReusableBehaviours.Dream
{
/// <summary>
/// Support camera used for stereo rendering of the simulation effect
/// </summary>
public class SupportSimulationCamera : MonoBehaviour
{
private Camera camera;
private SimulationCamera simulationCamera;
private RenderTexture simulationRenderTexture;
private void Awake()
{
this.camera = gameObject.AddComponent<Camera>();
this.camera.stereoTargetEye = StereoTargetEyeMask.Right;
this.simulationRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.sRGB);
this.simulationRenderTexture.name = "SimulationRenderTexture_R";
this.simulationRenderTexture.useDynamicScale = true;
this.enabled = false;
}
public void SetupSimulationCameraParent(SimulationCamera simulationCamera)
{
this.simulationCamera = simulationCamera;
this.gameObject.layer = simulationCamera.gameObject.layer;
this.camera.cullingMask = simulationCamera._camera.cullingMask;
this.camera.depthTextureMode = DepthTextureMode.Depth;
this.camera.allowMSAA = false;
this.camera.clearFlags = simulationCamera._camera.clearFlags;
this.camera.backgroundColor = simulationCamera._camera.backgroundColor;
this.camera.renderingPath = simulationCamera._camera.renderingPath;
this.camera.depth = simulationCamera._camera.depth;
this.camera.nearClipPlane = simulationCamera._camera.nearClipPlane;
this.camera.farClipPlane = simulationCamera._camera.farClipPlane;
this.camera.allowDynamicResolution = simulationCamera._camera.allowDynamicResolution;
simulationCamera._simulationMaskMaterial.shader = ShaderLoader.GetShader("Hidden/StereoBlitSimulationMask");
simulationCamera._simulationCompositeMaterial.shader = ShaderLoader.GetShader("Hidden/StereoBlitSimulationComposite");
simulationCamera._simulationMaskMaterial.SetTexture("_RightTex", this.simulationRenderTexture);
simulationCamera._simulationCompositeMaterial.SetTexture("_RightTex", this.simulationRenderTexture);
}
private void OnEnable()
{
this.camera.enabled = true;
}
private void OnDisable()
{
this.camera.enabled = false;
}
public void AllocateTexture()
{
this.simulationRenderTexture.Create();
this.camera.targetTexture = this.simulationRenderTexture;
}
public void DeallocateTexture()
{
this.camera.targetTexture = null;
this.simulationRenderTexture.Release();
}
private void OnDestroy()
{
this.simulationRenderTexture.Release();
GameObject.Destroy(this.simulationRenderTexture);
this.simulationRenderTexture = null;
}
public void VerifyRenderTexResolution(Camera targetCamera)
{
if (targetCamera == null)
{
return;
}
if (this.simulationRenderTexture.width == targetCamera.pixelWidth && this.simulationRenderTexture.height == targetCamera.pixelHeight)
{
return;
}
if (this.simulationRenderTexture.IsCreated())
{
this.simulationRenderTexture.Release();
this.simulationRenderTexture.width = targetCamera.pixelWidth;
this.simulationRenderTexture.height = targetCamera.pixelHeight;
this.simulationRenderTexture.Create();
return;
}
this.simulationRenderTexture.width = targetCamera.pixelWidth;
this.simulationRenderTexture.height = targetCamera.pixelHeight;
}
private void OnPreRender()
{
if (this.simulationCamera == null || this.simulationCamera._targetCamera == null) return;
GraphicsHelper.ForceCameraToEye(this.camera, this.simulationCamera._targetCamera.mainCamera, Valve.VR.EVREye.Eye_Right);
}
}
}