Skip to content

Commit 4b97f3c

Browse files
2 parents 2db10c0 + a746bdb commit 4b97f3c

5 files changed

Lines changed: 784 additions & 770 deletions

File tree

Assets/ScriptableRenderPipeline/common/Camera/CameraSwitcher.cs

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,109 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public class CameraSwitcher : MonoBehaviour
5+
namespace UnityEngine.Experimental.Rendering
66
{
7-
public Camera[] m_Cameras;
8-
9-
private int m_CurrentCameraIndex = -1;
10-
private Camera m_OriginalCamera = null;
11-
private Vector3 m_OriginalCameraPosition;
12-
private Quaternion m_OriginalCameraRotation;
13-
private Camera m_CurrentCamera = null;
7+
public class CameraSwitcher : MonoBehaviour
8+
{
9+
public Camera[] m_Cameras;
1410

15-
private float m_MessageDuration = 1.0f;
16-
private float m_MessageTimer = 1000.0f;
11+
private int m_CurrentCameraIndex = -1;
12+
private Camera m_OriginalCamera = null;
13+
private Vector3 m_OriginalCameraPosition;
14+
private Quaternion m_OriginalCameraRotation;
15+
private Camera m_CurrentCamera = null;
1716

18-
private static string kDebugNext = "Debug Next";
19-
private static string kDebugPrevious = "Debug Previous";
20-
private string[] m_RequiredInputButtons = { kDebugNext, kDebugPrevious };
21-
private bool m_Valid = true;
17+
private float m_MessageDuration = 1.0f;
18+
private float m_MessageTimer = 1000.0f;
2219

23-
void OnEnable()
24-
{
25-
m_OriginalCamera = GetComponent<Camera>();
26-
m_CurrentCamera = m_OriginalCamera;
27-
28-
m_Valid = Debugging.CheckRequiredInputButtonMapping(m_RequiredInputButtons);
29-
}
20+
private static string kDebugNext = "Debug Next";
21+
private static string kDebugPrevious = "Debug Previous";
22+
private string[] m_RequiredInputButtons = { kDebugNext, kDebugPrevious };
23+
private bool m_Valid = true;
3024

31-
int GetCameraCount()
32-
{
33-
return m_Cameras.Length + 1; // We need +1 for handling the original camera.
34-
}
25+
void OnEnable()
26+
{
27+
m_OriginalCamera = GetComponent<Camera>();
28+
m_CurrentCamera = m_OriginalCamera;
3529

36-
void NextCamera()
37-
{
38-
m_CurrentCameraIndex = (m_CurrentCameraIndex + 1) % GetCameraCount();
39-
}
30+
m_Valid = Debugging.CheckRequiredInputButtonMapping(m_RequiredInputButtons);
31+
}
4032

41-
void PreviousCamera()
42-
{
43-
m_CurrentCameraIndex = m_CurrentCameraIndex - 1;
44-
if (m_CurrentCameraIndex == -1)
45-
m_CurrentCameraIndex = m_Cameras.Length;
46-
}
33+
int GetCameraCount()
34+
{
35+
return m_Cameras.Length + 1; // We need +1 for handling the original camera.
36+
}
4737

48-
Camera GetNextCamera()
49-
{
50-
if (m_CurrentCameraIndex == m_Cameras.Length)
51-
return m_OriginalCamera;
52-
else
53-
return m_Cameras[m_CurrentCameraIndex];
54-
}
38+
void NextCamera()
39+
{
40+
m_CurrentCameraIndex = (m_CurrentCameraIndex + 1) % GetCameraCount();
41+
}
5542

56-
void Update()
57-
{
58-
if (m_Valid && Debugging.debugControlEnabled && m_OriginalCamera != null)
43+
void PreviousCamera()
5944
{
60-
m_MessageTimer += Time.deltaTime;
61-
bool needUpdateCamera = false;
62-
if (Input.GetButtonDown(kDebugNext))
63-
{
64-
NextCamera();
65-
needUpdateCamera = true;
66-
}
45+
m_CurrentCameraIndex = m_CurrentCameraIndex - 1;
46+
if (m_CurrentCameraIndex == -1)
47+
m_CurrentCameraIndex = m_Cameras.Length;
48+
}
6749

68-
if (Input.GetButtonDown(kDebugPrevious))
69-
{
70-
PreviousCamera();
71-
needUpdateCamera = true;
72-
}
50+
Camera GetNextCamera()
51+
{
52+
if (m_CurrentCameraIndex == m_Cameras.Length)
53+
return m_OriginalCamera;
54+
else
55+
return m_Cameras[m_CurrentCameraIndex];
56+
}
7357

74-
if (needUpdateCamera)
58+
void Update()
59+
{
60+
if (m_Valid && Debugging.debugControlEnabled && m_OriginalCamera != null)
7561
{
76-
m_MessageTimer = 0.0f;
77-
78-
if(m_CurrentCamera == m_OriginalCamera)
62+
m_MessageTimer += Time.deltaTime;
63+
bool needUpdateCamera = false;
64+
if (Input.GetButtonDown(kDebugNext))
7965
{
80-
m_OriginalCameraPosition = m_OriginalCamera.transform.position;
81-
m_OriginalCameraRotation = m_OriginalCamera.transform.rotation;
66+
NextCamera();
67+
needUpdateCamera = true;
8268
}
8369

84-
m_CurrentCamera = GetNextCamera();
70+
if (Input.GetButtonDown(kDebugPrevious))
71+
{
72+
PreviousCamera();
73+
needUpdateCamera = true;
74+
}
8575

86-
if(m_CurrentCamera != null)
76+
if (needUpdateCamera)
8777
{
88-
// If we witch back to the original camera, put back the transform in it.
89-
if (m_CurrentCamera == m_OriginalCamera)
78+
m_MessageTimer = 0.0f;
79+
80+
if(m_CurrentCamera == m_OriginalCamera)
9081
{
91-
m_OriginalCamera.transform.position = m_OriginalCameraPosition;
92-
m_OriginalCamera.transform.rotation = m_OriginalCameraRotation;
82+
m_OriginalCameraPosition = m_OriginalCamera.transform.position;
83+
m_OriginalCameraRotation = m_OriginalCamera.transform.rotation;
9384
}
9485

95-
transform.position = m_CurrentCamera.transform.position;
96-
transform.rotation = m_CurrentCamera.transform.rotation;
86+
m_CurrentCamera = GetNextCamera();
87+
88+
if(m_CurrentCamera != null)
89+
{
90+
// If we witch back to the original camera, put back the transform in it.
91+
if (m_CurrentCamera == m_OriginalCamera)
92+
{
93+
m_OriginalCamera.transform.position = m_OriginalCameraPosition;
94+
m_OriginalCamera.transform.rotation = m_OriginalCameraRotation;
95+
}
96+
97+
transform.position = m_CurrentCamera.transform.position;
98+
transform.rotation = m_CurrentCamera.transform.rotation;
99+
}
97100
}
98-
}
99101

100-
if (m_MessageTimer < m_MessageDuration)
101-
{
102-
string cameraName = m_CurrentCamera != null ? m_CurrentCamera.name : "NULL";
103-
string message = string.Format("Switching to camera : {0}", cameraName);
104-
Debugging.PushDebugMessage(message);
102+
if (m_MessageTimer < m_MessageDuration)
103+
{
104+
string cameraName = m_CurrentCamera != null ? m_CurrentCamera.name : "NULL";
105+
string message = string.Format("Switching to camera : {0}", cameraName);
106+
Debugging.PushDebugMessage(message);
107+
}
105108
}
106109
}
107110
}

Assets/ScriptableRenderPipeline/common/Camera/FreeCamera.cs

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,68 @@
11
using UnityEngine;
22

3-
public class FreeCamera : MonoBehaviour
3+
namespace UnityEngine.Experimental.Rendering
44
{
5-
public float m_LookSpeedController = 120f;
6-
public float m_LookSpeedMouse = 10.0f;
7-
public float m_MoveSpeed = 50.0f;
8-
public float m_Turbo = 10.0f;
9-
5+
public class FreeCamera : MonoBehaviour
6+
{
7+
public float m_LookSpeedController = 120f;
8+
public float m_LookSpeedMouse = 10.0f;
9+
public float m_MoveSpeed = 50.0f;
10+
public float m_Turbo = 10.0f;
1011

11-
private static string kMouseX = "Mouse X";
12-
private static string kMouseY = "Mouse Y";
13-
private static string kRightStickX = "Controller Right Stick X";
14-
private static string kRightStickY = "Controller Right Stick Y";
15-
private static string kVertical = "Vertical";
16-
private static string kHorizontal = "Horizontal";
17-
private string[] m_RequiredInputAxes = { kMouseX, kMouseY, kRightStickX, kRightStickY, kVertical, kHorizontal };
12+
private static string kMouseX = "Mouse X";
13+
private static string kMouseY = "Mouse Y";
14+
private static string kRightStickX = "Controller Right Stick X";
15+
private static string kRightStickY = "Controller Right Stick Y";
16+
private static string kVertical = "Vertical";
17+
private static string kHorizontal = "Horizontal";
18+
private string[] m_RequiredInputAxes = { kMouseX, kMouseY, kRightStickX, kRightStickY, kVertical, kHorizontal };
1819

19-
private bool m_Valid = true;
20-
void OnEnable()
21-
{
22-
m_Valid = Debugging.CheckRequiredInputAxisMapping(m_RequiredInputAxes);
23-
}
20+
private bool m_Valid = true;
21+
void OnEnable()
22+
{
23+
m_Valid = Debugging.CheckRequiredInputAxisMapping(m_RequiredInputAxes);
24+
}
2425

25-
void Update()
26-
{
27-
if(m_Valid)
26+
void Update()
2827
{
29-
float inputRotateAxisX = 0.0f;
30-
float inputRotateAxisY = 0.0f;
31-
if (Input.GetMouseButton(1))
28+
if(m_Valid)
3229
{
33-
inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
34-
inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
35-
}
36-
inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * Time.deltaTime);
37-
inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * Time.deltaTime);
30+
float inputRotateAxisX = 0.0f;
31+
float inputRotateAxisY = 0.0f;
32+
if (Input.GetMouseButton(1))
33+
{
34+
inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
35+
inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
36+
}
37+
inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * Time.deltaTime);
38+
inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * Time.deltaTime);
3839

39-
float inputVertical = Input.GetAxis(kVertical);
40-
float inputHorizontal = Input.GetAxis(kHorizontal);
40+
float inputVertical = Input.GetAxis(kVertical);
41+
float inputHorizontal = Input.GetAxis(kHorizontal);
4142

42-
bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f;
43-
if (moved)
44-
{
45-
float rotationX = transform.localEulerAngles.x;
46-
float newRotationY = transform.localEulerAngles.y + inputRotateAxisX;
43+
bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f;
44+
if (moved)
45+
{
46+
float rotationX = transform.localEulerAngles.x;
47+
float newRotationY = transform.localEulerAngles.y + inputRotateAxisX;
4748

48-
// Weird clamping code due to weird Euler angle mapping...
49-
float newRotationX = (rotationX - inputRotateAxisY);
50-
if (rotationX <= 90.0f && newRotationX >= 0.0f)
51-
newRotationX = Mathf.Clamp(newRotationX, 0.0f, 90.0f);
52-
if (rotationX >= 270.0f)
53-
newRotationX = Mathf.Clamp(newRotationX, 270.0f, 360.0f);
49+
// Weird clamping code due to weird Euler angle mapping...
50+
float newRotationX = (rotationX - inputRotateAxisY);
51+
if (rotationX <= 90.0f && newRotationX >= 0.0f)
52+
newRotationX = Mathf.Clamp(newRotationX, 0.0f, 90.0f);
53+
if (rotationX >= 270.0f)
54+
newRotationX = Mathf.Clamp(newRotationX, 270.0f, 360.0f);
5455

55-
transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);
56+
transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);
5657

57-
float moveSpeed = Time.deltaTime * m_MoveSpeed;
58-
if (Input.GetMouseButton(1))
59-
moveSpeed *= Input.GetKey(KeyCode.LeftShift) ? m_Turbo : 1.0f;
60-
else
61-
moveSpeed *= Input.GetAxis("Fire1") > 0.0f ? m_Turbo : 1.0f;
62-
transform.position += transform.forward * moveSpeed * inputVertical;
63-
transform.position += transform.right * moveSpeed * inputHorizontal;
58+
float moveSpeed = Time.deltaTime * m_MoveSpeed;
59+
if (Input.GetMouseButton(1))
60+
moveSpeed *= Input.GetKey(KeyCode.LeftShift) ? m_Turbo : 1.0f;
61+
else
62+
moveSpeed *= Input.GetAxis("Fire1") > 0.0f ? m_Turbo : 1.0f;
63+
transform.position += transform.forward * moveSpeed * inputVertical;
64+
transform.position += transform.right * moveSpeed * inputHorizontal;
65+
}
6466
}
6567
}
6668
}

0 commit comments

Comments
 (0)