Skip to content

Commit

Permalink
v1.1 - added camera movement script panel
Browse files Browse the repository at this point in the history
  • Loading branch information
shdw9 committed May 23, 2023
1 parent 1d1a679 commit 07aa318
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 6 deletions.

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

107 changes: 107 additions & 0 deletions Editor/Scripts/Panels/CamControl Panel/AttachCamController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEditor.PackageManager;

using Unity.Formats.USD;

public class AttachCamController : EditorWindow
{
public static DropdownField m_dropDown;
public static DropdownField m_orientationDropdown;
private static List<string> listOfCameras = new List<string>();

[MenuItem("USD/Previsualization Panels/Add Camera Controller", priority = 130)]
public static void FullDomeView()
{
EditorWindow wnd = GetWindow<AttachCamController>();
wnd.titleContent = new GUIContent("[Previz] Camera Controller");

// this makes it so that its not resizable
wnd.minSize = new Vector2(300,140);
wnd.maxSize = new Vector2(300,140);

wnd.ShowPopup();
}

void CreateGUI() {
m_dropDown = new DropdownField();

foreach (Camera cam in FindObjectsOfType<Camera>())
{
listOfCameras.Add(cam.name);
m_dropDown.value = cam.name;
}

m_dropDown.choices = listOfCameras;

rootVisualElement.Add(new Label("\n Select a camera to attach controller script:"));
rootVisualElement.Add(m_dropDown);
rootVisualElement.Add(new Label(""));

var switchButton = new Button();
switchButton.text = "Switch to Camera";
switchButton.clicked += switchCamera;
rootVisualElement.Add(switchButton);

var addButton = new Button();
addButton.text = "Add Camera Controller Script";
addButton.clicked += addScript;
rootVisualElement.Add(addButton);

var removeButton = new Button();
removeButton.text = "Remove Camera Controller Script";
removeButton.clicked += removeScript;
rootVisualElement.Add(removeButton);

}

// add camera movement script
private void addScript() {
if (m_dropDown.value == null) {
return;
}

GameObject.Find(m_dropDown.value).AddComponent<CameraController>();

Debug.Log("[PREVIZ] Added the camera controller script to the " + m_dropDown.value + " camera!");
}

// remove camera movement script
private void removeScript() {
if (m_dropDown.value == null) {
return;
}

if (GameObject.Find(m_dropDown.value).GetComponent<CameraController>() != null) {
if (Application.isPlaying) {
Destroy(GameObject.Find(m_dropDown.value).GetComponent<CameraController>());
} else {
DestroyImmediate(GameObject.Find(m_dropDown.value).GetComponent<CameraController>());
}
Debug.Log("[PREVIZ] Removed the camera controller script from the " + m_dropDown.value + " camera!");
}
}

// switch to selected camera
private void switchCamera() {
foreach (string camname in listOfCameras)
{
if (camname == m_dropDown.value)
{
GameObject.Find(camname).GetComponent<Camera>().enabled = true;
}
else
{
GameObject.Find(camname).GetComponent<Camera>().enabled = false;
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Panels/CamControl Panel/AttachCamController.cs.meta

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

5 changes: 3 additions & 2 deletions Editor/Scripts/Panels/Fulldome Panel/AttachFulldome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static void FullDomeView()
wnd.titleContent = new GUIContent("[Previz] Fulldome Camera");

// this makes it so that its not resizable
wnd.minSize = new Vector2(300,200);
wnd.maxSize = new Vector2(300,200);
wnd.minSize = new Vector2(300,170);
wnd.maxSize = new Vector2(300,170);

wnd.ShowPopup();
}
Expand Down Expand Up @@ -119,6 +119,7 @@ private void removeFulldome() {
} else {
DestroyImmediate(GameObject.Find("Fulldome Camera"));
}
Debug.Log("[PREVIZ] Removed the fulldome camera script from " + m_dropDown.value + "!");

}

Expand Down
7 changes: 6 additions & 1 deletion Editor/Scripts/Timeline/ExportTimeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public static void ExportToJson()
if (path.Length != 0)
{
Debug.Log(timelineJson);
File.WriteAllText(path, JsonConvert.SerializeObject(JObject.Parse(timelineJson), Formatting.Indented));
try {
File.WriteAllText(path, JsonConvert.SerializeObject(JObject.Parse(timelineJson), Formatting.Indented));
} catch {
File.WriteAllText(path, timelineJson);
}

Debug.Log("[PREVIZ] Saved timeline information to " + path + "!");
}

Expand Down
7 changes: 7 additions & 0 deletions LICENSE.meta

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

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The following is a brief list of features of this addon:
* Toggle scene cameras to be seen through a fulldome lens
* To stream fulldome footage to the dome, see [fulldome docs](https://github.com/shdw9/fulldome_previz_plugin/tree/main/Documentation~/Fulldome.md)
* Utilizies Open Source Project: [FulldomeCameraForUnity](https://github.com/rsodre/FulldomeCameraForUnity)
* Camera Movement Script
* Control scene cameras with controls in the play scene
* Customizable settings *(smoothing, sensitivity, camera speed)*
* Export Timeline Information
* Save timeline changes to a JSON file
* Allows you to import that information to different Digital Content Creation tools
Expand Down
7 changes: 7 additions & 0 deletions README.md.meta

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

8 changes: 8 additions & 0 deletions Runtime/CameraControllerScript.meta

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

114 changes: 114 additions & 0 deletions Runtime/CameraControllerScript/CameraController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using UnityEngine;

// Daniel Flanigan, 2014
// This is a combined mouse look and camera move script.
// The cam move script is by: Francis R. Griffiths-Keam
// original source: https://forum.unity.com/threads/combined-camera-movement-and-mouse-look-script.283290/

public class CameraController : MonoBehaviour
{
Vector2 _mouseAbsolute;
Vector2 _smoothMouse;
[Space(20)]
[Header("Mouse Look Settings :")]
public Vector2
clampInDegrees = new Vector2(360, 180);
public bool lockCursor;
public Vector2 sensitivity = new Vector2(2, 2);
public Vector2 smoothing = new Vector2(3, 3);
public Vector2 targetDirection;
public Vector2 targetCharacterDirection;

// Assign this if there's a parent object controlling motion, such as a Character Controller.
// Yaw rotation will affect this object instead of the camera if set.
public GameObject characterBody;

[Space(20)]
[Header("Camera Move Settings :")]

public float speed = 5.0f;

public KeyCode fwdKey = KeyCode.W;
public KeyCode leftKey = KeyCode.A;
public KeyCode backKey = KeyCode.S;
public KeyCode rightKey = KeyCode.D;

Animator animator;

void Start()
{
// Set target direction to the camera's initial orientation.
targetDirection = transform.localRotation.eulerAngles;

// Set target direction for the character body to its inital state.
if (characterBody)
targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
}

void Update()
{
// Ensure the cursor is always locked when set
Screen.lockCursor = lockCursor;

// Allow the script to clamp based on a desired target value.
var targetOrientation = Quaternion.Euler(targetDirection);
var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

// Get raw mouse input for a cleaner reading on more sensitive mice.
var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

// Scale input against the sensitivity setting and multiply that against the smoothing value.
mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

// Interpolate mouse movement over time to apply smoothing delta.
_smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

// Find the absolute mouse movement value from point zero.
_mouseAbsolute += _smoothMouse;

// Clamp and apply the local x value first, so as not to be affected by world transforms.
if (clampInDegrees.x < 360)
_mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;

// Then clamp and apply the global y value.
if (clampInDegrees.y < 360)
_mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

transform.localRotation *= targetOrientation;

// If there's a character body that acts as a parent to the camera
if (characterBody)
{
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
characterBody.transform.localRotation = yRotation;
characterBody.transform.localRotation *= targetCharacterOrientation;
}
else
{
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
transform.localRotation *= yRotation;
}

if (Input.GetKey(rightKey))
{
transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
}
if (Input.GetKey(leftKey))
{
transform.Translate(new Vector3(-speed * Time.deltaTime, 0, 0));
}
if (Input.GetKey(backKey))
{
transform.Translate(new Vector3(0, 0, -speed * Time.deltaTime));
}
if (Input.GetKey(fwdKey))
{
transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
}

}
}
11 changes: 11 additions & 0 deletions Runtime/CameraControllerScript/CameraController.cs.meta

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "com.sfsucsc631team3b.fulldomepreviz",
"version": "1.0.0",
"version": "1.1.0",
"displayName": "Fulldome Previsualization",
"description": "SFSU CSC 631 2023 - Team 3B Fulldome Previsualization Plugin in Unity\n\nThe Fulldome Previsualization Plugin modifies the USD menu to allow you to: \n - Import USD files with animation clips\n - Export USD files with modified animations\n - Add a fulldome camera rig to any specified camera in the scene\n - Export timeline information to a JSON file\n\nUnity Version: Unity.2022.2.9f1",
"description": "SFSU CSC 631 2023 - Team 3B Fulldome Previsualization Plugin in Unity\n\nThe Fulldome Previsualization Plugin modifies the USD menu to allow you to: \n\n - Import USD files with animation clips\n - Export USD files with modified animations using Unity Recorder\n - Toggle scene cameras to be seen through a fisheye/fulldome lens\n - Control scene cameras with controls in the play scene\n - Export timeline information to a JSON file\n\nBased on a student project proposal by Jeroen Lapr\u00e9 for the Computer Science Department of San Francisco State University\n\nUnity Version: Unity.2022.2.9f1",
"unity": "2022.2",
"unityRelease": "9f1",
"documentationUrl": "https://github.com/shdw9/fulldome_previz_plugin/",
Expand Down

0 comments on commit 07aa318

Please sign in to comment.