Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit bb4a44f

Browse files
committed
Fixed for Beat Saber 0.11.2. Removed FPS option (it was messing with custom avatars with minimal performance boost). Fixed SteamVR check.
1 parent e092bca commit bb4a44f

7 files changed

+56
-63
lines changed

CameraPlus/CameraMoverPointer.cs

+17-16
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@
33

44
namespace CameraPlus
55
{
6-
public class CameraMoverPointer : VRPointer
6+
public class CameraMoverPointer : MonoBehaviour
77
{
8+
protected const float MinScrollDistance = 0.25f;
9+
protected const float MaxLaserDistance = 50;
10+
11+
protected VRPointer _vrPointer;
812
protected CameraPlusBehaviour _cameraPlus;
913
protected Transform _cameraCube;
1014
protected VRController _grabbingController;
1115
protected Vector3 _grabPos;
1216
protected Quaternion _grabRot;
1317
protected Vector3 _realPos;
1418
protected Quaternion _realRot;
15-
protected const float MinDistance = 0.25f;
1619

17-
public virtual void Init(CameraPlusBehaviour cameraPlus,Transform cameraCube)
20+
public virtual void Init(CameraPlusBehaviour cameraPlus, Transform cameraCube)
1821
{
1922
_cameraPlus = cameraPlus;
2023
_cameraCube = cameraCube;
2124
_realPos = Plugin.Instance.Config.Position;
2225
_realRot = Quaternion.Euler(Plugin.Instance.Config.Rotation);
26+
_vrPointer = GetComponent<VRPointer>();
2327
}
2428

25-
public override void OnEnable()
29+
protected virtual void OnEnable()
2630
{
27-
base.OnEnable();
2831
Plugin.Instance.Config.ConfigChangedEvent += PluginOnConfigChangedEvent;
2932
}
3033

31-
public override void OnDisable()
34+
protected virtual void OnDisable()
3235
{
33-
base.OnDisable();
3436
Plugin.Instance.Config.ConfigChangedEvent -= PluginOnConfigChangedEvent;
3537
}
3638

@@ -40,19 +42,18 @@ protected virtual void PluginOnConfigChangedEvent(Config config)
4042
_realRot = Quaternion.Euler(config.Rotation);
4143
}
4244

43-
public override void Update()
45+
protected virtual void Update()
4446
{
45-
base.Update();
46-
if (vrController != null)
47-
if (vrController.triggerValue > 0.9f)
47+
if (_vrPointer.vrController != null)
48+
if (_vrPointer.vrController.triggerValue > 0.9f)
4849
{
4950
if (_grabbingController != null) return;
50-
if (Physics.Raycast(vrController.position, vrController.forward, out var hit, _defaultLaserPointerLength))
51+
if (Physics.Raycast(_vrPointer.vrController.position, _vrPointer.vrController.forward, out var hit, MaxLaserDistance))
5152
{
5253
if (hit.transform != _cameraCube) return;
53-
_grabbingController = vrController;
54-
_grabPos = vrController.transform.InverseTransformPoint(_cameraCube.position);
55-
_grabRot = Quaternion.Inverse(vrController.transform.rotation) * _cameraCube.rotation;
54+
_grabbingController = _vrPointer.vrController;
55+
_grabPos = _vrPointer.vrController.transform.InverseTransformPoint(_cameraCube.position);
56+
_grabRot = Quaternion.Inverse(_vrPointer.vrController.transform.rotation) * _cameraCube.rotation;
5657
}
5758
}
5859

@@ -67,7 +68,7 @@ protected virtual void LateUpdate()
6768
if (_grabbingController != null)
6869
{
6970
var diff = _grabbingController.verticalAxisValue * Time.deltaTime;
70-
if (_grabPos.magnitude > MinDistance)
71+
if (_grabPos.magnitude > MinScrollDistance)
7172
{
7273
_grabPos -= Vector3.forward * diff;
7374
}

CameraPlus/CameraPlusBehaviour.cs

+11-30
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public bool ThirdPerson
4444
protected Material _previewMaterial;
4545
protected Camera _cam;
4646
protected Transform _cameraCube;
47-
protected float _lastRenderTime;
4847
protected ScreenCameraBehaviour _screenCamera;
4948
protected GameObject _cameraPreviewQuad;
5049

@@ -57,12 +56,10 @@ public virtual void Init(Camera mainCamera)
5756
{
5857
_mainCamera = mainCamera;
5958

60-
Camera.onPreCull += OnCameraPreCull;
61-
6259
XRSettings.showDeviceView = false;
6360

6461
Plugin.Instance.Config.ConfigChangedEvent += PluginOnConfigChangedEvent;
65-
SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
62+
SceneManager.sceneLoaded += SceneManagerOnSceneLoaded;
6663

6764
var gameObj = Instantiate(_mainCamera.gameObject);
6865
gameObj.SetActive(false);
@@ -88,7 +85,7 @@ public virtual void Init(Camera mainCamera)
8885

8986
_cam = gameObj.GetComponent<Camera>();
9087
_cam.stereoTargetEye = StereoTargetEyeMask.None;
91-
_cam.enabled = false;
88+
_cam.enabled = true;
9289

9390
gameObj.SetActive(true);
9491

@@ -130,33 +127,13 @@ public virtual void Init(Camera mainCamera)
130127
_cameraCube.eulerAngles = ThirdPersonRot;
131128
}
132129

133-
SceneManagerOnActiveSceneChanged(new Scene(), new Scene());
134-
}
135-
136-
protected virtual void OnCameraPreCull(Camera cam)
137-
{
138-
if (cam != _mainCamera) return;
139-
var currentTime = Time.realtimeSinceStartup;
140-
if (_lastRenderTime + (1 / Plugin.Instance.Config.fps) > currentTime) return;
141-
142-
if (Screen.width != _prevScreenWidth || Screen.height != _prevScreenHeight)
143-
{
144-
CreateScreenRenderTexture();
145-
}
146-
147-
if (_cam.targetTexture != null)
148-
{
149-
_cam.Render();
150-
}
151-
152-
_lastRenderTime = currentTime;
130+
SceneManagerOnSceneLoaded(new Scene(), LoadSceneMode.Single);
153131
}
154132

155133
protected virtual void OnDestroy()
156134
{
157135
Plugin.Instance.Config.ConfigChangedEvent -= PluginOnConfigChangedEvent;
158-
SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
159-
Camera.onPreCull -= OnCameraPreCull;
136+
SceneManager.sceneLoaded -= SceneManagerOnSceneLoaded;
160137
}
161138

162139
protected virtual void PluginOnConfigChangedEvent(Config config)
@@ -238,17 +215,21 @@ protected virtual void GetScaledScreenResolution(float scale, out int scaledWidt
238215
scaledHeight = Mathf.Clamp(Mathf.RoundToInt(scaledWidth * ratio), 1, int.MaxValue);
239216
}
240217

241-
protected virtual void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
218+
protected virtual void SceneManagerOnSceneLoaded(Scene scene, LoadSceneMode mode)
242219
{
243220
var pointer = Resources.FindObjectsOfTypeAll<VRPointer>().FirstOrDefault();
244221
if (pointer == null) return;
245-
var movePointer = (CameraMoverPointer) ReflectionUtil.CopyComponent(pointer, typeof(CameraMoverPointer), pointer.gameObject);
246-
DestroyImmediate(pointer);
222+
var movePointer = pointer.gameObject.AddComponent<CameraMoverPointer>();
247223
movePointer.Init(this, _cameraCube);
248224
}
249225

250226
protected virtual void LateUpdate()
251227
{
228+
if (Screen.width != _prevScreenWidth || Screen.height != _prevScreenHeight)
229+
{
230+
CreateScreenRenderTexture();
231+
}
232+
252233
var camera = _mainCamera.transform;
253234

254235
if (ThirdPerson)

CameraPlus/Config.cs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class Config
88
{
99
public string FilePath { get; }
1010
public float fov = 90;
11-
public float fps = 60;
1211
public int antiAliasing = 2;
1312
public float renderScale = 1;
1413
public float positionSmooth = 10;

CameraPlus/Plugin.cs

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using System.Collections;
23
using System.IO;
3-
using System.Linq;
44
using IllusionPlugin;
55
using UnityEngine;
66
using UnityEngine.SceneManagement;
@@ -11,13 +11,14 @@ namespace CameraPlus
1111
public class Plugin : IPlugin
1212
{
1313
public readonly Config Config = new Config(Path.Combine(Environment.CurrentDirectory, "cameraplus.cfg"));
14+
private readonly WaitForSecondsRealtime _waitForSecondsRealtime = new WaitForSecondsRealtime(0.1f);
1415

1516
private CameraPlusBehaviour _cameraPlus;
1617
private bool _init;
1718

1819
public static Plugin Instance { get; private set; }
1920
public string Name => "CameraPlus";
20-
public string Version => "v2.0.0";
21+
public string Version => "v2.0.1";
2122

2223
public void OnApplicationStart()
2324
{
@@ -26,12 +27,12 @@ public void OnApplicationStart()
2627

2728
Instance = this;
2829

29-
SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
30+
SceneManager.sceneLoaded += SceneManagerOnSceneLoaded;
3031
}
3132

3233
public void OnApplicationQuit()
3334
{
34-
SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
35+
SceneManager.sceneLoaded -= SceneManagerOnSceneLoaded;
3536
Config.Save();
3637
}
3738

@@ -51,13 +52,23 @@ public void OnFixedUpdate()
5152
{
5253
}
5354

54-
private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
55+
private void SceneManagerOnSceneLoaded(Scene scene, LoadSceneMode mode)
5556
{
56-
if (scene.buildIndex < 1) return;
57+
SharedCoroutineStarter.instance.StartCoroutine(DelayedOnSceneLoaded(scene));
58+
}
59+
60+
private IEnumerator DelayedOnSceneLoaded(Scene scene)
61+
{
62+
yield return _waitForSecondsRealtime;
63+
64+
if (scene.buildIndex < 1) yield break;
5765
if (_cameraPlus != null) Object.Destroy(_cameraPlus.gameObject);
5866

59-
var mainCamera = Object.FindObjectsOfType<Camera>().FirstOrDefault(x => x.CompareTag("MainCamera"));
60-
if (mainCamera == null) return;
67+
var mainCamera = Camera.main;
68+
if (mainCamera == null)
69+
{
70+
yield break;
71+
}
6172

6273
var gameObj = new GameObject("CameraPlus");
6374
_cameraPlus = gameObj.AddComponent<CameraPlusBehaviour>();

CameraPlus/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.2")]
35-
[assembly: AssemblyFileVersion("1.2")]
34+
[assembly: AssemblyVersion("2.0.1.0")]
35+
[assembly: AssemblyFileVersion("2.0.1.0")]

CameraPlus/ScreenCameraBehaviour.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
namespace CameraPlus
45
{
@@ -21,14 +22,14 @@ private void Awake()
2122
_cam = gameObject.AddComponent<Camera>();
2223
_cam.clearFlags = CameraClearFlags.Nothing;
2324
_cam.cullingMask = 0;
24-
_cam.depth = 1000;
25+
_cam.depth = -1000;
2526
_cam.stereoTargetEye = StereoTargetEyeMask.None;
2627
}
2728

2829
private void OnRenderImage(RenderTexture src, RenderTexture dest)
2930
{
3031
if (_renderTexture == null) return;
31-
Graphics.Blit(_renderTexture, (RenderTexture) null);
32+
Graphics.Blit(_renderTexture, dest);
3233
}
3334
}
3435
}

CameraPlus/SteamVRCompatibility.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class SteamVRCompatibility
1111

1212
private static bool FindSteamVRAsset()
1313
{
14-
SteamVRCamera = Type.GetType("SteamVR_Camera", false);
15-
SteamVRExternalCamera = Type.GetType("SteamVR_ExternalCamera", false);
16-
SteamVRFade = Type.GetType("SteamVR_Fade", false);
14+
SteamVRCamera = Type.GetType("SteamVR_Camera, Assembly-CSharp-firstpass", false);
15+
SteamVRExternalCamera = Type.GetType("SteamVR_ExternalCamera, Assembly-CSharp-firstpass", false);
16+
SteamVRFade = Type.GetType("SteamVR_Fade, Assembly-CSharp-firstpass", false);
1717
return SteamVRCamera != null && SteamVRExternalCamera != null && SteamVRFade != null;
1818
}
1919
}

0 commit comments

Comments
 (0)