JY-Dev Tech Blog

유니티(Unity) - Android Device Camera (Webcamtexture) 설정방법 본문

유니티

유니티(Unity) - Android Device Camera (Webcamtexture) 설정방법

JY-Dev 2020. 7. 28. 15:17

[OverView]

이번에는 유니티 안드로이드 카메라를 불러와 webcamtexture를 이용해 RawImage에 보여주도록 하겠습니다.

 

WebCamDevice frontCameraDevice; 
WebCamDevice backCameraDevice; 
WebCamDevice activeCameraDevice; 

WebCamTexture frontCameraTexture; 
WebCamTexture backCameraTexture; 
WebCamTexture activeCameraTexture;

6개의 변수를 이용해 android에 카메라디바이스와 Texture를 가져올수있습니다.

위 3개의 디바이스 변수는 현재 사용하는 디바이스의 정보를 가지고 있는 변수입니다.

아래 3개의 Texture 변수는 현재 사용하고 있는 카메라의 Texture를 가지고 있는 변수입니다.

 

일단 Android에서 카메라를 사용하려면 카메라 Permission를 설정해줘야 하는데 Unity에서는 이런식으로 불러

올 수있습니다.

 

//카메라 퍼미션이 허용되지않았을때

if(!Permission.HasUserAuthorizedPermission(Permission.Camera))

{

   // 카메라 퍼미션 설정

   Permission.RequestUserPermission(Permission.Camera);

}

 

퍼미션를 허용했으면 이제 디바이스 정보를 얻어와야합니다. 아까 선언해둔 변수를 통해 얻어오도록하겠습니다.

 

if(WebCamTexture.devices.Length==0)

{

 // 디바이스가 없는 경우에는 Return 해줍시다.

Debug.Log("No devices cameras found");

return;

}



// 전면 카메라 디바이스

frontCameraDevice =WebCamTexture.devices.Last();

// 후면 카메라 디바이스 

backCameraDevice =WebCamTexture.devices.First();



// 전면 카메라 Texture

frontCameraTexture = new WebCamTexture(frontCameraDevice.name);

// 후면 카메라 Texture

backCameraTexture = new WebCamTexture(backCameraDevice.name);

 

여기서 기본적으로 Texture에 width랑 height를 사용하지않으면 640 x 420 사이즈로 나오기때문에 디바이스 카메라 크기에 따라 설정해주셔야합니다. 설정하는 방법은 아래와 같습니다. (1920 x 1080) 기준

 

CameraTexture = new WebCamTexture(CameraDevice.name,1920,1080);

 

그리고 RawImage에 Texture가 부드럽게 보이기위해 Filter를 설정해줍시다.

frontCameraTexture.filterMode = FilterMode.Trilinear;

backCameraTexture.filterMode = FilterMode.Trilinear;

 

[Trilinear 필터링]

텍스쳐 샘플들은 평범해지고, 또한 mipmap 레벨사이에서 혼합된다 .

RenderTextures 는 mipmaps를 지원하지 않으며, 그래서 이 셋팅은 Bilinear로 되돌아 간다.

 

전면 카메라를 선택할건지 후면 카메라를 선택할건지 세팅하는 방법에 대해서 알려드리겠습니다.

 

// 카메라가 Play중이라면 멈춰주도록합시다.

if(activeCameraTexture != null)

{

 activeCameraTexture.Stop();

}



// 전면카메라 사용시

activeCameraTexture = frontCameraTexture;

activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device => device.name == frontCameraTexture.deviceName);



// 후면카메라 사용시

activeCameraTexture = backCameraTexture;

activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device => device.name == backCameraTexture.deviceName);

 

[FirstOrDefault]

요소가 없는 경우 기본값을 하거나, 시퀀스의 첫 번째 요소를 반환 합니다.

 

// RawImage에 카메라 Texture를 입혀줍시다.

rawimage.texture = activeCameraTexture;

rawimage.material.mainTexture = activeCameraTexture;

activeCameraTexture.Play();

 

그리고 Update() 함수에

 

// 부정확한 카메라 데이터가 들어왔을때는 return 해줍시다.

if(activeCameraTexture.width < 100)

{

 return;

}



//orientation에 맞게 Rawimage를 조정해줍니다.

 rotationVector.z = -activeCameraTexture.videoRotationAngle; 
 rawimage.rectTransform.localEulerAngles = rotationVector;



// 화면 조정

rawimage.uvRect = activeCameraTexture.videoVerticallyMirrored ? fixedRect : defaultRect;

 

https://docs.unity3d.com/kr/530/ScriptReference/WebCamTexture-videoVerticallyMirrored.html

 

Unity - 스크립팅 API: WebCamTexture.videoVerticallyMirrored

Please note, that this will query platform-specific part, which might be not ready before actual video feed started; so it is not enough to call it once after play.

docs.unity3d.com

 

이렇게 해주시면 Rawimage에 카메라를 보여줄 수 있습니다.

감사합니다.

Comments