0

I would like to display live camera feed in Google Cardboard application. Basically speaking - I just want to see what me camera sees within cardboard app. Can you tell me how to achieve it, please? I'm getting lost in Unity.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • See if this helps a bit: https://stackoverflow.com/questions/30305948/using-smartphone-camera-video-in-google-cardboard-app – yakobom Jun 12 '17 at 03:49
  • Does this answer your question? [Display live camera feed in Unity](https://stackoverflow.com/questions/19482481/display-live-camera-feed-in-unity) – Liam Mar 24 '23 at 08:54

1 Answers1

0

I think you want stereoscopic view in unity, its very simple try this code for getting the live feed from camera and follow the steps giving below to make ur app in cardboard app.

/***** script for getting live feed from camera *****/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CamScript : MonoBehaviour {

    private bool camAvilable ;
    private WebCamTexture webcam;
    private Texture2D defaultbackground;
    private Texture2D output;

    public RawImage background;
    public RawImage imageToDisplay;
    public AspectRatioFitter fit;
    private Color[] data;

    void IntialCam(){
        defaultbackground = background.texture;

        WebCamDevice[] cam_devices = WebCamTexture.devices;

        if(cam_devices.Length == 0){
            Debug.Log ("No Camera");
            camAvilable = false;
            return;
        }

        webcam = new WebCamTexture (cam_devices [0].name, 320, 400, 60);

        if (webcam == null) {
            Debug.Log ("Unable to detect camera");
            return;
        }
        webcam.Play ();
        background.texture = webcam;
        imageToDisplay.texture = output;
        camAvilable = true;
    }

    void GetFeed(){
        if(!camAvilable){
            return;
        }

        float ratio = (float)webcam.width / (float)webcam.height;
        fit.aspectRatio = ratio;
        float scaleY = webcam.videoVerticallyMirrored ? -1f : 1f;
        background.rectTransform.localScale = new Vector3 (1f, scaleY, 1f);
        int orinat = -webcam.videoRotationAngle;
        background.rectTransform.localEulerAngles = new Vector3 (0,0,orinat);
    }

    void Start () {

        IntialCam ();

        output = new Texture2D (320,400);
        GetComponent<Renderer> ().material.mainTexture = output; 
        data = new Color[webcam.width * webcam.height];
    }

    // Update is called once per frame
    void Update () {
        GetFeed ();
        webcam.GetPixels(data);
        output.SetPixels(data);
        output.Apply();
    }

}

Now to make it a cardboard app u just have to go to Unity Player Settings>> other settings >> the check the virtual reality check box >> then in virtual reality sdk select cardboard.

thats it now just build it u will get the result.

wayneOS
  • 1,427
  • 1
  • 14
  • 20
  • or use this https://stackoverflow.com/questions/19482481/display-live-camera-feed-in-unity for getting the live feed and then follow the steps – Atul Singh Apr 07 '18 at 20:20