Skip to content

Commit

Permalink
Use freelook camera instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaManatsu committed Aug 20, 2019
1 parent ff952e5 commit 96863e4
Show file tree
Hide file tree
Showing 13 changed files with 1,068 additions and 322 deletions.
1,168 changes: 1,021 additions & 147 deletions Assets/Scenes/MikuMikuPreview.unity

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Assets/Scripts/ButtonMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityTemplateProjects;

public class ButtonMethods : MonoBehaviour
{
Expand Down Expand Up @@ -40,7 +39,6 @@ public void SaveRenderTexture()
public void HideButton()
{
StartCoroutine(SetCanvasAlpha(canvas.alpha, 0, 0.5f));
camera.GetComponent<SimpleCameraController>().enabled = true;
}

private IEnumerator SetCanvasAlpha(float oldValue, float newValue, float duration)
Expand Down
30 changes: 30 additions & 0 deletions Assets/Scripts/CameraInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

public class CameraInput : MonoBehaviour
{
public GameObject camera;
public CanvasGroup canvas;

private IObservable<bool> IsUIEnable { get; set; }

// Start is called before the first frame update
void Awake()
{
IsUIEnable = this.FixedUpdateAsObservable()
.Select(x => canvas.alpha == 0 ? false : true);

IsUIEnable.DistinctUntilChanged()
.Subscribe(x => camera.SetActive(x ? false : true));

#if UNITY_EDITOR

IsUIEnable.DistinctUntilChanged()
.Subscribe(x => Debug.Log(x));

#endif
}

}
11 changes: 11 additions & 0 deletions Assets/Scripts/CameraInput.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
168 changes: 0 additions & 168 deletions Assets/Scripts/SimpleCameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,113 +5,8 @@ namespace UnityTemplateProjects
{
public class SimpleCameraController : MonoBehaviour
{
class CameraState
{
public float yaw;
public float pitch;
public float roll;
public float x;
public float y;
public float z;

public void SetFromTransform(Transform t)
{
pitch = t.eulerAngles.x;
yaw = t.eulerAngles.y;
roll = t.eulerAngles.z;
x = t.position.x;
y = t.position.y;
z = t.position.z;
}

public void Translate(Vector3 translation)
{
Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;

x += rotatedTranslation.x;
y += rotatedTranslation.y;
z += rotatedTranslation.z;
}

public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
{
yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);

x = Mathf.Lerp(x, target.x, positionLerpPct);
y = Mathf.Lerp(y, target.y, positionLerpPct);
z = Mathf.Lerp(z, target.z, positionLerpPct);
}

public void UpdateTransform(Transform t)
{
t.eulerAngles = new Vector3(pitch, yaw, roll);
t.position = new Vector3(x, y, z);
}
}

CameraState m_TargetCameraState = new CameraState();
CameraState m_InterpolatingCameraState = new CameraState();

[Header("Movement Settings")]
[Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
public float boost = 3.5f;

[Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
public float positionLerpTime = 0.2f;

[Header("Rotation Settings")]
[Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));

[Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
public float rotationLerpTime = 0.01f;

[Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
public bool invertY = false;

public CanvasGroup canvas;

public GameObject cameraPivot;

void OnEnable()
{
m_TargetCameraState.SetFromTransform(transform);
m_InterpolatingCameraState.SetFromTransform(transform);
}

Vector3 GetInputTranslationDirection()
{
Vector3 direction = new Vector3();
if (Input.GetKey(KeyCode.W))
{
direction += Vector3.forward;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector3.right;
}
if (Input.GetKey(KeyCode.Q))
{
direction += Vector3.down;
}
if (Input.GetKey(KeyCode.E))
{
direction += Vector3.up;
}

return direction;
}

public IEnumerator ResetCanvasAlpha(float oldValue, float newValue, float duration)
{
for (float t = 0f; t < duration; t += Time.deltaTime)
Expand All @@ -124,72 +19,9 @@ public IEnumerator ResetCanvasAlpha(float oldValue, float newValue, float durati

void Update()
{
// Exit Sample

if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
// Hide and lock cursor when right mouse button pressed
if (Input.GetMouseButtonDown(1))
{
Cursor.lockState = CursorLockMode.Locked;
}

// Unlock and show cursor when right mouse button released
if (Input.GetMouseButtonUp(1))
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}

// Rotation
if (Input.GetMouseButton(1))
{
var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));

var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);

m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
}

// Translation
var translation = GetInputTranslationDirection() * Time.deltaTime;

// Speed up movement when shift key held
if (Input.GetKey(KeyCode.LeftShift))
{
translation *= 10.0f;
}

// Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
boost += Input.mouseScrollDelta.y * 0.2f;
translation *= Mathf.Pow(2.0f, boost);

m_TargetCameraState.Translate(translation);

// Framerate-independent interpolation
// Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

m_InterpolatingCameraState.UpdateTransform(transform);

if (Input.GetKey(KeyCode.U))
{
StartCoroutine(ResetCanvasAlpha(canvas.alpha, 1, 0.5f));

enabled = false;
}

if (Input.GetKey(KeyCode.R))
{
m_TargetCameraState.SetFromTransform(cameraPivot.transform);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ private void Awake()
case var language when (language == SystemLanguage.Chinese) || (language == SystemLanguage.ChineseSimplified) || (language == SystemLanguage.ChineseTraditional):
FilesReadingText = @"正在从目录收集文件";
LoadingText = @"读取中";
TipText = @"游览模式下按"" U ""显示界面,按"" R ""重置相机";
TipText = @"游览模式下按"" U ""显示界面";
break;
default:
LoadingText = @"Loading";
FilesReadingText = @"Reading files";
TipText = @"Press ""U"" to show UI, "" R ""to reset camera";
TipText = @"Press ""U"" to show UI";
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Settings/VolumeSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
active: 1
skyType:
m_OverrideState: 0
m_OverrideState: 1
m_Value: 2
fogType:
m_OverrideState: 0
Expand Down
1 change: 1 addition & 0 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"com.unity.analytics": "3.2.2",
"com.unity.cinemachine": "2.2.9",
"com.unity.collab-proxy": "1.2.15",
"com.unity.package-manager-ui": "2.0.7",
"com.unity.postprocessing": "2.1.6",
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PlayerSettings:
useOnDemandResources: 0
accelerometerFrequency: 60
companyName: "Copyright \xA9 Lolisay Studio"
productName: MikuMikuPreview 1.9.5b
productName: MikuMikuPreview 2.0.1.1b
defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0}
m_SplashScreenBackgroundColor: {r: 0, g: 0, b: 0, a: 1}
Expand Down Expand Up @@ -120,7 +120,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 1.9.5b
bundleVersion: 2.0.1.1b
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down

0 comments on commit 96863e4

Please sign in to comment.