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

Commit

Permalink
Added third person camera represented with a cube in-game.
Browse files Browse the repository at this point in the history
  • Loading branch information
xyonico committed May 24, 2018
1 parent 9a3c096 commit 4ccc21d
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 121 deletions.
72 changes: 72 additions & 0 deletions CameraPlus/CameraMoverPointer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using UnityEngine;
using VRUIControls;

namespace CameraPlus
{
public class CameraMoverPointer : VRPointer
{
private Transform _grabbedCamera;
private VRController _grabbingController;
private Vector3 _grabPos;
private Quaternion _grabRot;
private Vector3 _realPos;
private Quaternion _realRot;

public override void Update()
{
base.Update();
if (vrController != null)
if (vrController.triggerValue > 0.9f)
{
if (_grabbingController != null) return;
RaycastHit hit;
if (Physics.Raycast(vrController.position, vrController.forward, out hit, _defaultLaserPointerLength))
{
if (hit.transform.name != "CameraCube") return;
_grabbedCamera = hit.transform;
_grabbingController = vrController;
_grabPos = vrController.transform.InverseTransformPoint(_grabbedCamera.position);
_grabRot = Quaternion.Inverse(vrController.transform.rotation) * _grabbedCamera.rotation;
}
}

if (_grabbingController == null || !(_grabbingController.triggerValue <= 0.9f)) return;
if (_grabbingController == null) return;
SaveToIni();
_grabbingController = null;
}

private void LateUpdate()
{
if (_grabbedCamera == null) return;
if (_grabbingController != null)
{
_realPos = _grabbingController.transform.TransformPoint(_grabPos);
_realRot = _grabbingController.transform.rotation * _grabRot;
}

_grabbedCamera.position = Vector3.Lerp(_grabbedCamera.position, _realPos,
CameraPlusBehaviour.PosSmooth * Time.deltaTime);
_grabbedCamera.rotation = Quaternion.Slerp(_grabbedCamera.rotation, _realRot,
CameraPlusBehaviour.RotSmooth * Time.deltaTime);

CameraPlusBehaviour.ThirdPersonPos = _grabbedCamera.position;
CameraPlusBehaviour.ThirdPersonRot = _grabbedCamera.rotation;
}

private void SaveToIni()
{
var ini = Plugin.Ini;
var pos = _grabbedCamera.position;
var rot = _grabbedCamera.rotation;
ini.WriteValue("posx", pos.x.ToString());
ini.WriteValue("posy", pos.y.ToString());
ini.WriteValue("posz", pos.z.ToString());
ini.WriteValue("rotx", rot.x.ToString());
ini.WriteValue("roty", rot.y.ToString());
ini.WriteValue("rotz", rot.z.ToString());
ini.WriteValue("rotw", rot.w.ToString());
}
}
}
18 changes: 11 additions & 7 deletions CameraPlus/CameraPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,30 @@
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.Networking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.Networking.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UnityWebRequestModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.UnityWebRequestModule.dll</HintPath>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UIElementsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.UIElementsModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UnityWebRequestWWWModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.UnityWebRequestWWWModule.dll</HintPath>
<Reference Include="UnityEngine.UIModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.UIModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.VRModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>D:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.VRModule.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CameraMoverPointer.cs" />
<Compile Include="Ini.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
140 changes: 92 additions & 48 deletions CameraPlus/CameraPlusBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.SceneManagement;
using VRUIControls;

