Example #1
0
    public void Start()
    {
        try
        {
            // get sensor data
            KinectManager kinectManager = KinectManager.Instance;
            if (kinectManager && kinectManager.IsInitialized())
            {
                sensorData = kinectManager.GetSensorData();
            }

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Background removal cannot be started, because KinectManager is missing or not initialized.");
            }

            // ensure the needed dlls are in place and speech recognition is available for this interface
            bool bNeedRestart = false;
            bool bSuccess     = sensorData.sensorInterface.IsBackgroundRemovalAvailable(ref bNeedRestart);

            if (bSuccess)
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "BR");
                    return;
                }
            }
            else
            {
                string sInterfaceName = sensorData.sensorInterface.GetType().Name;
                throw new Exception(sInterfaceName + ": Background removal is not supported!");
            }

            // inverted alpha-body mask to color texture
            sensorData.invertAlphaColorMask = invertAlphaColorMask;

            if (invertAlphaColorMask &&
                (sensorData.sensorInterface.GetSensorPlatform() == KinectInterop.DepthSensorPlatform.KinectSDKv1))
            {
                // enable the foreground blender if found
                ForegroundBlender foreBlender = ForegroundBlender.Instance;

                if (foreBlender)
                {
                    foreBlender.enabled = true;

                    // disable the foreground camera, too
                    foregroundCamera = null;
                }
            }

            // Initialize the background removal
            bSuccess = sensorData.sensorInterface.InitBackgroundRemoval(sensorData, colorCameraResolution);

            if (!bSuccess)
            {
                throw new Exception("Background removal could not be initialized.");
            }

            // create the foreground image and alpha-image
            int imageLength = sensorData.sensorInterface.GetForegroundFrameLength(sensorData, colorCameraResolution);
            foregroundImage = new byte[imageLength];

            // get the needed rectangle
            Rect neededFgRect = sensorData.sensorInterface.GetForegroundFrameRect(sensorData, colorCameraResolution);

            // create the foreground texture
            foregroundTex = new Texture2D((int)neededFgRect.width, (int)neededFgRect.height, TextureFormat.RGBA32, false);

            // calculate the foreground rectangle
            if (foregroundCamera != null)
            {
                Rect  cameraRect = foregroundCamera.pixelRect;
                float rectHeight = cameraRect.height;
                float rectWidth  = cameraRect.width;

                if (rectWidth > rectHeight)
                {
                    rectWidth = Mathf.Round(rectHeight * neededFgRect.width / neededFgRect.height);
                }
                else
                {
                    rectHeight = Mathf.Round(rectWidth * neededFgRect.height / neededFgRect.width);
                }

                foregroundRect = new Rect((cameraRect.width - rectWidth) / 2, cameraRect.height - (cameraRect.height - rectHeight) / 2, rectWidth, -rectHeight);
            }

            isBrInited = true;

            //DontDestroyOnLoad(gameObject);
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = "Please check the Kinect and BR-Library installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = ex.Message;
            }
        }
    }
 void Awake()
 {
     instance = this;
 }