// locates and starts the available depth-sensors and their interfaces private void StartDepthSensors() { try { // try to initialize the available sensors KinectInterop.FrameSource dwFlags = GetFrameSourceFlags(); // locate the available depth-sensor interfaces in the scene List <DepthSensorBase> sensorInts = new List <DepthSensorBase>(); sensorInts.AddRange(gameObject.GetComponents <DepthSensorBase>()); // FindObjectsOfType<MonoBehaviour>(); sensorInts.AddRange(gameObject.GetComponentsInChildren <DepthSensorBase>()); if (sensorInts.Count == 0) { // by-default add K4A interface transform.position = new Vector3(0f, 1f, 0f); transform.rotation = Quaternion.identity; DepthSensorBase sensorInt = gameObject.AddComponent <Kinect4AzureInterface>(); sensorInts.Add(sensorInt); } for (int i = 0; i < sensorInts.Count; i++) { if (sensorInts[i] is DepthSensorBase) { DepthSensorBase sensorInt = (DepthSensorBase)sensorInts[i]; if (!sensorInt.enabled || sensorInt.deviceStreamingMode == KinectInterop.DeviceStreamingMode.Disabled || sensorInt.deviceIndex < 0) { Debug.Log(string.Format("S{0}: {1} disabled.", i, sensorInt.GetType().Name)); continue; } try { Debug.Log(string.Format("Opening S{0}: {1}, device-index: {2}", i, sensorInt.GetType().Name, sensorInt.deviceIndex)); KinectInterop.SensorData sensorData = sensorInt.OpenSensor(dwFlags, syncDepthAndColor, false); if (sensorData != null) { //Debug.Log("Succeeded opening " + sensorInt.GetType().Name); sensorData.sensorInterface = sensorInt; KinectInterop.InitSensorData(sensorData, this); sensorInterfaces.Add(sensorInt); sensorDatas.Add(sensorData); if (pollFramesInThread) { sensorData.threadStopEvent = new AutoResetEvent(false); sensorData.pollFramesThread = new Thread(() => PollFramesThread(sensorData)); sensorData.pollFramesThread.IsBackground = true; sensorData.pollFramesThread.Start(); } } } catch (Exception ex) { Debug.LogException(ex); Debug.LogError("Failed opening " + sensorInt.GetType().Name + ", device-index: " + sensorInt.deviceIndex); } } } Debug.Log(string.Format("{0} sensor(s) opened.", sensorDatas.Count)); // set initialization status if (sensorInterfaces.Count > 0) { kinectInitialized = true; } else { kinectInitialized = false; string sErrorMessage = "No suitable depth-sensor found. Please check the connected devices and installed SDKs."; Debug.LogError(sErrorMessage); if (statusInfoText != null) { statusInfoText.text = sErrorMessage; } } } //catch (DllNotFoundException ex) //{ // string message = ex.Message + " cannot be loaded. Please check the respective SDK installation."; // Debug.LogError(message); // Debug.LogException(ex); // if (calibrationText != null) // { // calibrationText.text = message; // } // return; //} catch (Exception ex) { string message = ex.Message; Debug.LogError(message); Debug.LogException(ex); if (statusInfoText != null) { statusInfoText.text = message; } return; } }
// 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); }
// 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); }