Created
April 9, 2016 14:50
-
-
Save anonymous/345c00b504833641829b0c0d78d7f8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NatCamU; | |
using OpenCVForUnity; | |
using UnityEngine; | |
public class MovieManager : MonoBehaviour { | |
[SerializeField] private Renderer _renderer; | |
private Texture2D _destination; | |
private bool _initialized; | |
void Start() { | |
NatCam.SetDebugMode(); | |
NatCam.Initialize(PreviewType.Readable); | |
NatCam.SetActiveCamera(DeviceCamera.RearCamera); | |
NatCam.SetResolution(ResolutionPreset.HD); | |
NatCam.Play(); | |
NatCam.FocusMode = FocusMode.AutoFocus; | |
NatCam.DetectTouchesForFocus = Switch.Off; | |
NatCam.NatCamPreviewStartEvent += NatCamOnNatCamPreviewStartEvent; | |
} | |
private void NatCamOnNatCamPreviewStartEvent() { | |
if (_initialized) { | |
Debug.LogWarning("MovieManager: Already Initialized"); | |
return; | |
} | |
Debug.Log("MovieManager: Initializing " + System.DateTime.UtcNow); | |
if (NatCam.PreviewMatrix == null) { | |
Debug.LogError("MovieManager: Preview Mat is null"); | |
return; | |
} | |
// Fill texture with red to easily verify the texture is actually writting to later | |
_destination = new Texture2D(NatCam.PreviewMatrix.width(), NatCam.PreviewMatrix.height(), TextureFormat.RGBA32, false); | |
for (int x = 0; x < _destination.width; x++) { | |
for (int y = 0; y < _destination.width; y++) { | |
_destination.SetPixel(x, y, Color.red); | |
} | |
} | |
_destination.Apply(); | |
_renderer.material.SetTexture("_MainTex", _destination); | |
_initialized = true; | |
} | |
private void Update() { | |
if (!_initialized) { | |
Debug.Log("MovieManager: Not initialized yet"); | |
return; | |
} | |
Utils.matToTexture2D(NatCam.PreviewMatrix, _destination); | |
} | |
void OnDestroy() { | |
NatCam.Stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment