public void Render() { var movement = Time.deltaTime * SPEED; var headPos = processJoint(Skeleton.GetJoint(Skeleton.HEAD)); if (headObject == null) { headObject = Instantiate(HeadObjectPrefab) as GameObject; headObject.transform.SetParent(this.transform, false); headObject.transform.localPosition = headPos; } var headMove = Vector3.Lerp(headObject.transform.localPosition, headPos, movement); headObject.transform.localPosition = headMove; foreach (var jArray in SkeletonUtils.GetOrderedJointPairs(Skeleton)) { try{ JointObject jointObject = null; var key = String.Format("{0}-{1}-{2}", Skeleton.SkeletonID, jArray[0].Item1, jArray[1].Item1); jointMap.TryGetValue(key, out jointObject); var v1 = processJoint(jArray[0].Item2); var v2 = processJoint(jArray[1].Item2); if (jointObject == null) { jointObject = new JointObject() { Start = v1, End = v2, GameObject = Instantiate(JointConnectionPrefab) }; drawLine(jointObject.GameObject, v1, v2, movement); jointObject.GameObject.name = key; jointMap.Add(key, jointObject); jointObject.GameObject.transform.SetParent(this.transform, false); } else { var v1Lerp = Vector3.Lerp(jointObject.Start, v1, movement); var v2Lerp = Vector3.Lerp(jointObject.End, v2, movement); drawLine(jointObject.GameObject, v1Lerp, v2Lerp, movement); jointObject.Start = v1; jointObject.End = v2; jointMap.Add(key, jointObject); } } catch (Exception ex) { Debug.LogWarning(ex.ToString()); } } foreach (var joint in Skeleton.Joints) { try{ GameObject jointPoint = null; partMap.TryGetValue(joint.Key, out jointPoint); var lerp = true; if (jointPoint == null) { lerp = false; if (joint.Key == Skeleton.L_HAND || joint.Key == Skeleton.R_HAND || joint.Key == Skeleton.L_FOOT || joint.Key == Skeleton.R_FOOT) { jointPoint = Instantiate(HandPrefab) as GameObject; } else if (joint.Key != Skeleton.L_WRIST && joint.Key != Skeleton.R_WRIST) { jointPoint = Instantiate(JointPrefab) as GameObject; } jointPoint.name = joint.Key; partMap.Add(joint.Key, jointPoint); jointPoint.transform.SetParent(this.transform, false); } if (lerp) { var jointMove = Vector3.Lerp(jointPoint.transform.localPosition, processJoint(joint.Value), movement * 2.5f); jointPoint.transform.localPosition = jointMove; } else { jointPoint.transform.localPosition = processJoint(joint.Value); } } catch (Exception ex) { } } }
void DrawBoneController(int controllerID, Transform boneTransform, List <Transform> childs, nuitrack.JointType jointType, float size) { childs ??= new List <Transform>(); Event e = Event.current; //We divide the size by 2, since strange behavior is detected when an element falls into the selection. //The size of the visual element is set by the diameter, and the selection area by the radius. Handles.SphereHandleCap(controllerID, boneTransform.position, boneTransform.rotation, size / 2, EventType.Layout); switch (e.GetTypeForControl(controllerID)) { case EventType.MouseDown: if (HandleUtility.nearestControl == controllerID && e.button == 0) { // Respond to a press on this handle. Drag starts automatically. GUIUtility.hotControl = controllerID; GUIUtility.keyboardControl = controllerID; e.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == controllerID && e.button == 0) { GUIUtility.hotControl = 0; e.Use(); OnBoneSetActive?.Invoke(jointType); } break; case EventType.Repaint: Color handlesColor = Handles.color; if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == controllerID) { handlesColor = Color.Lerp(Handles.color, hoverColor, 0.5f); } if (SelectedJoint == jointType) { handlesColor = Color.Lerp(handlesColor, selectColor, 0.5f); } using (new HandlesColor(handlesColor)) { Handles.SphereHandleCap(controllerID, boneTransform.position, boneTransform.rotation, size, EventType.Repaint); foreach (Transform child in childs) { SkeletonUtils.DrawBone(boneTransform.position, child.position); } } break; case EventType.KeyDown: if ((e.keyCode == KeyCode.Backspace || e.keyCode == KeyCode.Delete) && GUIUtility.keyboardControl == controllerID) { GUIUtility.keyboardControl = 0; e.Use(); OnBoneDelete?.Invoke(jointType); } break; } }