public void SetUpPhysicalCamera(int deviceIndex)
    {
        //check for physical cameras
        if (WebCamTexture.devices.Length <= 0)
        {
            Debug.LogWarning("No Camera Device Was Found!");
            return;
        }

        //check if the provided index is correct
        if (deviceIndex >= WebCamTexture.devices.Length)
        {
            webCamDevice = null;
            Debug.LogError("There is no device with that index");
            return;
        }

        //Get the device
        webCamDevice = WebCamTexture.devices[deviceIndex];

        //Create a WebCamTexture
        webCamTexture = new WebCamTexture(webCamDevice.Value.name, webCamSettings.requestedWidth,
                                          webCamSettings.requestedHeight, webCamSettings.TextureFPS);


        ApplyTextureConversionParameters();

        AssignNewCameraTextureResolution(webCamSettings.requestedWidth, webCamSettings.requestedHeight, true);
    }
Пример #2
0
    public void ResetScreen(WebCamDevice?device)
    {
        if (webCamTexture != null && webCamTexture.isPlaying)
        {
            webCamTexture.Stop();
            webCamTexture = null;
        }

        if (device == null)
        {
            return;
        }

        webCamTexture = new WebCamTexture(device?.name, DefaultHeight, DefaultWidth, FPS);

        try {
            webCamTexture.Play();
        } catch (Exception e) {
            Debug.LogWarning(e.Message);
            return;
        }

        Renderer renderer = GetComponent <Renderer>();

        outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height);
        renderer.material.mainTexture = outputTexture;

        pixelData = new Color32[webCamTexture.width * webCamTexture.height];
    }
Пример #3
0
    /// TODO: use Coroutine and call InitScreen here
    public void ResetScreen(WebCamDevice?device)
    {
        if (isPlaying)
        {
            webCamTexture.Stop();
            webCamTexture = null;
        }

        if (device is WebCamDevice deviceValue)
        {
            webCamDevice = deviceValue;
        }
        else
        {
            return;
        }

        /// TODO: call Application.RequestUserAuthorization
        webCamTexture = new WebCamTexture(webCamDevice.name, Width, Height, FPS);
        WebCamTextureFramePool.Instance.SetDimension(Width, Height);

        try {
            webCamTexture.Play();
            Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}");
        } catch (Exception e) {
            Debug.LogWarning(e.ToString());
            return;
        }
    }
Пример #4
0
    void OnValueChanged(Dropdown dropdown)
    {
        WebCamDevice?device = dropdown.value < devices.Length ? (WebCamDevice?)devices[dropdown.value] : null;

        Debug.Log("WebCamDevice Changed: " + device?.name);
        sceneDirector.GetComponent <SceneDirector>().ChangeWebCamDevice(device);
    }
    void ResetCamera(WebCamDevice?webCamDevice)
    {
        StopCamera();
        var webCamScreen = GameObject.Find("WebCamScreen");

        cameraSetupCoroutine = StartCoroutine(webCamScreen.GetComponent <WebCamScreenController>().ResetScreen(webCamDevice));
        this.webCamDevice    = webCamDevice;
    }
Пример #6
0
    private CameraManager()
    {
        WebCamDevice?device = WebCamTexture.devices.FirstOrDefault(d => d.isFrontFacing);

        if (device.HasValue)
        {
            Camera            = new WebCamTexture(device.Value.name, Screen.height, Screen.width);
            Camera.filterMode = FilterMode.Point;
        }
    }
Пример #7
0
    public void ChangeWebCamDevice(WebCamDevice?webCamDevice)
    {
        lock (graphLock) {
            ResetCamera(webCamDevice);

            if (graphPrefab != null)
            {
                StopGraph();
                StartGraph();
            }
        }
    }
Пример #8
0
    public void SetWebCamDevice(WebCamDevice?webCamDevice)
    {
        this.webCamDevice = webCamDevice;

        webCamScreen.GetComponent <WebCamScreenController>().ResetScreen(webCamDevice);

        if (graphRunner != null)
        {
            StopCoroutine(graphRunner);
        }

        graphRunner = StartCoroutine(RunGraph());
    }
Пример #9
0
    public IEnumerator ResetScreen(WebCamDevice?device)
    {
        if (isPlaying)
        {
            webCamTexture.Stop();
            webCamTexture = null;
            pixelData     = null;
        }

        if (device is WebCamDevice deviceValue)
        {
            webCamDevice = deviceValue;
        }
        else
        {
            yield break;
        }

        webCamTexture = new WebCamTexture(webCamDevice.name, Width, Height, FPS);
        WebCamTextureFramePool.Instance.SetDimension(Width, Height);

        try {
            webCamTexture.Play();
            Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}");
        } catch (Exception e) {
            Debug.LogWarning(e.ToString());
            yield break;
        }

        var waitFrame = MAX_FRAMES_TO_BE_INITIALIZED;

        yield return(new WaitUntil(() => {
            return isWebCamTextureInitialized || --waitFrame < 0;
        }));

        if (!isWebCamTextureInitialized)
        {
            Debug.LogError("Failed to initialize WebCamTexture");
            yield break;
        }

        Renderer renderer = GetComponent <Renderer>();
        RawImage rawImage = GetComponent <RawImage>();

        outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
        //renderer.material.mainTexture = outputTexture;
        rawImage.texture = outputTexture;

        pixelData = new Color32[webCamTexture.width * webCamTexture.height];
    }
    void OnDestroy()
    {
        if (webCamTexture != null && webCamTexture.isPlaying)
        {
            Debug.Log("Camera is still playing");
            webCamTexture.Pause();

            while (webCamTexture.isPlaying)
            {
                Debug.Log("Camera is stopping....");
            }

            Debug.Log("Camera stopped playing");
        }

        webCamTexture.Stop();
        webCamTexture = null;
        webCamDevice  = null;
    }
