Exemplo n.º 1
0
        // initializes background removal with shaders
        private bool InitBackgroundRemoval(KinectInterop.SensorData sensorData)
        {
            if (sensorData != null && sensorData.sensorInterface != null && KinectInterop.IsDirectX11Available())
            {
                if (filterByBody != null)
                {
                    if (!filterByBody.InitBackgroundRemoval(sensorData, MAX_BODY_COUNT))
                    {
                        Debug.LogError("Could not init the background removal by body bounds!");
                        return(false);
                    }
                }
                else if (filterByBI != null)
                {
                    if (!filterByBI.InitBackgroundRemoval(sensorData))
                    {
                        Debug.LogError("Could not init the background removal by body index!");
                        return(false);
                    }
                }
                else if (filterByGS != null)
                {
                    if (!filterByGS.InitBackgroundRemoval(sensorData))
                    {
                        Debug.LogError("Could not init the background removal by green screen!");
                        return(false);
                    }
                }

                sensorInt = (DepthSensorBase)sensorData.sensorInterface;

                // set the texture resolution
                if (sensorInt.pointCloudColorTexture == null && sensorInt.pointCloudVertexTexture == null)
                {
                    sensorInt.pointCloudResolution = foregroundImageResolution;
                }

                textureRes = sensorInt.GetPointCloudTexResolution(sensorData);

                if (sensorInt.pointCloudColorTexture == null)
                {
                    colorTexture = KinectInterop.CreateRenderTexture(colorTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);
                    sensorInt.pointCloudColorTexture = colorTexture;
                    bColorTextureCreated             = true;
                }
                else
                {
                    colorTexture         = sensorInt.pointCloudColorTexture;
                    bColorTextureCreated = false;
                }

                if (filterByBody != null || filterByDist != null)
                {
                    if (sensorInt.pointCloudVertexTexture == null)
                    {
                        vertexTexture = KinectInterop.CreateRenderTexture(vertexTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGBHalf);
                        sensorInt.pointCloudVertexTexture = vertexTexture;
                        bVertexTextureCreated             = true;
                    }
                    else
                    {
                        vertexTexture         = sensorInt.pointCloudVertexTexture;
                        bVertexTextureCreated = false;
                    }
                }

                alphaTexture      = KinectInterop.CreateRenderTexture(alphaTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);
                foregroundTexture = KinectInterop.CreateRenderTexture(foregroundTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);

                Shader erodeShader = Shader.Find("Kinect/ErodeShader");
                erodeFilterMat = new Material(erodeShader);
                erodeFilterMat.SetFloat("_TexResX", (float)textureRes.x);
                erodeFilterMat.SetFloat("_TexResY", (float)textureRes.y);
                //sensorData.erodeBodyMaterial.SetTexture("_MainTex", sensorData.bodyIndexTexture);

                Shader dilateShader = Shader.Find("Kinect/DilateShader");
                dilateFilterMat = new Material(dilateShader);
                dilateFilterMat.SetFloat("_TexResX", (float)textureRes.x);
                dilateFilterMat.SetFloat("_TexResY", (float)textureRes.y);
                //sensorData.dilateBodyMaterial.SetTexture("_MainTex", sensorData.bodyIndexTexture);

                Shader gradientShader = Shader.Find("Kinect/GradientShader");
                gradientFilterMat = new Material(gradientShader);

                Shader medianShader = Shader.Find("Kinect/MedianShader");
                medianFilterMat = new Material(medianShader);
                //sensorData.medianBodyMaterial.SetFloat("_Amount", 1.0f);

                Shader blurShader = Shader.Find("Kinect/BlurShader");
                blurFilterMat = new Material(blurShader);

                Shader invertShader = Shader.Find("Kinect/InvertShader");
                invertAlphaMat = new Material(invertShader);

                Shader foregroundShader = Shader.Find("Kinect/ForegroundShader");
                foregroundMat = new Material(foregroundShader);

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        // initializes background removal with shaders
        private bool InitBackgroundRemoval(KinectInterop.SensorData sensorData)
        {
            if (sensorData != null && sensorData.sensorInterface != null && KinectInterop.IsDirectX11Available())
            {
                sensorInt = (DepthSensorBase)sensorData.sensorInterface;

                if (filterByBI != null)
                {
                    if (!filterByBI.InitBackgroundRemoval(sensorData))
                    {
                        Debug.LogError("Could not init the background removal by body index!");
                        return(false);
                    }
                }

                // set the texture resolution
                if (sensorInt.pointCloudColorTexture == null && sensorInt.pointCloudVertexTexture == null)
                {
                    sensorInt.pointCloudResolution = foregroundImageResolution;
                }

                textureRes = sensorInt.GetPointCloudTexResolution(sensorData);

                colorTexture = KinectInterop.CreateRenderTexture(colorTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);
                if (filterByBI == null)
                {
                    vertexTexture = KinectInterop.CreateRenderTexture(vertexTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGBHalf);
                }

                alphaTexture      = KinectInterop.CreateRenderTexture(alphaTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);
                foregroundTexture = KinectInterop.CreateRenderTexture(foregroundTexture, textureRes.x, textureRes.y, RenderTextureFormat.ARGB32);

                sensorInt.pointCloudColorTexture  = colorTexture;
                sensorInt.pointCloudVertexTexture = vertexTexture;

                Shader erodeShader = Shader.Find("Kinect/ErodeShader");
                erodeFilterMat = new Material(erodeShader);
                erodeFilterMat.SetFloat("_TexResX", (float)textureRes.x);
                erodeFilterMat.SetFloat("_TexResY", (float)textureRes.y);
                //sensorData.erodeBodyMaterial.SetTexture("_MainTex", sensorData.bodyIndexTexture);

                Shader dilateShader = Shader.Find("Kinect/DilateShader");
                dilateFilterMat = new Material(dilateShader);
                dilateFilterMat.SetFloat("_TexResX", (float)textureRes.x);
                dilateFilterMat.SetFloat("_TexResY", (float)textureRes.y);
                //sensorData.dilateBodyMaterial.SetTexture("_MainTex", sensorData.bodyIndexTexture);

                Shader gradientShader = Shader.Find("Kinect/GradientShader");
                gradientFilterMat = new Material(gradientShader);

                Shader medianShader = Shader.Find("Kinect/MedianShader");
                medianFilterMat = new Material(medianShader);
                //sensorData.medianBodyMaterial.SetFloat("_Amount", 1.0f);

                Shader blurShader = Shader.Find("Kinect/BlurShader");
                blurFilterMat = new Material(blurShader);

                Shader invertShader = Shader.Find("Kinect/InvertShader");
                invertAlphaMat = new Material(invertShader);

                Shader foregroundShader = Shader.Find("Kinect/ForegroundShader");
                foregroundMat = new Material(foregroundShader);

                if (filterByDist == null && filterByBI == null)
                {
                    foregroundFilterShader = Resources.Load("ForegroundFiltBodyShader") as ComputeShader;
                    foregroundFilterKernel = foregroundFilterShader != null?foregroundFilterShader.FindKernel("FgFiltBody") : -1;

                    //foregroundFilterPos = new Vector4[KinectInterop.Constants.MaxBodyCount];
                    bodyPosMin  = new Vector4[MAX_BODY_COUNT];
                    bodyPosMaxX = new Vector4[MAX_BODY_COUNT];
                    bodyPosMaxY = new Vector4[MAX_BODY_COUNT];
                    bodyPosMaxZ = new Vector4[MAX_BODY_COUNT];
                    bodyPosDot  = new Vector4[MAX_BODY_COUNT];
                }

                return(true);
            }

            return(false);
        }