namespace CameraPlus
{
public class CameraPlusBehaviour : MonoBehaviour
{
public static Camera MainCamera;
public static float FOV;
public static float PosSmooth;
public static float RotSmooth;

public static bool ThirdPerson;
public static Vector3 ThirdPersonPos;
public static Quaternion ThirdPersonRot;
private static RenderTexture _renderTexture;
private static Material _previewMaterial;
private Camera _cam;
private float _fov;
private float _posSmooth;
private float _rotSmooth;

private bool _fixed;
private Vector3 _fixedPos;
private Quaternion _fixedRot;

private Camera _previewCam;
private Transform _cameraCube;
private const int Width = 256;

private void Awake()
{
SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
SceneManagerOnActiveSceneChanged(new Scene(), new Scene());
var gameObj = Instantiate(MainCamera.gameObject);
gameObj.SetActive(false);
gameObj.name = "Camera Plus";
gameObj.tag = "Untagged";
while (gameObj.transform.childCount > 0)
{
DestroyImmediate(gameObj.transform.GetChild(0).gameObject);
}
while (gameObj.transform.childCount > 0) DestroyImmediate(gameObj.transform.GetChild(0).gameObject);
DestroyImmediate(gameObj.GetComponent("CameraRenderCallbacksManager"));
DestroyImmediate(gameObj.GetComponent("AudioListener"));
DestroyImmediate(gameObj.GetComponent("MeshCollider"));
Expand All @@ -39,66 +45,104 @@ private void Awake()
_cam.stereoTargetEye = StereoTargetEyeMask.None;
_cam.targetTexture = null;
_cam.depth += 100;

gameObj.SetActive(true);

var camera = MainCamera.transform;
transform.position = camera.position;
transform.rotation = camera.rotation;

gameObj.transform.parent = transform;
gameObj.transform.localPosition = Vector3.zero;
gameObj.transform.localRotation = Quaternion.identity;
gameObj.transform.localScale = Vector3.one;

var cameraCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cameraCube.SetActive(ThirdPerson);
_cameraCube = cameraCube.transform;
_cameraCube.localScale = new Vector3(0.15f, 0.15f, 0.22f);
_cameraCube.name = "CameraCube";

_previewCam = Instantiate(_cam.gameObject, _cameraCube).GetComponent<Camera>();

if (_renderTexture == null && _previewMaterial == null)
{
_renderTexture = new RenderTexture(Width, (int) (Width / _cam.aspect), 24);
_previewMaterial = new Material(Shader.Find("Hidden/BlitCopyWithDepth"));
_previewMaterial.SetTexture("_MainTex", _renderTexture);
}

_previewCam.targetTexture = _renderTexture;

var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
DestroyImmediate(quad.GetComponent<Collider>());
quad.GetComponent<MeshRenderer>().material = _previewMaterial;
quad.transform.parent = _cameraCube;
quad.transform.localPosition = new Vector3(-1f * ((_cam.aspect - 1) / 2 + 1), 0, 0.22f);
quad.transform.localEulerAngles = new Vector3(0, 180, 0);
quad.transform.localScale = new Vector3(-1 * _cam.aspect, 1, 1);

ReadIni();
}

private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
{
var pointer = Resources.FindObjectsOfTypeAll<VRPointer>().FirstOrDefault();
if (pointer == null) return;
ReflectionUtil.CopyComponent(pointer, typeof(CameraMoverPointer), pointer.gameObject);
DestroyImmediate(pointer);
}

public void ReadIni()
{
_fov = Convert.ToSingle(Plugin.Ini.GetValue("fov", "", "90"));
_posSmooth = Convert.ToSingle(Plugin.Ini.GetValue("positionSmooth", "", "10"));
_rotSmooth = Convert.ToSingle(Plugin.Ini.GetValue("rotationSmooth", "", "5"));

_fixed = Convert.ToBoolean(Plugin.Ini.GetValue("fixed", "", "false"));
_fixedPos = new Vector3(
Convert.ToSingle(Plugin.Ini.GetValue("posx", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("posy", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("posz", "", "0"))
);

_fixedRot = new Quaternion(
Convert.ToSingle(Plugin.Ini.GetValue("rotx", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("roty", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("rotz", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("rotw", "", "0"))
);
FOV = Convert.ToSingle(Plugin.Ini.GetValue("fov", "", "90"));
PosSmooth = Convert.ToSingle(Plugin.Ini.GetValue("positionSmooth", "", "10"));
RotSmooth = Convert.ToSingle(Plugin.Ini.GetValue("rotationSmooth", "", "5"));

ThirdPerson = Convert.ToBoolean(Plugin.Ini.GetValue("thirdPerson", "", "false"));
_cameraCube.gameObject.SetActive(ThirdPerson);
ThirdPersonPos = new Vector3(
Convert.ToSingle(Plugin.Ini.GetValue("posx", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("posy", "", "2")),
Convert.ToSingle(Plugin.Ini.GetValue("posz", "", "-1"))
);

ThirdPersonRot = new Quaternion(
Convert.ToSingle(Plugin.Ini.GetValue("rotx", "", "0.25")),
Convert.ToSingle(Plugin.Ini.GetValue("roty", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("rotz", "", "0")),
Convert.ToSingle(Plugin.Ini.GetValue("rotw", "", "1"))
);

SetFOV();
}

private void LateUpdate()
{
var camera = MainCamera.transform;

if (_fixed)
{
transform.position = _fixedPos;
transform.rotation = _fixedRot;
return;
}
private void LateUpdate()
{
var camera = MainCamera.transform;

transform.position = Vector3.Lerp(transform.position, camera.position, _posSmooth * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, camera.rotation, _rotSmooth * Time.deltaTime);
if (ThirdPerson)
{
transform.position = ThirdPersonPos;
transform.rotation = ThirdPersonRot;
_cameraCube.position = ThirdPersonPos;
_cameraCube.rotation = ThirdPersonRot;
return;
}

}
transform.position = Vector3.Lerp(transform.position, camera.position, PosSmooth * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, camera.rotation, RotSmooth * Time.deltaTime);
}

private void SetFOV()
{
if (_cam == null) return;
_cam.fieldOfView = (float) (57.2957801818848 *
(2.0 * Mathf.Atan(Mathf.Tan((float) (_fov * (Math.PI / 180.0) * 0.5)) /
MainCamera.aspect)));
var fov = (float) (57.2957801818848 *
(2.0 * Mathf.Atan(Mathf.Tan((float) (FOV * (Math.PI / 180.0) * 0.5)) /
MainCamera.aspect)));
_cam.fieldOfView = fov;
if (_previewCam == null) return;
_previewCam.fieldOfView = fov;
}
}
}
Loading

0 comments on commit 4ccc21d

Please sign in to comment.