Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active April 9, 2020 03:54
Show Gist options
  • Select an option

  • Save olokobayusuf/a4f13816f8808bae74996221220102a3 to your computer and use it in GitHub Desktop.

Select an option

Save olokobayusuf/a4f13816f8808bae74996221220102a3 to your computer and use it in GitHub Desktop.
Pseudocode example illustrating recording multiple game cameras with NatCorder.
/*
* NatCorder
* Copyright (c) 2019 Yusuf Olokoba
*/
namespace NatCorder.Inputs {
using UnityEngine;
using System;
using Clocks;
using Dispatch;
/// <summary>
/// Recorder input for recording multiple game cameras
/// </summary>
public sealed class MultiCameraInput : IDisposable {
#region --Client API--
/// <summary>
/// Create a video recording input from multiple game cameras
/// </summary>
/// <param name="mediaRecorder">Media recorder to receive committed frames</param>
/// <param name="clock">Clock for generating timestamps</param>
/// <param name="cameras">Game cameras to record</param>
public static MultiCameraInput Create (IMediaRecorder mediaRecorder, IClock clock, params Camera[] cameras) {
if (mediaRecorder == null) {
Debug.LogError("NatCorder Error: Cannot create camera input for null media recorder");
return null;
}
if (clock == null) {
Debug.LogError("NatCorder Error: Cannot create multi-camera recorder with no clock");
return null;
}
if (cameras.Length < 2) {
Debug.LogError("NatCorder Error: Cannot create multi-camera recorder with less than two cameras");
return null;
}
return new MultiCameraInput(mediaRecorder, clock, cameras);
}
/// <summary>
/// Stop recorder input and teardown resources
/// </summary>
public void Dispose () {
DispatchUtility.onFrame -= OnFrame;
}
#endregion
#region --Operations--
private readonly IMediaRecorder mediaRecorder;
private readonly IClock clock;
private readonly Camera[] cameras;
private MultiCameraRecorder (IMediaRecorder mediaRecorder, IClock clock, Camera[] cameras) {
this.mediaRecorder = mediaRecorder;
this.clock = clock;
this.cameras = cameras;
DispatchUtility.onFrame += OnFrame;
}
private void OnFrame () {
var encoderFrame = mediaRecorder.AcquireFrame();
foreach (var camera in cameras) {
var prevTarget = camera.targetTexture;
camera.targetTexture = encoderFrame;
camera.Render();
camera.targetTexture = prevTarget;
}
mediaRecorder.CommitFrame(encoderFrame, clock.Timestamp);
}
#endregion
}
}
@yosun
Copy link

yosun commented Nov 2, 2018

Can you optimize this, or include a full example script?

@DenisFernandes
Copy link

Can you optimize this, or include a full example script?

+1 for this

@olokobayusuf
Copy link
Author

Updated with full script.

@TrueXRco
Copy link

TrueXRco commented Apr 9, 2020

Updated with full script.

Hi, I am getting the error Assets\Scripts\MultiCameraInput.cs(12,11): error CS0246: The type or namespace name 'Dispatch' could not be found (are you missing a using directive or an assembly reference?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment