public float length; // length of bone /// <summary> /// Creates a new MoCap data object. /// </summary> /// public MoCapData(MoCapDataBuffer buffer) { this.buffer = buffer; pos = new Vector3(); rot = new Quaternion(); tracked = false; length = 0; }
//// <summary> /// Called once per frame. /// </summary> /// void Update() { if (controllingBone == null) { return; } // update bones foreach (KeyValuePair <Bone, MoCapDataBuffer> entry in dataBuffers) { Bone bone = entry.Key; MoCapDataBuffer buffer = entry.Value; GameObject obj = buffer.GameObject; // pump data through buffer MoCapData data = buffer.Process(bone); // update hierarchy object if (data.tracked) { if ((trackingUsage == TrackingUsage.RotationOnly) || (trackingUsage == TrackingUsage.PositionAndRotation)) { obj.transform.localRotation = data.rot; } if ((trackingUsage == TrackingUsage.PositionOnly) || (trackingUsage == TrackingUsage.PositionAndRotation)) { obj.transform.localPosition = data.pos; } obj.SetActive(true); } else { // bone not tracked anymore, freeze or disable if (trackingLostBehaviour == TrackingLostBehaviour.Disable) { obj.SetActive(false); } } } }
/// <summary> /// Creates a hierarchy of a selected bone of an actor. /// </summary> /// <param name="actor">actor to use</param> /// private void CreateHierarchy(Actor actor) { // create node for containing all the hierarchy objects rootNode = new GameObject(); rootNode.name = this.name + "_Root"; rootNode.transform.parent = this.transform.parent; rootNode.transform.localPosition = Vector3.zero; rootNode.transform.localRotation = Quaternion.identity; rootNode.transform.localScale = Vector3.one; // create hierarchy GameObject boneNode = rootNode; foreach (Bone bone in controllingBone.chain) { // add empty for position/orientation boneNode = new GameObject(); boneNode.name = bone.name; if (bone.parent != null) { // attach to parent node GameObject parentObject = dataBuffers[bone.parent].GameObject; boneNode.transform.parent = parentObject.transform; } else { // no parent = root bone > attach to root node boneNode.transform.parent = rootNode.transform; } boneNode.transform.localScale = Vector3.one; dataBuffers[bone] = new MoCapDataBuffer(bone.name, this.gameObject, boneNode); } // move this transform to the end of the hierarchy this.transform.parent = boneNode.transform; this.transform.localPosition = Vector3.zero; this.transform.localRotation = Quaternion.identity; }