본문 바로가기
NOTE/SaveSource

[SaveSource] GyroScope Camera

by DevAthena 2016. 10. 6.

[CameraScene]

스마트폰움직임이랑 Unity MainCamera랑 움직임 동일화

1. GyroCamera.cs 

using UnityEngine;
using System.Collections;

public class GyroCamera : MonoBehaviour {
    private Gyroscope gyro;
    private bool gyroSupported;
    private Quaternion rotFix;

	void Start () {
        gyroSupported = SystemInfo.supportsGyroscope;

        GameObject camParent = new GameObject("camParent");
        camParent.transform.position = transform.position;
        transform.parent = camParent.transform;

        if(gyroSupported)
        {
            gyro = Input.gyro;
            gyro.enabled = true;

            camParent.transform.rotation = Quaternion.Euler(90f, 180f, 0f);
            rotFix = new Quaternion(0, 0, 1, 0);
        }        
	}
	
	void Update () {
        transform.localRotation = gyro.attitude * rotFix;
	}
}


2. GyroCamera.cs

public class GyroCamera : MonoBehaviour {
    private Gyroscope gyro;
    private bool gyroSupported;
    private Quaternion rotFix;

    [SerializeField]
    private Transform worldObj;
    private float startY;

	void Start () {
        gyroSupported = SystemInfo.supportsGyroscope;

        GameObject camParent = new GameObject("camParent");
        camParent.transform.position = transform.position;
        transform.parent = camParent.transform;

        if(gyroSupported)
        {
            gyro = Input.gyro;
            gyro.enabled = true;

            camParent.transform.rotation = Quaternion.Euler(90f, 180f, 0f);
            rotFix = new Quaternion(0, 0, 1, 0);
        }        
	}
	
	void Update () {
        if(gyroSupported && startY == 0)
        {
            ResetGyroRotation();
        }
        transform.localRotation = gyro.attitude * rotFix;
	}

    void ResetGyroRotation()
    {
        startY = transform.eulerAngles.y;
        worldObj.rotation = Quaternion.Euler(0f, startY, 0f);
    }
}


[WebcamScene]

RawImage에다가 WebcamTexture입혀서 백그라운드로

1. CameraAsBackground.cs

public class CameraAsBackground : MonoBehaviour {
    private RawImage image;
    private WebCamTexture cam;
    private AspectRatioFitter arf;

	void Start () {
        arf = GetComponent<Aspectratiofitter>();

        image = GetComponent<Rawimage>();
        cam = new WebCamTexture(Screen.width, Screen.height);
        image.texture = cam;
        cam.Play();
	}
	
	void Update () {
	    if(cam.width < 100)
        {
            return;
        }

        float cwNeeded = -cam.videoRotationAngle;
        if (cam.videoVerticallyMirrored)
            cwNeeded += 180f;

        image.rectTransform.localEulerAngles = new Vector3(0f, 0f, cwNeeded);

        float vidioRatio = (float)cam.width / (float)cam.height;
        arf.aspectRatio = vidioRatio;
	}
}


2. CameraAsBackground.cs

public class CameraAsBackground : MonoBehaviour {
    private RawImage image;
    private WebCamTexture cam;
    private AspectRatioFitter arf;

	void Start () {
        arf = GetComponent<Aspectratiofitter>();

        image = GetComponent<Rawimage>();
        cam = new WebCamTexture(Screen.width, Screen.height);
        image.texture = cam;
        cam.Play();
	}
	
	void Update () {
	    if(cam.width < 100)
        {
            return;
        }

        float cwNeeded = -cam.videoRotationAngle;
        if (cam.videoVerticallyMirrored)
            cwNeeded += 180f;

        image.rectTransform.localEulerAngles = new Vector3(0f, 0f, cwNeeded);

        float vidioRatio = (float)cam.width / (float)cam.height;
        arf.aspectRatio = vidioRatio;

        if(cam.videoVerticallyMirrored)
        {
            image.uvRect = new Rect(1, 0, -1, 1);
        }
        else
        {
            image.uvRect = new Rect(0, 0, 1, 1);
        }
	}
}


'NOTE > SaveSource' 카테고리의 다른 글

[Source Save] TouchCameraMove.cs  (0) 2016.10.31
[Source Save] InputTouchManager.cs  (0) 2016.10.27
[SaveSource] Drag Script  (0) 2016.10.14
[SaveSource] Touch Pinch Zoom  (0) 2016.10.14
[SaveSource] Player Cam & Input.Axis  (0) 2016.10.07