void MeasureBoneLength(Body body, JointType joint1, JointType joint2, Text text, int stickmanJointIndex1, int stickmanJointIndex2, out float worldLength, out float screenLength) { worldLength = (body.Joints[joint1].WorldPosition - body.Joints[joint2].WorldPosition).Length.ToCentimeters(); screenLength = (adapter.WorldToDepthSpace(body.Joints[joint1].WorldPosition) - adapter.WorldToDepthSpace(body.Joints[joint2].WorldPosition)).Length; text.text = worldLength.ToString("N0") + "cm"; }
void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { UnityEngine.SceneManagement.SceneManager.LoadScene(0); return; } if (adapter == null) { return; } if (adapter.SensorType != sensorType) { adapter.SensorType = sensorType; } Frame frame = adapter.UpdateFrame(); if (frame != null) { if (frame.ImageData != null) { imageViewTexture = ValidateTexture(imageViewTexture, frame.ImageWidth, frame.ImageHeight, imageViewMaterial, imageViewTransform); if (imageViewTexture != null) { imageViewTexture.LoadRawTextureData(frame.ImageData); imageViewTexture.Apply(false); } } depthFilter.UpdateFilter(frame); if (depthFilter.Result != null) { depthViewTexture = ValidateTexture(depthViewTexture, frame.DepthWidth, frame.DepthHeight, depthViewMaterial, depthViewTransform); if (depthViewTexture != null) { depthViewTexture.LoadRawTextureData(depthFilter.Result); depthViewTexture.Apply(false); } } Body body = frame.GetClosestBody(); if (body != null) { imageViewStickman.UpdateStickman(adapter, frame, body, imageViewTransform, Visualization.Image); depthViewStickman.UpdateStickman(adapter, frame, body, depthViewTransform, Visualization.Depth); if (!hadBody) { hadBody = true; model1.AvatarRoot.SetActive(true); model2.AvatarRoot.SetActive(true); } model1.DoAvateering(body); model1.Bones[(int)JointType.SpineBase].Transform.position = adapter.WorldToImageSpace(body.Joints[JointType.SpineBase].WorldPosition). GetPositionOnPlane(frame.ImageWidth, frame.ImageHeight, imageViewTransform.position, imageViewTransform.rotation, imageViewTransform.localScale); model2.DoAvateering(body); model2.Bones[(int)JointType.SpineBase].Transform.position = adapter.WorldToDepthSpace(body.Joints[JointType.SpineBase].WorldPosition). GetPositionOnPlane(frame.DepthWidth, frame.DepthHeight, depthViewTransform.position, depthViewTransform.rotation, depthViewTransform.localScale); if (Vector3D.Angle(Vector3D.Up, body.Joints[JointType.Neck].WorldPosition - body.Joints[JointType.SpineBase].WorldPosition) < 10) { bodyImageSize = (imageViewStickman.jointPoints[0].position - imageViewStickman.jointPoints[4].position).magnitude; bodyDepthSize = (depthViewStickman.jointPoints[0].position - depthViewStickman.jointPoints[4].position).magnitude; float scale = bodyImageSize / model1Size; model1.AvatarRoot.transform.localScale = new Vector3(scale, scale, scale); scale = bodyDepthSize / model2Size; model2.AvatarRoot.transform.localScale = new Vector3(scale, scale, scale); } } else if (hadBody) { hadBody = false; model1.AvatarRoot.SetActive(false); model2.AvatarRoot.SetActive(false); } } }
public void UpdateStickman(SensorAdapter adapter, Frame frame, Body body, Transform viewPlane, Visualization visualization) { bool isPlaybackFrame = frame != null && frame.IsPlaybackFrame; if ((!isPlaybackFrame && adapter == null) || frame == null || body == null || viewPlane == null) { return; } if (JointTypes == null) { Initialize(); } Vector3D viewPlanePosition = viewPlane.position; LightBuzz.Quaternion viewPlaneRotation = viewPlane.rotation; Vector3D viewPlaneScale = viewPlane.localScale; if (viewPlane.GetType() == typeof(RectTransform)) { viewPlaneScale.Set(viewPlaneScale.X * ((RectTransform)viewPlane).root.localScale.x * ((RectTransform)viewPlane).rect.size.x, viewPlaneScale.Y * ((RectTransform)viewPlane).root.localScale.y * ((RectTransform)viewPlane).rect.size.y, 1); } float smoothness = Mathf.Lerp(1f, Time.deltaTime, updateSmoothness); Vector3D currPosition; for (int i = 0; i < JointTypes.Length; i++) { if (visualization == Visualization.Image) { if (isPlaybackFrame) { currPosition = VideoPlayer.WorldToImageSpace(JointTypes[i], frame, body).GetPositionOnPlane( frame.ImageWidth, frame.ImageHeight, viewPlanePosition, viewPlaneRotation, viewPlaneScale); } else { currPosition = adapter.WorldToImageSpace(body.Joints[JointTypes[i]].WorldPosition).GetPositionOnPlane( frame.ImageWidth, frame.ImageHeight, viewPlanePosition, viewPlaneRotation, viewPlaneScale); } } else { if (isPlaybackFrame) { currPosition = VideoPlayer.WorldToDepthSpace(JointTypes[i], frame, body).GetPositionOnPlane( frame.DepthWidth, frame.DepthHeight, viewPlanePosition, viewPlaneRotation, viewPlaneScale); } else { currPosition = adapter.WorldToDepthSpace(body.Joints[JointTypes[i]].WorldPosition).GetPositionOnPlane( frame.DepthWidth, frame.DepthHeight, viewPlanePosition, viewPlaneRotation, viewPlaneScale); } } jointPoints[i].position = Vector3D.Lerp(jointPoints[i].position, currPosition, smoothness); } jointLines[0].SetPosition(0, jointPoints[0].position); jointLines[0].SetPosition(1, jointPoints[1].position); jointLines[0].SetPosition(2, jointPoints[2].position); jointLines[0].SetPosition(3, jointPoints[3].position); jointLines[0].SetPosition(4, jointPoints[4].position); jointLines[1].SetPosition(0, jointPoints[8].position); jointLines[1].SetPosition(1, jointPoints[7].position); jointLines[1].SetPosition(2, jointPoints[6].position); jointLines[1].SetPosition(3, jointPoints[5].position); jointLines[1].SetPosition(4, jointPoints[2].position); jointLines[1].SetPosition(5, jointPoints[9].position); jointLines[1].SetPosition(6, jointPoints[10].position); jointLines[1].SetPosition(7, jointPoints[11].position); jointLines[1].SetPosition(8, jointPoints[12].position); jointLines[2].SetPosition(0, jointPoints[16].position); jointLines[2].SetPosition(1, jointPoints[15].position); jointLines[2].SetPosition(2, jointPoints[14].position); jointLines[2].SetPosition(3, jointPoints[13].position); jointLines[2].SetPosition(4, jointPoints[4].position); jointLines[2].SetPosition(5, jointPoints[17].position); jointLines[2].SetPosition(6, jointPoints[18].position); jointLines[2].SetPosition(7, jointPoints[19].position); jointLines[2].SetPosition(8, jointPoints[20].position); }