Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@olokobayusuf

This comment has been minimized.

Copy link
Owner Author

olokobayusuf commented Aug 13, 2018

Blending Camera Views

If you intend to do some sort of split screen as opposed to compositing by depth (which is what the script currently does), you can modify the Camera.rect property on each camera being recorded (just like setting the viewport rect in the Editor). The key difference is that instead of setting Camera.targetTexture before calling Camera.Render, call Camera.SetTargetBuffers instead. Camera.targetTexture will not respect the camera viewport rect setting, whereas Camera.SetTargetBuffers will.

@yosun

This comment has been minimized.

Copy link

yosun commented Nov 2, 2018

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

@DenisFernandes

This comment has been minimized.

Copy link

DenisFernandes commented Nov 7, 2018

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

+1 for this

@olokobayusuf

This comment has been minimized.

Copy link
Owner Author

olokobayusuf commented Nov 11, 2018

Updated with full script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.