NatCam Tutorial Series 1: Starting Off
Welcome to the NatCam Tutorial Series. We decided to move to written tutorials as they are much easier to follow, and easier to update given the rate at which we make changes to the API. Just in case you wandered here from the wild, I’ll explain what NatCam is and how it came to be. Get the popcorn ready!
NatCam was started in the Fall of 2015. I was working at a startup that needed to scan barcodes in Unity. There was no solution at the time, and the closest (WebCamTexture) was simply unsatisfactory. So I decided to create an API that would do just what we needed of it. Fast-forward one and a half years later, we’re here!
So just what does NatCam do? Glad you asked. Here’s a (non-exhaustive) list:
- Fluid camera preview, much unlike WebCamTexture
- High resolution previews (1920x1080 and higher on some devices)
- Custom resolution previews, as high or low as you need
- Photo capture. Take high-res stills at 8MP, 12MP, or higher
- Flash. When capturing photos, set the flash mode
- Focus, and autofocus
- Exposure control. Brighten or darken the scene
- Zoom, optically by the camera
- Torch. Put on the torch, regardless of whether the camera preview is running
- VR Support, for all our Cardboard and GearVR enthusiasts out there
- Preview data. Get access to the preview data as it is being streamed
- Sources. Get access to the native sources*
Starting Off: Key Concepts
Before we delve into code, I’d like to explain a few key concepts you will encounter severally when working with NatCam:
- Camera: NatCam provides access to the hardware cameras on a device using the
DeviceCameraclass. This is different from a Unity camera, which renders the game view. With the this class, you can do things like setting resolutions, framerate, exposure, focus, flash, and so on.
- Preview: This is the camera preview. When you start a camera (using
NatCam.Play), a stream of data will begin from the camera. This stream will contain the preview (or more specifically, the preview frames that constitute the preview). You are able to configure the size (resolution) of the preview by requesting it with DeviceCamera.SetPreviewResolution. NatCam provides this stream in a
Enough Talk, Let’s Code
I suggest starting with a blank project in Unity. Import NatCam (Core or Pro) and follow along. If you have questions, make sure to ask them in the comments of this post. Now, let’s begin.
Our goal is to start the rear camera preview and display it to the screen. First, we create a fullscreen
RawImage where we will display the preview texture:
Next, let us create a C# script. I’ll call mine StartingOff:
Here’s an explanation of what’s going on:
- To start the camera preview, we simply call
NatCam.Playwith the camera we want to play from.
- We subscribe a delegate to the
NatCam.OnStartevent. This event is broadcast by NatCam when the camera preview actually starts because it is an asynchronous process.
- In our
OnStartdelegate, we display the
NatCam.Previewtexture on our
RawImage. Why didn’t we simply do this in
Start? Well, the preview texture only becomes available when the camera preview has actually started. So prior to the
OnStartevent being called,
NatCam.Previewwill be null.
Now, you might notice some stretching in the camera preview. This stretching is as a result of the difference between the aspect ratios of the camera preview and RawImage used to display it. To eliminate stretching, both aspect ratios must be the same. We will use the
AspectRatioFitter component that comes with Unity to set the aspect ratio:
AspectRatioFitter component onto the RawImage then add a reference to it in the
StartingOff component (as shown below):
That’s it for today folks! In the next tutorial, I will explore the NatCam Control Pipeline, showing how to perform different camera operations using the DeviceCamera class.
— Lanre Olokoba.