// Called for rendering the gui void OnGUI() { // AA: Position the depth image so the user can see the kinect output if (!m_disableTrackingImage && (!m_disableTrackingImageWithSwipeMenu || !m_swipeMenuDisplayedLastFrame)) { // Debug image GUI.depth = -4; GUI.DrawTexture(new Rect(25, Screen.height - m_yRes / m_factor - 25, m_xRes / m_factor, m_yRes / m_factor), m_depthMapTexture); //GUI.DrawTexture(new Rect(Screen.width-m_xRes/m_factor, Screen.height-m_yRes/m_factor, m_xRes / m_factor, m_yRes / m_factor), m_depthMapTexture); } //AA: add the GUI elements int shift = 42; GUI.Box(new Rect(5 + shift, 25, Screen.width / 3 - 130, Screen.height - 50), "FILTERS"); m_bUseSimpleAverage = GUI.Toggle(new Rect(45 + shift, 50, 200, 30), m_bUseSimpleAverage, " SIMPLE AVERAGE 10"); m_bUseMovingAverage = GUI.Toggle(new Rect(45 + shift, 90, 200, 30), m_bUseMovingAverage, " MOVING AVERAGE"); m_bUseSimpleAverage5 = GUI.Toggle(new Rect(45 + shift, 130, 200, 30), m_bUseSimpleAverage5, " SIMPLE AVERAGE 5"); m_bDblMovingAverage = GUI.Toggle(new Rect(45 + shift, 170, 200, 30), m_bDblMovingAverage, " DOUBLE MOV AVERAGE"); m_bUseExpSmoothing = GUI.Toggle(new Rect(45 + shift, 210, 200, 30), m_bUseExpSmoothing, " EXP SMOOTHING"); m_bUseDblExpSmoothing = GUI.Toggle(new Rect(45 + shift, 250, 200, 30), m_bUseDblExpSmoothing, " DOUBLE EXP SMOOTHING"); m_bUseAdaptive = GUI.Toggle(new Rect(45 + shift, 290, 200, 30), m_bUseAdaptive, " ADAPTIVE DBL EXP"); m_bUseMedian = GUI.Toggle(new Rect(45 + shift, 330, 200, 30), m_bUseMedian, " MEDIAN"); m_bUseCombination1 = GUI.Toggle(new Rect(45 + shift, 370, 200, 30), m_bUseCombination1, " SIMPLE AVG + Median"); m_bUseCombination2 = GUI.Toggle(new Rect(45 + shift, 410, 200, 30), m_bUseCombination2, " DBL MOV AVG + Median"); m_bUseNone = GUI.Toggle(new Rect(45 + shift, 450, 200, 30), m_bUseNone, " NONE"); if (GUI.Button(new Rect(50 + shift, Screen.height - 75, 150, 30), "Clear")) { WriteableAreaResize(); fv.DrawCircle(); } // If some button has been pressed OR this is the first exection if (GUI.changed) { LoadFilters(); if (fm.filters.Count == 1) { m_principalCursor = 0; } } int count = 0; if (m_bUseSimpleAverage) { GUI.Label(new Rect(15 + shift, 45, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseMovingAverage) { GUI.Label(new Rect(15 + shift, 85, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseSimpleAverage5) { GUI.Label(new Rect(15 + shift, 125, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bDblMovingAverage) { GUI.Label(new Rect(15 + shift, 165, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseExpSmoothing) { GUI.Label(new Rect(15 + shift, 205, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseDblExpSmoothing) { GUI.Label(new Rect(15 + shift, 245, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseAdaptive) { GUI.Label(new Rect(15 + shift, 285, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseMedian) { GUI.Label(new Rect(15 + shift, 325, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseCombination1) { GUI.Label(new Rect(15 + shift, 365, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseCombination2) { GUI.Label(new Rect(15 + shift, 405, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (m_bUseNone) { GUI.Label(new Rect(15 + shift, 445, 30, 30), m_colorTextureDictionary[fm.colors[count].ToString()]); count++; } if (prevScreenWidth != Screen.width || prevScreenHeight != Screen.height) { // Resize writeable area, redraw the circle WriteableAreaResize(); prevScreenWidth = Screen.width; prevScreenHeight = Screen.height; fv.DrawCircle(); } //AA: Draw the writeable area fv.Apply(); for (int i = 0; i < fv.m_filterOutputTexture.Count; i++) { GUI.DrawTexture(new Rect(fv.filterOutputLocX, fv.filterOutputLocY, fv.filterOutputWidth, fv.filterOutputHeight), fv.m_filterOutputTexture[i]); } // Cursor m_gotNewFubiCoordinates = false; if (Fubi.isInitialized()) { // Take closest user uint userID = Fubi.getClosestUserID(); if (userID != m_currentUser) { m_currentUser = userID; m_lastCalibrationSucceded = false; } if (userID > 0) { if (!m_lastCalibrationSucceded) { m_lastCalibrationSucceded = calibrateCursorMapping(m_currentUser); } FubiUtils.SkeletonJoint joint = FubiUtils.SkeletonJoint.RIGHT_HAND; FubiUtils.SkeletonJoint relJoint = FubiUtils.SkeletonJoint.RIGHT_SHOULDER; // Get hand and shoulder position and check their confidence double timeStamp; float handX, handY, handZ, confidence; Fubi.getCurrentSkeletonJointPosition(userID, joint, out handX, out handY, out handZ, out confidence, out timeStamp); if (confidence > 0.5f) { float relX, relY, relZ; Fubi.getCurrentSkeletonJointPosition(userID, relJoint, out relX, out relY, out relZ, out confidence, out timeStamp); if (confidence > 0.5f) { // AA: Filtering should happen here for the hand and relative joints separately // If true, use the smoothed joints for calculating screen coordinates fm.UpdateJointFilters(new Vector3(handX, handY, handZ), new Vector3(relX, relY, relZ)); for (int i = 0; i < fm.filters.Count; i++) { if (m_bUseJointFiltering) { //Debug.Log ("Prehand " + new Vector3(handX, handY, handZ) + " relJoint " + new Vector3(relX, relY, relZ)); Vector3 handPos = fm.joints[i]; // filter.Update(new Vector3(handX, handY, handZ), Filter.JOINT_TYPE.JOINT); Vector3 relJointPos = fm.relativeJoints[i]; //filter.Update(new Vector3(relX, relY, relZ), Filter.JOINT_TYPE.RELATIVEJOINT); //Debug.Log ("hand " + handPos + " relJoint " + relJointPos); handZ = handPos.z; handY = handPos.y; handX = handPos.x; relZ = relJointPos.z; relY = relJointPos.y; relX = relJointPos.x; m_relativeCursorPosition = fm.relativeCursorPosition[i]; } // AA: End // Take relative coordinates float zDiff = handZ - relZ; float yDiff = handY - relY; float xDiff = handX - relX; // Check if hand is enough in front of shoulder if ((yDiff > 0 && zDiff < -150.0f) || (Mathf.Abs(xDiff) > 150.0f && zDiff < -175.0f) || zDiff < -225.0f) { // Now get the possible cursor position // Convert to screen coordinates float newX, newY; float mapX = m_mapping.x; newX = (xDiff - mapX) / m_mapping.width; newY = (m_mapping.y - yDiff) / m_mapping.height; // Flip y for the screen coordinates // Filtering // New coordinate is weighted more if it represents a longer distance change // This should reduce the lagging of the cursor on higher distances, but still filter out small jittering float changeX = newX - m_relativeCursorPosition.x; float changeY = newY - m_relativeCursorPosition.y; if (changeX != 0 || changeY != 0 && timeStamp != m_timeStamp) { float changeLength = Mathf.Sqrt(changeX * changeX + changeY * changeY); float filterFactor = changeLength; //Mathf.Sqrt(changeLength); if (filterFactor > 1.0f) { filterFactor = 1.0f; } // Apply the tracking to the current position with the given filter factor // AA: Filtering should happen here for joint-to-relativejoint (VECTOR) filtering // AA: filtering code Vector2 tempNew = new Vector2(newX, newY); fm.UpdateVectorFilters(m_relativeCursorPosition, tempNew, filterFactor); // If true, use the calculated factor for smoothing, else just use the new if (m_bUseVectorFiltering) { m_relativeCursorPosition = fm.vectors[i]; //filter.Update(m_relativeCursorPosition, tempNew, filterFactor); } else // Just give equal weight to both { m_relativeCursorPosition = filter.Update(m_relativeCursorPosition, tempNew, 0.5f); } // AA: Calculate all filters // fm.UpdateVectorFilters(m_relativeCursorPosition, tempNew, filterFactor); m_timeStamp = timeStamp; // Send it, but only if it is more or less within the screen if (m_relativeCursorPosition.x > -0.1f && m_relativeCursorPosition.x < 1.1f && m_relativeCursorPosition.y > -0.1f && m_relativeCursorPosition.y < 1.1f) { MoveMouse(m_relativeCursorPosition.x, m_relativeCursorPosition.y, i); // Each filter must store it's own value of relative position, absolute and previous absolute positions fm.relativeCursorPosition[i] = m_relativeCursorPosition; fm.absPixelPosition[i] = m_absPixelPosition; fm.prevAbsPixelPosition[i] = m_previousAbsPixelPosition; DrawFilterOutputs(i); m_gotNewFubiCoordinates = true; m_lastCursorChangeDoneByFubi = true; } } } } } } } } // AA: FUBI does not move mouse if the confidence value is too low if (!m_gotNewFubiCoordinates) // AA: this only executes when input is coming from mouse { // Got no mouse coordinates from fubi this frame Vector2 mousePos = Input.mousePosition; // Only move mouse if it wasn't changed by fubi the last time or or it really has changed if (!m_lastCursorChangeDoneByFubi || mousePos != m_lastMousePos) { //AA: Old code for cursor placement m_relativeCursorPosition.x = mousePos.x / (float)Screen.width; m_relativeCursorPosition.y = 1.0f - (mousePos.y / (float)Screen.height); // Get mouse X and Y position as a percentage of screen width and height MoveActualMouse(m_relativeCursorPosition.x, m_relativeCursorPosition.y, true); m_lastMousePos = mousePos; m_lastCursorChangeDoneByFubi = false; } } }
// Initialization void Start() { //AA: Filter visalization related initializations filter = new Filter(); fv = new FilterVisualization(); fm = new FilterManager(); fv.filterOutputLocX = Screen.width / 3 - 50; fv.Initialise(); fv.DrawCircle(); m_colorTextureDictionary = new Dictionary <string, Texture2D>(); foreach (Texture2D tex in m_colorTextures) { m_colorTextureDictionary.Add(tex.name, tex); } LoadFilters(); // First set instance so Fubi.release will not be called while destroying old objects instance = this; // Remain this instance active until new one is created DontDestroyOnLoad(this); // Destroy old instance of Fubi object[] objects = GameObject.FindObjectsOfType(typeof(FubiUnity)); if (objects.Length > 1) { Destroy(((FubiUnity)objects[0])); } m_lastMouseClick = 0; m_lastGesture = 0; // Init FUBI if (!m_disableFubi) { // Only init if not already done if (!Fubi.isInitialized()) { Fubi.init(new FubiUtils.SensorOptions(new FubiUtils.StreamOptions(640, 480, 30), new FubiUtils.StreamOptions(640, 480, 30), new FubiUtils.StreamOptions(-1, -1, -1), FubiUtils.SensorType.OPENNI2), new FubiUtils.FilterOptions()); if (!Fubi.isInitialized()) { Debug.Log("Fubi: FAILED to initialize Fubi!"); } else { Debug.Log("Fubi: initialized!"); } } } else { m_disableTrackingImage = true; } // Initialize debug image // m_depthMapTexture = new Texture2D((int)(m_xRes / m_factor), (int)(m_yRes / m_factor), TextureFormat.RGBA32, false); // m_depthMapPixels = new Color[(int)((m_xRes / m_factor) * (m_yRes / m_factor))]; // m_rawImage = new byte[(int)(m_xRes * m_yRes * 4)]; m_userImageTexture = null; // Disable system cursor if (m_defaultCursor != null && m_disableFubi == false) { Screen.showCursor = false; } else { Screen.showCursor = true; } // Default mapping values m_mapping.x = -100.0f; m_mapping.y = 200.0f; m_mapping.height = 550.0f; // Get screen aspect m_aspect = (float)Screen.width / (float)Screen.height; // Calculated Map width with aspect m_mapping.width = m_mapping.height / m_aspect; if (Fubi.isInitialized()) { // Clear old gesture recognizers Fubi.clearUserDefinedRecognizers(); // And (re)load them if (Fubi.loadRecognizersFromXML("UnitySampleRecognizers.xml")) { Debug.Log("Fubi: gesture recognizers 'BarRecognizers.xml' loaded!"); } else { Debug.Log("Fubi: loading XML recognizers failed!"); } // load mouse control recognizers if (Fubi.loadRecognizersFromXML("MouseControlRecognizers.xml")) { Debug.Log("Fubi: mouse control recognizers loaded!"); } else { Debug.Log("Fubi: loading mouse control recognizers failed!"); } } }