Пример #11
0
    protected IEnumerator Initialize()
    {
        /* Waiting for a frame can help on some devices, especially when initializing the camera when returning from  background */
        yield return(null);

        WebCamDevice?selectedDevice = null;

        /* First search for a back-facing device */
        foreach (var device in WebCamTexture.devices)
        {
            if (!device.isFrontFacing)
            {
                selectedDevice = device;
                break;
            }
        }

        /* If no back-facing device was found, search again for a front facing device */
        if (selectedDevice == null)
        {
            if (WebCamTexture.devices.Length > 0)
            {
                selectedDevice = WebCamTexture.devices[0];
            }
        }

        if (selectedDevice != null)
        {
            _feed = new WebCamTexture(selectedDevice.Value.name, FrameWidth, FrameHeight);
            _feed.Play();
        }

        if (_feed == null)
        {
            Debug.LogError("Could not find any cameras on the device.");
        }

        ResetBuffers(FrameWidth, FrameHeight, 4);
    }
Пример #12
0
    public void ResetScreen(WebCamDevice?device)
    {
        if (webCamTexture != null && webCamTexture.isPlaying)
        {
            webCamTexture.Stop();
            webCamTexture = null;
        }

        if (device == null)
        {
            return;
        }

        webCamTexture = new WebCamTexture(device?.name, DefaultWidth, DefaultHeight, FPS);

        try {
            webCamTexture.Play();
        } catch (Exception e) {
            Debug.LogWarning(e.ToString());
            return;
        }
    }
Пример #13
0
    }// Start is called before the first frame update

    // Update is called once per frame
    private WebCamDevice FinddFrontFacingCamera()
    {
        // find camera
        var deviceList = WebCamTexture.devices;

        if (deviceList.Length == 0)
        {
            if (Application.isEditor)
            {
                Debug.LogWarning("No camera found");
            }
            else
            {
                throw new Exception("No camera found");
            }
        }
        WebCamDevice?device = null;

        foreach (var d in deviceList)
        {
            if (d.isFrontFacing)
            {
                device = d;
            }
        }
        if (device == null)
        {
            if (Application.isEditor)
            {
                Debug.LogWarning("No FrontFacing camera found");
            }
            else
            {
                throw new Exception("No FrontFacing camera found");
            }
        }
        return(device.Value);
    }
Пример #14
0
    private WebCamDevice FindFrontFacingCamera()
    {
        // find camera
        var deviceList = WebCamTexture.devices;

        if (deviceList.Length == 0)
        {
            throw new Exception("No camera found");
        }
        WebCamDevice?device = null;

        foreach (var d in deviceList)
        {
            if (d.isFrontFacing)
            {
                device = d;
            }
        }
        if (device == null)
        {
            throw new Exception("No FrontFacing camera found");
        }
        return(device.Value);
    }
    public IEnumerator ResetScreen(WebCamDevice?device)
    {
#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
        {
            Permission.RequestUserPermission(Permission.Camera);
        }
#elif UNITY_IOS
        if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            yield return(Application.RequestUserAuthorization(UserAuthorization.WebCam));
        }
#endif

        if (isPlaying)
        {
            webCamTexture.Stop();
            webCamTexture = null;
            pixelData     = null;
        }

        if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            Debug.LogWarning("Not permitted to use Camera");
            yield break;
        }

        if (device is WebCamDevice deviceValue)
        {
            webCamDevice = deviceValue;
        }
        else
        {
            yield break;
        }

        webCamTexture = new WebCamTexture(webCamDevice.name, Width, Height, FPS);
        WebCamTextureFramePool.Instance.SetDimension(Width, Height);

        try {
            webCamTexture.Play();
            Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}");
        } catch (Exception e) {
            Debug.LogWarning(e.ToString());
            yield break;
        }

        var waitFrame = MAX_FRAMES_TO_BE_INITIALIZED;

        yield return(new WaitUntil(() => {
            return isWebCamTextureInitialized || --waitFrame < 0;
        }));

        if (!isWebCamTextureInitialized)
        {
            Debug.LogError("Failed to initialize WebCamTexture");
            yield break;
        }

        Renderer renderer = GetComponent <Renderer>();
        outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
        renderer.material.mainTexture = outputTexture;

        pixelData = new Color32[webCamTexture.width * webCamTexture.height];
    }
