Decompiled source of Wide Angle Camera v1.0.0

WideAngleCamera.dll

Decompiled 8 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("WideAngleCamera")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0db7f372f4b75385932dc3b74c1f0d3a69ca4fd9")]
[assembly: AssemblyProduct("A White Knuckle mod to implement stereographic camera projection for wider angle views")]
[assembly: AssemblyTitle("WideAngleCamera")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace WideAngleCamera
{
	[BepInPlugin("wk.barackobusiness.wideangle", "Wide Angle Camera", "1.0.0")]
	public class WideAnglePlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		private ConfigEntry<int> resolution;

		private ConfigEntry<bool> useBackCam;

		private ConfigEntry<float> fieldOfView;

		private Material stereoMat;

		private GameObject projector;

		private GameObject stereoCam;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			resolution = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Resolution", 512, "The default side length of a face on the cubemap.");
			useBackCam = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable backface", false, "Whether to render behind the player or not, this option incurs additional performance cost and is only useful if using extreme fields of view at which distortion makes gameplay impractical.");
			fieldOfView = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Field of view", 135f, "The vertical field of view of the camera in degrees.");
			if (LoadBundleAssets())
			{
				SceneManager.sceneLoaded += OnSceneLoad;
				Logger.LogInfo((object)"Wide angle views are now possible.");
			}
		}

		private void OnSceneLoad(Scene scene, LoadSceneMode mode)
		{
			if (((Scene)(ref scene)).name != "Intro" && ((Scene)(ref scene)).name != "Main-Menu")
			{
				SetupScene();
			}
		}

		private void SetupScene()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = ((Component)Camera.main).transform;
			GameObject obj = Object.Instantiate<GameObject>(stereoCam, transform);
			GameObject val = Object.Instantiate<GameObject>(projector, transform);
			val.layer = 31;
			val.transform.localPosition = new Vector3(0f, 0f, 0.5f);
			val.transform.localRotation = Quaternion.Euler(0f, 0f, 180f);
			val.transform.localScale = new Vector3(Camera.main.aspect, 1f, 1f);
			obj.AddComponent<StereographicCameraManager>().Init((Renderer)(object)val.GetComponent<MeshRenderer>(), useBackCam.Value, resolution.Value, fieldOfView.Value);
			Camera.main.orthographic = true;
			Camera.main.orthographicSize = 0.5f;
			Camera.main.cullingMask = int.MinValue;
		}

		private bool LoadBundleAssets()
		{
			AssetBundle val = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "stereoassets"));
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"Wide angle views are NOT possible, please check if the asset bundle is in the same directory as the plugin.");
				return false;
			}
			stereoMat = val.LoadAsset<Material>("Screen");
			projector = val.LoadAsset<GameObject>("Projector");
			stereoCam = val.LoadAsset<GameObject>("Stereographic Camera");
			if ((Object)(object)stereoMat == (Object)null || (Object)(object)projector == (Object)null || (Object)(object)stereoCam == (Object)null)
			{
				Logger.LogError((object)"Wide angle views are NOT possible, please check the checksum of the asset bundle in the plugin directory, if it does not match X, reacquire this bundle");
				return false;
			}
			Object.DontDestroyOnLoad((Object)(object)stereoMat);
			Object.DontDestroyOnLoad((Object)(object)projector);
			Object.DontDestroyOnLoad((Object)(object)stereoCam);
			return true;
		}
	}
	public class StereographicCameraManager : MonoBehaviour
	{
		private Camera perspective;

		private Camera front;

		private Camera back;

		private Camera left;

		private Camera right;

		private Camera down;

		private Camera up;

		private RenderTexture cubemap;

		public void Init(Renderer projector, bool useBack, int size, float fov)
		{
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Expected O, but got Unknown
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			perspective = Camera.main;
			front = ((Component)((Component)this).transform.GetChild(0)).GetComponent<Camera>();
			SetupCam(front, size);
			left = ((Component)((Component)this).transform.GetChild(1)).GetComponent<Camera>();
			SetupCam(left, size);
			right = ((Component)((Component)this).transform.GetChild(2)).GetComponent<Camera>();
			SetupCam(right, size);
			down = ((Component)((Component)this).transform.GetChild(3)).GetComponent<Camera>();
			SetupCam(down, size);
			up = ((Component)((Component)this).transform.GetChild(4)).GetComponent<Camera>();
			SetupCam(up, size);
			if (useBack)
			{
				GameObject val = Object.Instantiate<GameObject>(((Component)front).gameObject, ((Component)this).transform, false);
				((Object)val).name = "Back";
				val.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
				back = val.GetComponent<Camera>();
				SetupCam(back, size);
			}
			cubemap = new RenderTexture(size, size, 16);
			((Texture)cubemap).dimension = (TextureDimension)4;
			projector.material.SetTexture("_Cube", (Texture)(object)cubemap);
			projector.material.SetFloat("_FOV", fov);
		}

		private void Update()
		{
			Graphics.CopyTexture((Texture)(object)front.targetTexture, 0, 0, (Texture)(object)cubemap, 4, 0);
			Graphics.CopyTexture((Texture)(object)left.targetTexture, 0, 0, (Texture)(object)cubemap, 1, 0);
			Graphics.CopyTexture((Texture)(object)right.targetTexture, 0, 0, (Texture)(object)cubemap, 0, 0);
			Graphics.CopyTexture((Texture)(object)up.targetTexture, 0, 0, (Texture)(object)cubemap, 3, 0);
			Graphics.CopyTexture((Texture)(object)down.targetTexture, 0, 0, (Texture)(object)cubemap, 2, 0);
			if ((Object)(object)back != (Object)null)
			{
				Graphics.CopyTexture((Texture)(object)back.targetTexture, 0, 0, (Texture)(object)cubemap, 5, 0);
			}
		}

		private void OnDestroy()
		{
			cubemap.Release();
			front.targetTexture.Release();
			left.targetTexture.Release();
			right.targetTexture.Release();
			down.targetTexture.Release();
			up.targetTexture.Release();
			if ((Object)(object)back != (Object)null)
			{
				back.targetTexture.Release();
			}
		}

		private void SetupCam(Camera cam, int size)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			RenderTexture targetTexture = new RenderTexture(size, size, 16);
			cam.targetTexture = targetTexture;
			cam.cullingMask = perspective.cullingMask;
			cam.clearFlags = perspective.clearFlags;
			cam.depthTextureMode = (DepthTextureMode)1;
			cam.depth = 1f;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "WideAngleCamera";

		public const string PLUGIN_NAME = "A White Knuckle mod to implement stereographic camera projection for wider angle views";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}