Skip to content

Commit

Permalink
Provide better error messages if camera not found
Browse files Browse the repository at this point in the history
  • Loading branch information
mminer committed Jun 22, 2023
1 parent c496bd0 commit 00f51de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
62 changes: 42 additions & 20 deletions Editor/BigCameraInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ namespace UnityEditor.BigImageRecorder
/// </summary>
class BigCameraInput : RecorderInput
{
public bool HasCamera => camera != null;
public BigCameraInputSettings InputSettings => settings as BigCameraInputSettings;
public RenderTexture[,] OutputRenderTextures { get; private set; }

Camera camera;
Matrix4x4[,] projectionMatrices;

protected override void BeginRecording(RecordingSession session)
{
base.BeginRecording(session);
OutputRenderTextures = CreateOutputRenderTextures(InputSettings);
projectionMatrices = CreateProjectionMatrices(InputSettings);
camera = GetTargetCamera(InputSettings.CameraTag);
projectionMatrices = CreateProjectionMatrices(InputSettings, camera);
}

protected override void Dispose(bool disposing)
Expand All @@ -36,7 +39,12 @@ protected override void Dispose(bool disposing)
protected override void NewFrameReady(RecordingSession session)
{
base.NewFrameReady(session);
var camera = GetTargetCamera(InputSettings.CameraTag);

if (camera == null)
{
return;
}

var originalTargetTexture = camera.targetTexture;

for (var row = 0; row < InputSettings.Rows; row++)
Expand Down Expand Up @@ -70,10 +78,15 @@ protected override void NewFrameReady(RecordingSession session)
return outputRenderTextures;
}

static Matrix4x4[,] CreateProjectionMatrices(BigCameraInputSettings inputSettings)
static Matrix4x4[,] CreateProjectionMatrices(BigCameraInputSettings inputSettings, Camera camera)
{
if (camera == null)
{
return null;
}

var projectionMatrices = new Matrix4x4[inputSettings.Rows, inputSettings.Columns];
var camera = GetTargetCamera(inputSettings.CameraTag);
var nearClipPlane = camera.nearClipPlane;

// Values to create the original projection matrix.
// We multiply these by a modifier from -1 to 1 to get a partial projection matrix.
Expand All @@ -83,7 +96,7 @@ protected override void NewFrameReady(RecordingSession session)
// Left: left * 1, right * -1/3
// Center: left * 1/3, right * 1/3
// Right: left * -1/3, right * 1
var top = camera.nearClipPlane * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var top = nearClipPlane * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var bottom = -top;
var left = bottom * inputSettings.AspectRatio;
var right = top * inputSettings.AspectRatio;
Expand All @@ -103,9 +116,9 @@ protected override void NewFrameReady(RecordingSession session)
var tileRight = right * (-1 + 2 * horizontalTilePercent * (column + 1));

var projectionMatrix = camera.projectionMatrix;
projectionMatrix.m00 = 2 * camera.nearClipPlane / (tileRight - tileLeft);
projectionMatrix.m00 = 2 * nearClipPlane / (tileRight - tileLeft);
projectionMatrix.m02 = (tileRight + tileLeft) / (tileRight - tileLeft);
projectionMatrix.m11 = 2 * camera.nearClipPlane / (tileTop - tileBottom);
projectionMatrix.m11 = 2 * nearClipPlane / (tileTop - tileBottom);
projectionMatrix.m12 = (tileTop + tileBottom) / (tileTop - tileBottom);
projectionMatrices[row, column] = projectionMatrix;
}
Expand All @@ -116,26 +129,35 @@ protected override void NewFrameReady(RecordingSession session)

static Camera GetTargetCamera(string cameraTag)
{
GameObject[] gameObjectsWithTag;

try
{
var cameras = GameObject
.FindGameObjectsWithTag(cameraTag)
.Select(gameObject => gameObject.GetComponent<Camera>())
.Where(camera => camera != null)
.ToList();

if (cameras.Count > 1)
{
Debug.LogWarning($"Found more than one camera with tag '{cameraTag}'.");
}

return cameras.FirstOrDefault();
gameObjectsWithTag = GameObject.FindGameObjectsWithTag(cameraTag);
}
catch (UnityException)
{
Debug.LogError($"Tag '{cameraTag}' does not exist.");
Debug.LogError($"[Big Image Recorder] Tag '{cameraTag}' does not exist.");
return null;
}

var cameras = gameObjectsWithTag
.Select(gameObject => gameObject.GetComponent<Camera>())
.Where(camera => camera != null)
.ToList();

if (cameras.Count == 0)
{
Debug.LogError($"[Big Image Recorder] Found no camera with tag '{cameraTag}'.");
return null;
}

if (cameras.Count > 1)
{
Debug.LogWarning($"[Big Image Recorder] Found more than one camera with tag '{cameraTag}'.");
}

return cameras.First();
}
}
}
1 change: 0 additions & 1 deletion Editor/BigCameraInputSettingsPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
var outputWidth = property.FindPropertyRelative("outputWidth");
var rows = property.FindPropertyRelative("rows");


using (new EditorGUI.IndentLevelScope(-1))
{
EditorGUILayout.PropertyField(cameraTag);
Expand Down
2 changes: 1 addition & 1 deletion Editor/BigImageRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BigImageRecorder : GenericRecorder<BigImageRecorderSettings>

protected override bool BeginRecording(RecordingSession session)
{
if (!base.BeginRecording(session))
if (!base.BeginRecording(session) || !Input.HasCamera)
{
return false;
}
Expand Down

0 comments on commit 00f51de

Please sign in to comment.