Пример #16
0
    private IEnumerator Initialize(bool firstStart)
    {
        if (!firstStart)
        {
            // If we are resuming from background, we wait a frame to make sure that everything is initialized
            // before starting the camera again.
            yield return(null);
        }

        WebCamDevice?selectedDevice = null;

        // First search for a back-facing device
        foreach (var device in WebCamTexture.devices)
        {
            if (!device.isFrontFacing)
            {
                selectedDevice = device;
                break;
            }
        }

        // If no back-facing device was found, search again for a front facing device
        if (selectedDevice == null)
        {
            if (WebCamTexture.devices.Length > 0)
            {
                selectedDevice = WebCamTexture.devices[0];
            }
        }

        if (selectedDevice != null)
        {
            _feed = new WebCamTexture(selectedDevice.Value.name, FrameWidth, FrameHeight);
            _feed.Play();
        }

        if (_feed == null)
        {
            Debug.LogError("Could not find any cameras on the device.");
        }

        ResetBuffers(FrameWidth, FrameHeight, 4);

        // Wait a frame before getting the camera rotation, otherwise it might not be initialized yet
        yield return(null);

        if (Application.platform == RuntimePlatform.Android)
        {
            bool rotatedSensor = false;
            switch (Screen.orientation)
            {
            case ScreenOrientation.Portrait: {
                rotatedSensor = _feed.videoRotationAngle == 270;
                break;
            }

            case ScreenOrientation.LandscapeLeft: {
                rotatedSensor = _feed.videoRotationAngle == 180;
                break;
            }

            case ScreenOrientation.LandscapeRight: {
                rotatedSensor = _feed.videoRotationAngle == 0;
                break;
            }

            case ScreenOrientation.PortraitUpsideDown: {
                rotatedSensor = _feed.videoRotationAngle == 90;
                break;
            }
            }

            if (rotatedSensor)
            {
                // Normally, we use InvertedFrame = true, because textures in Unity are mirrored vertically, when compared with the ones the camera provides.
                // However, when we detect that the camera sensor is rotated by 180 degrees, as is the case for the Nexus 5X for example,
                // We turn off inverted frame and enable mirrored frame, which has the effect of rotating the frame upside down.
                // We use the MirroredFrame property and not the EnableMirroring property because the first one actually changes the data that
                // is being processed, while the second one only changes how the frame is rendered, leaving the frame data intact.
                // WikitudeCam.InvertedFrame = false;
                // WikitudeCam.MirroredFrame = true;

                // Additionally, because we are doing the rendering in Unity, we need to instruct the renderer to flip the image.
                Renderer.FlipImage = true;
            }
        }
    }
Пример #17
0
    public void ChangeWebCamDevice(WebCamDevice?webCamDevice)
    {
        this.webCamDevice = webCamDevice;

        webCamScreen.GetComponent <WebCamScreenController>().ResetScreen(webCamDevice);
    }
Пример #18
0
    private IEnumerator Initialize(bool firstStart)
    {
        if (!firstStart)
        {
            /* If we are resuming from background, we wait a frame to make sure that everything is initialized
             * before starting the camera again.
             */
            yield return(null);
        }

        if (_feed == null)
        {
            WebCamDevice?selectedDevice = null;
            /* First search for a back-facing device */
            foreach (var device in WebCamTexture.devices)
            {
                if (!device.isFrontFacing)
                {
                    selectedDevice = device;
                    break;
                }
            }

            /* If no back-facing device was found, search again for a front facing device */
            if (selectedDevice == null)
            {
                if (WebCamTexture.devices.Length > 0)
                {
                    selectedDevice = WebCamTexture.devices[0];
                }
            }

            if (selectedDevice != null)
            {
                _feed = new WebCamTexture(selectedDevice.Value.name, FrameWidth, FrameHeight);
                _feed.Play();
            }
        }
        else
        {
            _feed.Play();
        }

        if (_feed == null)
        {
            Debug.LogError("Could not find any cameras on the device.");
        }

        ResetBuffers(FrameWidth, FrameHeight, 4);

        // Wait a frame before getting the camera rotation, otherwise it might not be initialized yet
        yield return(null);

        if (Application.platform == RuntimePlatform.Android)
        {
            bool rotatedSensor = false;
            switch (Screen.orientation)
            {
            case ScreenOrientation.Portrait: {
                rotatedSensor = _feed.videoRotationAngle == 270;
                break;
            }

            case ScreenOrientation.LandscapeLeft: {
                rotatedSensor = _feed.videoRotationAngle == 180;
                break;
            }

            case ScreenOrientation.LandscapeRight: {
                rotatedSensor = _feed.videoRotationAngle == 0;
                break;
            }

            case ScreenOrientation.PortraitUpsideDown: {
                rotatedSensor = _feed.videoRotationAngle == 90;
                break;
            }
            }

            if (rotatedSensor)
            {
                // Because the sensor is inverted, we need to flip the image when rendering it to the screen.
                Renderer.FlipImage = true;
            }
        }
    }