public void ExpandToAnimation(bool logExpansion = true) { var oldLimits = Limits; var newLimits = new Dictionary <string, BVHElementLimit>(); FitAnimation(); foreach (string name in Limits.Keys) { if (oldLimits.ContainsKey(name)) { newLimits[name] = new BVHElementLimit(); if (Limits[name].AngularXLow < oldLimits[name].AngularXLow) { newLimits[name].AngularXLow = Limits[name].AngularXLow; if (logExpansion) { Debug.Log($"Expanded {name} AngularXLow limit from {oldLimits[name].AngularXLow} to {newLimits[name].AngularXLow}"); } } else { newLimits[name].AngularXLow = oldLimits[name].AngularXLow; } if (Limits[name].AngularXHigh > oldLimits[name].AngularXHigh) { newLimits[name].AngularXHigh = Limits[name].AngularXHigh; if (logExpansion) { Debug.Log($"Expanded {name} AngularXHigh limit from {oldLimits[name].AngularXHigh} to {newLimits[name].AngularXHigh}"); } } else { newLimits[name].AngularXHigh = oldLimits[name].AngularXHigh; } if (Limits[name].AngularYLow < oldLimits[name].AngularYLow) { newLimits[name].AngularYLow = Limits[name].AngularYLow; if (logExpansion) { Debug.Log($"Expanded {name} AngularYLow limit from {oldLimits[name].AngularYLow} to {newLimits[name].AngularYLow}"); } } else { newLimits[name].AngularYLow = oldLimits[name].AngularYLow; } if (Limits[name].AngularYHigh > oldLimits[name].AngularYHigh) { newLimits[name].AngularYHigh = Limits[name].AngularYHigh; if (logExpansion) { Debug.Log($"Expanded {name} AngularYHigh limit from {oldLimits[name].AngularYHigh} to {newLimits[name].AngularYHigh}"); } } else { newLimits[name].AngularYHigh = oldLimits[name].AngularYHigh; } if (Limits[name].AngularZLow < oldLimits[name].AngularZLow) { newLimits[name].AngularZLow = Limits[name].AngularZLow; if (logExpansion) { Debug.Log($"Expanded {name} AngularZLow limit from {oldLimits[name].AngularZLow} to {newLimits[name].AngularZLow}"); } } else { newLimits[name].AngularZLow = oldLimits[name].AngularZLow; } if (Limits[name].AngularZHigh > oldLimits[name].AngularZHigh) { newLimits[name].AngularZHigh = Limits[name].AngularZHigh; if (logExpansion) { Debug.Log($"Expanded {name} AngularZHigh limit from {oldLimits[name].AngularZHigh} to {newLimits[name].AngularZHigh}"); } } else { newLimits[name].AngularZHigh = oldLimits[name].AngularZHigh; } } else { Debug.LogWarning($"\"{name}\" not in original limits"); newLimits[name] = Limits[name]; } } Limits = newLimits; }
private void GenerateElements(Transform parent, BVHElement bvh) { GameObject bodyPartGO = new GameObject() { name = bvh.Name }; bodyPartGO.transform.parent = parent; bodyPartGO.transform.localPosition = bvh.Offset / 10f; BVHElementLimit bvhLimit = bvhLimits.Limits[bvh.Name]; float meanXLimit = (bvhLimit.AngularXLow + bvhLimit.AngularXHigh) / 2f; float meanYLimit = (bvhLimit.AngularYLow + bvhLimit.AngularYHigh) / 2f; float meanZLimit = (bvhLimit.AngularZLow + bvhLimit.AngularZHigh) / 2f; bodyPartGO.transform.localRotation = Quaternion.identity; bodyPartGO.transform.localRotation *= Quaternion.AngleAxis(meanXLimit, Vector3.right); bodyPartGO.transform.localRotation *= Quaternion.AngleAxis(meanYLimit, Vector3.up); bodyPartGO.transform.localRotation *= Quaternion.AngleAxis(meanZLimit, Vector3.forward); bodyPartGO.AddComponent <Rigidbody>(); //bodyPartGO.GetComponent<Rigidbody>().useGravity = false; BodyPart bodyPart = new BodyPart(bodyPartGO, bvh); BodyParts.Add(bodyPart); if (BodyData.bodySpec.ContainsKey(bvh.Name)) { foreach (PrimitiveSpec ps in BodyData.bodySpec[bvh.Name].Shapes) { GameObject go = GameObject.CreatePrimitive(ps.Type); go.transform.parent = bodyPart.GameObject.transform; go.transform.localPosition = ps.Position; go.transform.localRotation = ps.Rotation; go.transform.localScale = ps.Scale; go.layer = 8; // Body if (Material) { go.GetComponent <Renderer>().material = Material; } } if (bvh.Name != "Hips") { bodyPartGO.AddComponent <ConfigurableJoint>(); ConfigurableJoint cj = bodyPartGO.GetComponent <ConfigurableJoint>(); cj.connectedBody = parent.GetComponent <Rigidbody>(); cj.xMotion = ConfigurableJointMotion.Locked; cj.yMotion = ConfigurableJointMotion.Locked; cj.zMotion = ConfigurableJointMotion.Locked; cj.anchor = BodyData.bodySpec[bvh.Name].JointParameters.Anchor; cj.angularXMotion = ConfigurableJointMotion.Limited; cj.angularYMotion = ConfigurableJointMotion.Limited; cj.angularZMotion = ConfigurableJointMotion.Limited; cj.lowAngularXLimit = new SoftJointLimit { limit = bvhLimit.AngularXLow - meanXLimit }; cj.highAngularXLimit = new SoftJointLimit { limit = bvhLimit.AngularXHigh - meanXLimit }; cj.angularYLimit = new SoftJointLimit { limit = bvhLimit.AngularYHigh - meanYLimit }; cj.angularZLimit = new SoftJointLimit { limit = bvhLimit.AngularZHigh - meanZLimit }; } } foreach (BVHElement child in bvh.Children) { GenerateElements(bodyPart.GameObject.transform, child); } }
public BVHLimits(BVHData bvhData) { this.bvhData = bvhData; Limits = new Dictionary <string, BVHElementLimit>(); // default limits // (left and right reversed) Limits["Hips"] = new BVHElementLimit { AngularXLow = -180f, AngularXHigh = 180f, AngularYLow = -180f, AngularYHigh = 180f, AngularZLow = -180f, AngularZHigh = 180f }; Limits["LeftUpLeg"] = new BVHElementLimit { AngularXLow = -65f, AngularXHigh = 30f, AngularYLow = -45f, AngularYHigh = 48f, AngularZLow = -15f, AngularZHigh = 45f }; Limits["LeftLeg"] = new BVHElementLimit { AngularXLow = -3f, AngularXHigh = 150f, AngularYLow = -15f, AngularYHigh = 15f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["LeftFoot"] = new BVHElementLimit { AngularXLow = -25f, AngularXHigh = 35f, AngularYLow = -19f, AngularYHigh = 15f, AngularZLow = -22f, AngularZHigh = 16f }; Limits["RightUpLeg"] = new BVHElementLimit { AngularXLow = -65f, AngularXHigh = 30f, AngularYLow = -48f, AngularYHigh = 45f, AngularZLow = -45f, AngularZHigh = 15f }; Limits["RightLeg"] = new BVHElementLimit { AngularXLow = -3f, AngularXHigh = 150f, AngularYLow = -15f, AngularYHigh = 15f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["RightFoot"] = new BVHElementLimit { AngularXLow = -25f, AngularXHigh = 35f, AngularYLow = -15f, AngularYHigh = 19f, AngularZLow = -16f, AngularZHigh = 22f }; Limits["Spine"] = new BVHElementLimit { AngularXLow = -25f, AngularXHigh = 45f, AngularYLow = -25f, AngularYHigh = 25f, AngularZLow = -20f, AngularZHigh = 20f }; Limits["Spine1"] = new BVHElementLimit { AngularXLow = -27f, AngularXHigh = 40f, AngularYLow = -20f, AngularYHigh = 20f, AngularZLow = -20f, AngularZHigh = 20f }; Limits["Neck"] = new BVHElementLimit { AngularXLow = -20f, AngularXHigh = 20f, AngularYLow = -25f, AngularYHigh = 25f, AngularZLow = -10f, AngularZHigh = 10f }; Limits["Head"] = new BVHElementLimit { AngularXLow = -35f, AngularXHigh = 17f, AngularYLow = -12f, AngularYHigh = 12f, AngularZLow = -15f, AngularZHigh = 15f }; Limits["LeftShoulder"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = -30f, AngularYHigh = 30f, AngularZLow = -12f, AngularZHigh = 31f }; Limits["LeftArm"] = new BVHElementLimit { AngularXLow = -30f, AngularXHigh = 35f, AngularYLow = -45f, AngularYHigh = 35f, AngularZLow = -85f, AngularZHigh = 45f }; // angularY 15 -> 3 Limits["LeftForeArm"] = new BVHElementLimit { AngularXLow = -85f, AngularXHigh = 45f, AngularYLow = -130f, AngularYHigh = 3f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["LeftHand"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = -20f, AngularYHigh = 20f, AngularZLow = -80f, AngularZHigh = 70f }; Limits["RightShoulder"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = -30f, AngularYHigh = 30f, AngularZLow = -31f, AngularZHigh = 12f }; Limits["RightArm"] = new BVHElementLimit { AngularXLow = -30f, AngularXHigh = 35f, AngularYLow = -35f, AngularYHigh = 45f, AngularZLow = -45f, AngularZHigh = 85f }; // angularY 15 -> 3 Limits["RightForeArm"] = new BVHElementLimit { AngularXLow = -85f, AngularXHigh = 45f, AngularYLow = -3f, AngularYHigh = 130f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["RightHand"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = -20f, AngularYHigh = 20f, AngularZLow = -70f, AngularZHigh = 80f }; // from data Limits["LeftToeBase"] = new BVHElementLimit { AngularXLow = -28f, AngularXHigh = 32f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["RightToeBase"] = new BVHElementLimit { AngularXLow = -28f, AngularXHigh = 32f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["LeftHandThumb"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["L_Wrist_End"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["RightHandThumb"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["R_Wrist_End"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; Limits["End Site"] = new BVHElementLimit { AngularXLow = 0f, AngularXHigh = 0f, AngularYLow = 0f, AngularYHigh = 0f, AngularZLow = 0f, AngularZHigh = 0f }; }