Created
August 4, 2019 16:47
-
-
Save techtide/d0dea1796806d1490bace26f31cbb25b 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
// Adapted from https://forum.unity.com/threads/locatable-camera-in-unity.398803/ | |
using UnityEngine; | |
using System.Collections.Generic; | |
using UnityEngine.XR.WSA.Input; | |
using UnityEngine.XR.WSA.WebCam; | |
public class PhotoQuadNew : MonoBehaviour | |
{ | |
GestureRecognizer m_GestureRecognizer; | |
GameObject m_Quad = null; | |
Renderer m_QuadRenderer = null; | |
PhotoCapture m_PhotoCaptureObj; | |
CameraParameters m_CameraParameters; | |
bool m_CapturingPhoto = false; | |
Texture2D m_Texture = null; | |
// Use this for initialization | |
void Start() | |
{ | |
Initialize(); | |
} | |
void SetupGestureRecognizer() | |
{ | |
m_GestureRecognizer = new GestureRecognizer(); | |
m_GestureRecognizer.SetRecognizableGestures(GestureSettings.Tap); | |
m_GestureRecognizer.TappedEvent += OnTappedEvent; | |
m_GestureRecognizer.StartCapturingGestures(); | |
m_CapturingPhoto = false; | |
} | |
void Initialize() | |
{ | |
Debug.Log("Initializing..."); | |
List<Resolution> resolutions = new List<Resolution>(PhotoCapture.SupportedResolutions); | |
Resolution selectedResolution = resolutions[0]; | |
m_CameraParameters = new CameraParameters(WebCamMode.PhotoMode); | |
m_CameraParameters.cameraResolutionWidth = selectedResolution.width; | |
m_CameraParameters.cameraResolutionHeight = selectedResolution.height; | |
m_CameraParameters.hologramOpacity = 0.0f; | |
m_CameraParameters.pixelFormat = CapturePixelFormat.BGRA32; | |
m_Texture = new Texture2D(selectedResolution.width, selectedResolution.height, TextureFormat.BGRA32, false); | |
PhotoCapture.CreateAsync(false, OnCreatedPhotoCaptureObject); | |
} | |
void OnCreatedPhotoCaptureObject(PhotoCapture captureObject) | |
{ | |
m_PhotoCaptureObj = captureObject; | |
m_PhotoCaptureObj.StartPhotoModeAsync(m_CameraParameters, OnStartPhotoMode); | |
} | |
void OnStartPhotoMode(PhotoCapture.PhotoCaptureResult result) | |
{ | |
SetupGestureRecognizer(); | |
Debug.Log("Ready!"); | |
Debug.Log("Air Tap to take a picture."); | |
} | |
void OnTappedEvent(InteractionSourceKind source, int tapCount, Ray headRay) | |
{ | |
if (m_CapturingPhoto) | |
{ | |
return; | |
} | |
m_CapturingPhoto = true; | |
Debug.Log("Taking picture..."); | |
m_PhotoCaptureObj.TakePhotoAsync(OnPhotoCaptured); | |
} | |
void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) | |
{ | |
if (m_Quad == null) | |
{ | |
m_Quad = GameObject.CreatePrimitive(PrimitiveType.Quad); | |
m_Quad.name = "PhotoCaptureFrame"; | |
m_QuadRenderer = m_Quad.GetComponent<Renderer>() as Renderer; | |
m_QuadRenderer.material = new Material(Shader.Find("Custom/Unlit/UnlitTexture")); | |
} | |
Matrix4x4 cameraToWorldMatrix; | |
photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix); | |
Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse; | |
Matrix4x4 projectionMatrix; | |
photoCaptureFrame.TryGetProjectionMatrix(Camera.main.nearClipPlane, Camera.main.farClipPlane, out projectionMatrix); | |
photoCaptureFrame.UploadImageDataToTexture(m_Texture); | |
m_QuadRenderer.material.SetTexture("_MainTex", m_Texture); | |
Vector3 position = cameraToWorldMatrix.MultiplyPoint(Vector3.zero); | |
Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1)); | |
m_Quad.transform.position = position; | |
m_Quad.transform.rotation = rotation; | |
Debug.Log("Took picture!"); | |
m_CapturingPhoto = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment