Пример #1
0
        public override NUISkeleton MapSkeleton(NUISkeleton inputSkeleton)
        {
            NUISkeleton outputSkeleton = new NUISkeleton(OutputStructure.Structure);
            NUIJoint    parentJoint    = OutputStructure.Joints[OutputStructure.Structure.RootNode];

            outputSkeleton.Joints.Add(OutputStructure.Structure.RootNode, parentJoint);

            // Needs to iterate in order.
            foreach (var jointType in OutputStructure.Structure.OrderedJointTypes)
            {
                if (isJointMasked(jointType))
                {
                    NUIJoint maskedJoint = OutputStructure.Joints[jointType];
                    outputSkeleton.Joints.Add(jointType, maskedJoint);
                    continue;
                }
                if (OutputStructure.Structure.IsJointInStructure(jointType))
                {
                    if (!OutputStructure.Structure.IsJointAnExtremity(jointType))
                    {
                        // Build the skeleton for this current joint.
                        RotateJoint(jointType, inputSkeleton, outputSkeleton);
                    }
                    else
                    {
                        // If extremity, just copy from OutputStructure.
                        NUIJoint extremity = OutputStructure.Joints[jointType];
                        outputSkeleton.Joints.Add(jointType, extremity);
                    }
                }
            }

            return(outputSkeleton);
        }
Пример #2
0
 public void Add(string filterName, NUISkeleton skeleton)
 {
     filterData.Add(new FilterSkeleton()
     {
         FilterName = filterName, Skeleton = skeleton
     });
 }
Пример #3
0
        private void swapJoints(NUISkeleton skeleton, NUIJointType left, NUIJointType right)
        {
            NUIJoint leftJoint  = new NUIJoint(right, skeleton.Joints[left].Position, skeleton.Joints[left].Rotation, skeleton.Joints[left].Inferred);
            NUIJoint rightJoint = new NUIJoint(left, skeleton.Joints[right].Position, skeleton.Joints[right].Rotation, skeleton.Joints[right].Inferred);

            skeleton.Joints[right] = leftJoint;
            skeleton.Joints[left]  = rightJoint;
        }
        public override Vector3 GetHipPosition(NUISkeleton skeleton)
        {
            if (!skeleton.Joints.Keys.Contains <NUIJointType>(NUIJointType.SpineBase))
            {
                Debug.Log("skeleton has problem: " + skeleton.Joints.Count);
            }
            NUIJoint joint            = skeleton.Joints[NUIJointType.SpineBase];
            Vector3  positionInMeters = joint.Position;

            return(positionInMeters);
        }
Пример #5
0
        internal void AddNewFrame(NUISkeleton skeleton, double totalMilliseconds)
        {
            var data = new CacheFrameData();

            data.rawSkeleton = skeleton;
            data.elapsedTime = totalMilliseconds;

            frameData.Add(data);

            if (frameData.Count > cacheSize)
            {
                frameData.RemoveAt(0);
            }
        }
Пример #6
0
        /// <summary>
        /// Receive frame data from the Input profile.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void MocapProfile_SkeletonFrameCaptured(object sender, FrameDataEventArgs args)
        {
            if (MappingProfile != null && OutputProfile != null)
            {
                var elapsedTime = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                var skeleton    = NUICaptureHelper.GetNUISkeleton(args.SkeletonFrameData);

                // Cache raw data
                cache.AddNewFrame(skeleton, elapsedTime.TotalMilliseconds);

                // Filter the raw input with enabled filters.
                NUISkeleton filtered = skeleton;
                foreach (MocapFilter filter in preMapFilters)
                {
                    if (filter.Enabled)
                    {
                        filtered = filter.Filter(cache);
                        cache.AddFiltered(filter.Name, filtered);
                    }
                }

                // Convert the input skeleton to the normalized skeleton (Unity)
                NUISkeleton mapped   = MappingProfile.MapSkeleton(skeleton);
                Vector3     position = MappingProfile.GetHipPosition(skeleton);
                cache.AddMapped(mapped);

                // Apply any post-mapped filters selected by the user.
                filtered = mapped;
                foreach (MocapFilter filter in postMapFilters)
                {
                    if (filter.Enabled)
                    {
                        filtered = filter.Filter(cache);
                        cache.AddFiltered(filter.Name, filtered);
                    }
                }

                // Send the mapped and filtered skeleton to the output profile.
                cache.AddResult(filtered);
                OutputProfile.UpdatePreview(filtered, position);

                if (session != null && captureState == RecordingState.Recording)
                {
                    // Add frame to session.
                    MocapSessionKeyframe kf = new MocapSessionKeyframe(args.SkeletonFrameData, (int)elapsedTime.TotalMilliseconds);
                    session.CaptureData.Add(kf);
                }
            }
        }
Пример #7
0
        private void InputProfile_FrameSelected(object sender, FrameSelectedEventArgs args)
        {
            previousArgs = args;
            if (MappingProfile != null && OutputProfile != null)
            {
                cache = new CaptureCache();

                // Build the cache
                int startingFrame = BaseSystem.Math.Max(args.frame - cache.CacheSize, 0);

                for (int i = startingFrame; i <= args.frame; i++)
                {
                    var elapsedTime = args.session.CaptureData[i].ElapsedMilliseconds;
                    var skeleton    = NUICaptureHelper.GetNUISkeleton(args.session.CaptureData[i].Skeleton);

                    cache.AddNewFrame(skeleton, elapsedTime);

                    // Filter the raw input with enabled filters.
                    NUISkeleton filtered = skeleton;
                    foreach (MocapFilter filter in preMapFilters)
                    {
                        if (filter.Enabled)
                        {
                            filtered = filter.Filter(cache);
                            cache.AddFiltered(filter.Name, filtered);
                        }
                    }

                    // Convert the input skeleton to the normalized skeleton (Unity)
                    NUISkeleton mapped = MappingProfile.MapSkeleton(filtered);
                    cache.AddMapped(mapped);

                    // Apply any post-mapped filters selected by the user.
                    filtered = mapped;
                    foreach (MocapFilter filter in postMapFilters)
                    {
                        if (filter.Enabled)
                        {
                            filtered = filter.Filter(cache);
                            cache.AddFiltered(filter.Name, filtered);
                        }
                    }
                    cache.AddResult(filtered);
                }

                var s = NUICaptureHelper.GetNUISkeleton(args.session.CaptureData[args.frame].Skeleton);
                OutputProfile.UpdatePreview(cache.CurrentSkeleton, MappingProfile.GetHipPosition(s));
            }
        }
Пример #8
0
        public override NUISkeleton Filter(NUISkeleton input)
        {
            NUISkeleton output = input.Clone();

            Vector3 spineBasePosition = output.Joints[NUIJointType.SpineBase].Position;

            foreach (var joint in output.Joints)
            {
                joint.Value.Position -= spineBasePosition;
                joint.Value.Position  = Quaternion.AngleAxis(tilt, Vector3.right) * joint.Value.Position;
                joint.Value.Position += spineBasePosition;
            }

            return(output);
        }
Пример #9
0
        public override NUIHumanoidAnimation MapAnimation(NUIHumanoidAnimation animation)
        {
            NUIHumanoidAnimation mappedAnimation = new NUIHumanoidAnimation();

            foreach (NUIAnimationKeyframe kf in animation.Keyframes)
            {
                NUISkeleton mappedSkeleton = MapSkeleton(kf.Skeleton);

                NUIAnimationKeyframe newKF = new NUIAnimationKeyframe(mappedSkeleton, kf.ElapsedTime);

                mappedAnimation.AddKeyframe(newKF);
            }

            return(mappedAnimation);
        }
Пример #10
0
        public override void UpdatePreview(NUISkeleton skeleton, Vector3 position)
        {
            if (cinema_mocap_humanoid_instance != null)
            {
                HumanoidPoser poser     = cinema_mocap_humanoid_instance.GetComponent <HumanoidPoser>();
                var           rotations = new Dictionary <string, Quaternion>();

                foreach (var pair in skeleton.Joints)
                {
                    rotations.Add(NUIJointToColladaMapping(pair.Key), pair.Value.Rotation);
                }

                poser.SetWorldPosition(position);
                poser.SetRotations(rotations);
            }
        }
        public override NUIHumanoidAnimation MapAnimation(NUIHumanoidAnimation animation)
        {
            NUIHumanoidAnimation mappedAnimation = new NUIHumanoidAnimation();

            foreach (NUIAnimationKeyframe kf in animation.Keyframes)
            {
                Vector3 position = GetHipPosition(kf.Skeleton);

                NUISkeleton mappedSkeleton = MapSkeleton(kf.Skeleton);

                mappedSkeleton.Joints[NUIJointType.SpineBase].Position = position;
                NUIAnimationKeyframe newKF = new NUIAnimationKeyframe(mappedSkeleton, kf.ElapsedTime);

                mappedAnimation.AddKeyframe(newKF);
            }

            return(mappedAnimation);
        }
Пример #12
0
        public NUISkeleton GetSkeletonBeforeFilter(string filterName)
        {
            NUISkeleton skeleton = CurrentSkeleton;

            for (int i = 0; i < filterData.Count; i++)
            {
                if (filterData[i].FilterName == filterName)
                {
                    if (i == 0)
                    {
                        skeleton = rawSkeleton;
                    }
                    else
                    {
                        skeleton = filterData[i - 1].Skeleton;
                    }
                }
            }

            return(skeleton);
        }
Пример #13
0
        public override NUISkeleton Filter(NUISkeleton input)
        {
            NUISkeleton output = input.Clone();

            swapJoints(output, NUIJointType.HipLeft, NUIJointType.HipRight);
            swapJoints(output, NUIJointType.KneeLeft, NUIJointType.KneeRight);
            swapJoints(output, NUIJointType.AnkleLeft, NUIJointType.AnkleRight);
            swapJoints(output, NUIJointType.FootLeft, NUIJointType.FootRight);

            swapJoints(output, NUIJointType.ShoulderLeft, NUIJointType.ShoulderRight);
            swapJoints(output, NUIJointType.ElbowLeft, NUIJointType.ElbowRight);
            swapJoints(output, NUIJointType.WristLeft, NUIJointType.WristRight);
            swapJoints(output, NUIJointType.HandLeft, NUIJointType.HandRight);

            foreach (NUIJointType jointType in output.Joints.Keys)
            {
                output.Joints[jointType].Position = new Vector3(-output.Joints[jointType].Position.x, output.Joints[jointType].Position.y, output.Joints[jointType].Position.z);
            }

            return(output);
        }
        public override NUISkeleton MapSkeleton(NUISkeleton inputSkeleton)
        {
            NUISkeleton outputSkeleton = new NUISkeleton(OutputStructure.Structure);
            NUIJoint    parentJoint    = OutputStructure.Joints[OutputStructure.Structure.RootNode];

            outputSkeleton.Joints.Add(OutputStructure.Structure.RootNode, parentJoint);

            foreach (NUIJointType jointType in Enum.GetValues(typeof(NUIJointType)))
            {
                if (isJointMasked(jointType))
                {
                    if (OutputStructure.Joints.ContainsKey(jointType))
                    {
                        NUIJoint maskedJoint = OutputStructure.Joints[jointType];
                        outputSkeleton.Joints.Add(jointType, maskedJoint);
                        continue;
                    }
                }
                if (OutputStructure.Structure.IsJointInStructure(jointType))
                {
                    if (!OutputStructure.Structure.IsJointAnExtremity(jointType))
                    {
                        //if (jointType == NUIJointType.SpineBase)
                        {
                            // Build the skeleton for this current joint.
                            RotateJoint(jointType, inputSkeleton, outputSkeleton);
                        }
                    }
                    else
                    {
                        // If extremity, just copy from OutputStructure.
                        NUIJoint extremity = OutputStructure.Joints[jointType];
                        outputSkeleton.Joints.Add(jointType, extremity);
                    }
                }
            }
            return(outputSkeleton);
        }
Пример #15
0
        public override NUISkeleton Filter(CaptureCache cache)
        {
            List <NUISkeleton> skeletonCache = cache.GetCacheForFilter(this.Name);

            ResetHistory();
            NUISkeleton output = null;

            foreach (NUISkeleton skeleton in skeletonCache)
            {
                output = skeleton.Clone();

                Array jointTypeValues = Enum.GetValues(typeof(NUIJointType));
                JitterRadius = Math.Max(0.0001f, JitterRadius);

                float tempSmoothing          = Smoothing;
                float tempCorrection         = Correction;
                float tempPrediction         = Prediction;
                float tempJitterRadius       = JitterRadius;
                float tempMaxDeviationRadius = MaxDeviationRadius;

                foreach (NUIJointType jt in jointTypeValues)
                {
                    if (skeleton.Joints.ContainsKey(jt) && jt != NUIJointType.Unspecified)
                    {
                        if (skeleton.Joints[jt].Inferred)
                        {
                            tempJitterRadius       *= 2.0f;
                            tempMaxDeviationRadius *= 2.0f;
                        }

                        output.Joints[jt].Position = this.FilterJoint(skeleton, jt, tempSmoothing, tempCorrection,
                                                                      tempPrediction, tempJitterRadius, tempMaxDeviationRadius);
                    }
                }
            }

            return(output);
        }
        private void RotateJoint(NUIJointType jointType, NUISkeleton skeleton, NUISkeleton rig)
        {
            // Get Info from the OutputStructure
            NUIJointType parentJointType    = rig.Structure.GetParentJoint(jointType);
            NUIJointType childJointType     = rig.Structure.GetChildJoint(jointType);
            Quaternion   jointLocalRotation = OutputStructure.Joints[jointType].Rotation;
            Vector3      direction          = OutputStructure.Joints[jointType].directionToChild;
            Matrix4x4    matrix             = rig.Joints[parentJointType].WorldTransformationMatrix;

            // Get the target direction based on the captured skeleton.
            Vector3 target = new Vector3();

            if (rig.Structure.IsJointParentToMany(jointType))
            {
                if (jointType == NUIJointType.SpineBase)
                {
                    Vector3 worldDirection       = matrix.inverse.MultiplyVector(direction);
                    bool    hipDirectionInverted = (worldDirection.y > 0);

                    target = ((skeleton.Joints[NUIJointType.HipLeft].Position + skeleton.Joints[NUIJointType.HipRight].Position) / 2F) - skeleton.Joints[NUIJointType.SpineBase].Position;
                    if (hipDirectionInverted)
                    {
                        target.y *= -1;
                    }
                }
            }
            else
            {
                target = skeleton.Joints[childJointType].Position - skeleton.Joints[jointType].Position;
            }

            // Get the parent's matrix data
            NUIJoint outputJoint = OutputStructure.Joints[jointType];

            matrix *= outputJoint.TransformationMatrix;

            // Transform the target from capture space to skeleton space.
            target = matrix.inverse.MultiplyVector(target);

            // Obtain the rotation from the original joint direction to the target direction.
            Quaternion quat = Quaternion.FromToRotation(direction, target);

            //if (jointType == NUIJointType.SpineMid)
            //{
            //    direction = OutputStructure.ChestRight;

            //    target = skeleton.Joints[NUIJointType.ShoulderRight].Position - skeleton.Joints[NUIJointType.ShoulderLeft].Position;

            //    target = matrix.inverse.MultiplyVector(target);
            //    target -= Vector3.Project(target, OutputStructure.Joints[jointType].directionToChild);

            //    quat *= Quaternion.FromToRotation(direction, target);
            //}

            if (jointType == NUIJointType.SpineBase)
            {
                direction = OutputStructure.SpineBaseRight;

                target  = skeleton.Joints[NUIJointType.HipRight].Position - skeleton.Joints[NUIJointType.HipLeft].Position;
                target  = matrix.inverse.MultiplyVector(target);
                target -= Vector3.Project(target, OutputStructure.Joints[jointType].directionToChild);

                quat *= Quaternion.FromToRotation(direction, target);
            }

            // Knee correction TODO: Make this optional
            if (jointType == NUIJointType.KneeLeft || jointType == NUIJointType.KneeRight ||
                jointType == NUIJointType.HipLeft || jointType == NUIJointType.HipRight)
            {
                quat *= Quaternion.Euler(0, 0, -5f);
            }
            if (jointType == NUIJointType.AnkleLeft || jointType == NUIJointType.AnkleRight)
            {
                quat *= Quaternion.Euler(0, 0, -30f);
            }

            jointLocalRotation *= quat;

            if (jointType == NUIJointType.SpineBase)
            {
                //jointLocalRotation = Quaternion.identity;
            }

            // Update the rig
            NUIJoint newJoint = new NUIJoint(jointType);

            newJoint.Position                  = new Vector3(-outputJoint.Position.x, outputJoint.Position.y, outputJoint.Position.z);
            newJoint.Rotation                  = jointLocalRotation;
            newJoint.TransformationMatrix      = Matrix4x4.TRS(newJoint.Position, newJoint.Rotation, Vector3.one);
            newJoint.WorldTransformationMatrix = rig.Joints[parentJointType].WorldTransformationMatrix * newJoint.TransformationMatrix;

            if (!rig.Joints.ContainsKey(jointType))
            {
                rig.Joints.Add(jointType, newJoint);
            }
            else
            {
                rig.Joints[jointType] = newJoint;
            }
        }
Пример #17
0
 public abstract Vector3 GetHipPosition(NUISkeleton skeleton);
Пример #18
0
 public abstract NUISkeleton MapSkeleton(NUISkeleton skeleton);
Пример #19
0
        public virtual NUISkeleton Filter(CaptureCache cache)
        {
            NUISkeleton skeleton = cache.CurrentSkeleton;

            return(Filter(skeleton));
        }
Пример #20
0
 public virtual NUISkeleton Filter(NUISkeleton input)
 {
     return(input);
 }
Пример #21
0
        protected Vector3 FilterJoint(NUISkeleton skeleton, NUIJointType jt, float smoothingValue, float correctionValue,
                                      float predictionValue, float jitterRadiusValue, float maxDeviationRadiusValue)
        {
            int jointIndex = (int)jt;

            Vector3 filteredPosition;
            Vector3 diffvec;
            Vector3 trend;
            float   diffVal;

            Vector3 rawPosition          = skeleton.Joints[jt].Position;
            Vector3 prevFilteredPosition = this.history[jointIndex].FilteredPosition;
            Vector3 prevTrend            = this.history[jointIndex].Trend;
            Vector3 prevRawPosition      = this.history[jointIndex].RawPosition;

            bool jointIsValid = rawPosition.x != 0.0f || rawPosition.y != 0.0f || rawPosition.z != 0.0f;

            if (!jointIsValid)
            {
                this.history[jointIndex].FrameCount = 0;
            }

            // Initial start values
            if (this.history[jointIndex].FrameCount == 0)
            {
                filteredPosition = rawPosition;
                trend            = Vector3.zero;
            }
            else if (this.history[jointIndex].FrameCount == 1)
            {
                filteredPosition = (rawPosition + prevRawPosition) * 0.5f;
                diffvec          = (filteredPosition - prevFilteredPosition);
                trend            = (diffvec * correctionValue) + (prevTrend * (1.0f - correctionValue));
            }
            else
            {
                // First apply jitter filter
                diffvec = (rawPosition - prevFilteredPosition);
                diffVal = Math.Abs(diffvec.magnitude);

                if (diffVal <= jitterRadiusValue)
                {
                    filteredPosition = rawPosition * (diffVal / jitterRadiusValue) + (prevFilteredPosition * (1.0f - (diffVal / jitterRadiusValue)));
                }
                else
                {
                    filteredPosition = rawPosition;
                }

                filteredPosition = (filteredPosition * (1.0f - smoothingValue)) + ((prevFilteredPosition + prevTrend) * smoothingValue);

                diffvec = (filteredPosition - prevFilteredPosition);
                trend   = ((diffvec * correctionValue) + (prevTrend * (1.0f - correctionValue)));
            }

            Vector3 predictedPosition = (filteredPosition + (trend * predictionValue));

            diffvec = (predictedPosition - rawPosition);
            diffVal = Math.Abs(diffvec.magnitude);

            if (diffVal > maxDeviationRadiusValue)
            {
                predictedPosition = ((predictedPosition * (maxDeviationRadiusValue / diffVal)) + (rawPosition * (1.0f - (maxDeviationRadiusValue / diffVal))));
            }

            this.history[jointIndex].RawPosition      = rawPosition;
            this.history[jointIndex].FilteredPosition = filteredPosition;
            this.history[jointIndex].Trend            = trend;
            this.history[jointIndex].FrameCount++;

            return(predictedPosition);
        }
Пример #22
0
 internal void AddFiltered(string name, NUISkeleton filtered)
 {
     CurrentFrameData.Add(name, filtered);
 }
Пример #23
0
 internal void AddMapped(NUISkeleton mapped)
 {
     CurrentFrameData.Add("Mapped", mapped);
 }
Пример #24
0
 internal void AddResult(NUISkeleton filtered)
 {
     CurrentFrameData.Add("Result", filtered);
 }
Пример #25
0
        /// <summary>
        /// Once capture is complete, request the OutputProfile to save the animation.
        /// </summary>
        protected void saveAnimation(MocapSession session)
        {
            if (OutputProfile == null)
            {
                Debug.LogWarning("No Output method was found. Animation was not saved");
                return;
            }
            if (MappingProfile == null)
            {
                Debug.LogWarning("No Mapping method was found. Animation was not saved");
                return;
            }
            if (session == null)
            {
                return;
            }

            var animation = new NUIHumanoidAnimation();
            var cache     = new CaptureCache();

            foreach (MocapSessionKeyframe keyframe in session.CaptureData)
            {
                var elapsedTime = keyframe.ElapsedMilliseconds;
                var skeleton    = NUICaptureHelper.GetNUISkeleton(keyframe.Skeleton);

                cache.AddNewFrame(skeleton, elapsedTime);

                // Filter the raw input with enabled filters.
                NUISkeleton filtered = skeleton;
                foreach (MocapFilter filter in preMapFilters)
                {
                    if (filter.Enabled)
                    {
                        filtered = filter.Filter(cache);
                        cache.AddFiltered(filter.Name, filtered);
                    }
                }

                // Convert the input skeleton to the normalized skeleton (Unity)
                NUISkeleton mapped = MappingProfile.MapSkeleton(filtered);
                cache.AddMapped(mapped);

                // Apply any post-mapped filters selected by the user.
                filtered = mapped;
                foreach (MocapFilter filter in postMapFilters)
                {
                    if (filter.Enabled)
                    {
                        filtered = filter.Filter(cache);
                        cache.AddFiltered(filter.Name, filtered);
                    }
                }
                cache.AddResult(filtered);

                filtered.Joints[NUIJointType.SpineBase].Position = skeleton.Joints[NUIJointType.SpineBase].Position;
                animation.AddKeyframe(filtered, keyframe.ElapsedMilliseconds);
            }

            // Save the session
            if (saveSession)
            {
                string newFileName = fileName;
                if (BaseSystem.IO.File.Exists(string.Format(SAVE_DESTINATION_FORMAT, sessionSaveDestination, newFileName)))
                {
                    newFileName = CinemaMocapHelper.GetNewFilename(sessionSaveDestination, fileName, "asset");
                    UnityEngine.Debug.LogWarning(string.Format(NAME_DUPLICATE_ERROR_MSG, fileName, newFileName));
                }

                AssetDatabase.CreateAsset(session, string.Format(SAVE_DESTINATION_FORMAT, sessionSaveDestination, newFileName));
                AssetDatabase.SaveAssets();
            }

            // Save the animation
            OutputProfile.SaveAnimation(animation);
            AssetDatabase.Refresh();
        }
Пример #26
0
        /// <summary>
        /// Get the structure for what is expected in this output method.
        /// </summary>
        /// <returns></returns>
        public override NUISkeleton GetTargetStructure()
        {
            // Define the skeletal structure
            SkeletonStructure structure = new SkeletonStructure();

            structure.SetRootJoint(NUIJointType.SpineBase);
            structure.AddBone(ColladaToNUIJointMapping("SpineMid"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("SpineShoulder"), ColladaToNUIJointMapping("SpineMid"));
            structure.AddBone(ColladaToNUIJointMapping("Neck"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("Head"), ColladaToNUIJointMapping("Neck"));

            structure.AddBone(ColladaToNUIJointMapping("ShoulderLeft"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("ElbowLeft"), ColladaToNUIJointMapping("ShoulderLeft"));
            structure.AddBone(ColladaToNUIJointMapping("WristLeft"), ColladaToNUIJointMapping("ElbowLeft"));
            structure.AddBone(ColladaToNUIJointMapping("HandLeft"), ColladaToNUIJointMapping("WristLeft"));
            structure.AddBone(ColladaToNUIJointMapping("HandTipLeft"), ColladaToNUIJointMapping("HandLeft"));
            structure.AddBone(ColladaToNUIJointMapping("ThumbLeft"), ColladaToNUIJointMapping("HandLeft"));

            structure.AddBone(ColladaToNUIJointMapping("ShoulderRight"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("ElbowRight"), ColladaToNUIJointMapping("ShoulderRight"));
            structure.AddBone(ColladaToNUIJointMapping("WristRight"), ColladaToNUIJointMapping("ElbowRight"));
            structure.AddBone(ColladaToNUIJointMapping("HandRight"), ColladaToNUIJointMapping("WristRight"));
            structure.AddBone(ColladaToNUIJointMapping("HandTipRight"), ColladaToNUIJointMapping("HandRight"));
            structure.AddBone(ColladaToNUIJointMapping("ThumbRight"), ColladaToNUIJointMapping("HandRight"));

            structure.AddBone(ColladaToNUIJointMapping("HipLeft"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("KneeLeft"), ColladaToNUIJointMapping("HipLeft"));
            structure.AddBone(ColladaToNUIJointMapping("AnkleLeft"), ColladaToNUIJointMapping("KneeLeft"));
            structure.AddBone(ColladaToNUIJointMapping("FootLeft"), ColladaToNUIJointMapping("AnkleLeft"));

            structure.AddBone(ColladaToNUIJointMapping("HipRight"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("KneeRight"), ColladaToNUIJointMapping("HipRight"));
            structure.AddBone(ColladaToNUIJointMapping("AnkleRight"), ColladaToNUIJointMapping("KneeRight"));
            structure.AddBone(ColladaToNUIJointMapping("FootRight"), ColladaToNUIJointMapping("AnkleRight"));

            // Define the skeleton in Unity terms.
            NUISkeleton skeleton = new NUISkeleton(structure);

            // Get the hip right and the chest right.
            Vector3 hipRightTranslation      = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 hipLeftTranslation       = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 shoulderRightTranslation = rigData.GetJoint("ShoulderRight").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 shoulderLeftTranslation  = rigData.GetJoint("ShoulderLeft").LHSWorldTransformationMatrix.GetColumn(3);

            Vector3 hipRight   = hipRightTranslation - hipLeftTranslation;
            Vector3 chestRight = shoulderRightTranslation - shoulderLeftTranslation;

            skeleton.SpineBaseRight = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.inverse.MultiplyVector(hipRight);
            skeleton.ChestRight     = rigData.GetJoint("SpineMid").LHSWorldTransformationMatrix.inverse.MultiplyVector(chestRight);

            foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData)
            {
                NUIJointType jointType = ColladaToNUIJointMapping(jointData.Key);
                NUIJoint     joint     = new NUIJoint(jointType);

                // Convert the Collada Joint rotation from RHS to LHS (Unity)
                ColladaJointData currentJoint = jointData.Value;
                joint.Position                  = currentJoint.Translation;
                joint.Rotation                  = QuaternionHelper.RHStoLHS(currentJoint.RotationVector);
                joint.TransformationMatrix      = currentJoint.LHSTransformationMatrix;
                joint.WorldTransformationMatrix = currentJoint.LHSWorldTransformationMatrix;

                Vector3 directionToChild = Vector3.zero;

                if (!structure.IsJointAnExtremity(jointType)) // directionToChild is not needed for extremeties.
                {
                    NUIJointType childType = structure.GetChildJoint(jointType);
                    if (childType != NUIJointType.Unspecified)
                    {
                        Vector3 child  = rigData.GetJoint(NUIJointToColladaMapping(childType)).LHSWorldTransformationMatrix.GetColumn(3);
                        Vector3 parent = currentJoint.LHSWorldTransformationMatrix.GetColumn(3);
                        directionToChild = child - parent;
                    }
                }

                if (jointType == NUIJointType.SpineBase) // The Hip is a special case.
                {
                    Vector3 rightHipWorldPosition = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3);
                    Vector3 leftHipWorldPosition  = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3);
                    Vector3 hipWorldPosition      = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.GetColumn(3);

                    directionToChild = ((rightHipWorldPosition + leftHipWorldPosition) / 2F) - hipWorldPosition;

                    //float angle = Vector3.Angle(Vector3.up, directionToChild);
                    //directionToChild = Quaternion.AngleAxis(40 - angle, hipRight) * directionToChild;
                }

                joint.directionToChild = currentJoint.LHSWorldTransformationMatrix.inverse.MultiplyVector(directionToChild);

                skeleton.Joints.Add(jointType, joint);
            }
            skeleton.ChestRight -= Vector3.Project(skeleton.ChestRight, skeleton.Joints[NUIJointType.SpineMid].directionToChild);

            return(skeleton);
        }
Пример #27
0
        public override void Reset()
        {
            NUISkeleton skeleton = GetTargetStructure();

            UpdatePreview(skeleton, Vector3.zero);
        }
Пример #28
0
        public ColladaAnimationData GetColladaAnimation(NUIHumanoidAnimation animation)
        {
            NUISkeleton outputStructure = GetTargetStructure();

            ColladaAnimationData colladaAnimation = new ColladaAnimationData(rigData);
            List <float>         elapsedTimes     = new List <float>();

            Vector3 startingPosition    = Vector3.zero;
            Vector3 rigStartingPosition = rigData.GetJoint("SpineBase").Translation;


            int frameRate = 0;

            switch (selectedFrameRateIndex)
            {
            case 0:
                frameRate = 30;
                break;

            case 1:
                frameRate = 60;
                break;
            }

            NUIHumanoidAnimation animationConstrained = animation.ConstrainFramerate(frameRate);

            for (int k = 0; k < animationConstrained.Keyframes.Count; k++)
            {
                NUIAnimationKeyframe keyframe = animationConstrained.Keyframes[k];

                elapsedTimes.Add(keyframe.ElapsedTime);

                ColladaRigData   currentRig  = new ColladaRigData();
                ColladaJointData parentJoint = rigData.GetJoint("SpineBase");
                currentRig.Add(parentJoint.Id, parentJoint);

                foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData)
                {
                    ColladaJointData colladaJointData = rigData.GetJoint(jointData.Key);

                    if (colladaJointData.Id == "SpineBase")
                    {
                        Vector3 hipPosition = keyframe.Skeleton.Joints[NUIJointType.SpineBase].Position * 100;
                        hipPosition.x *= -1;
                        if (startingPosition == Vector3.zero)
                        {
                            startingPosition = hipPosition;
                        }
                        colladaJointData.Translation = (hipPosition - startingPosition) + rigStartingPosition;
                    }
                    colladaAnimation.jointTranslateX[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.x);
                    colladaAnimation.jointTranslateY[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.y);
                    colladaAnimation.jointTranslateZ[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.z);
                }

                foreach (KeyValuePair <NUIJointType, NUIJoint> kvp in keyframe.Skeleton.Joints)
                {
                    // For parent joints
                    Quaternion rotation = kvp.Value.Rotation;

                    string           id = NUIJointToColladaMapping(kvp.Key);
                    ColladaJointData colladaJointData = rigData.GetJoint(id);

                    if (!outputStructure.Structure.IsJointAnExtremity(kvp.Key))
                    {
                        Vector3 revert    = QuaternionHelper.ToEulerAnglesXYZ(rotation);
                        Vector3 corrected = new Vector3(revert.x, -revert.y, -revert.z);

                        colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", corrected.x);
                        colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", corrected.y);
                        colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", corrected.z);

                        Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, QuaternionHelper.FromEulerAnglesXYZ(corrected), Vector3.one);
                        colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString());
                    }
                    else
                    {
                        // Extremeties
                        colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.x);
                        colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.y);
                        colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.z);

                        Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, colladaJointData.Rotation, Vector3.one);
                        colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString());
                    }
                }
            }

            colladaAnimation.frameTimelapse = elapsedTimes;
            return(colladaAnimation);
        }
Пример #29
0
 /// <summary>
 /// Update the Preview Model.
 /// </summary>
 /// <param name="skeleton">The skeleton to update the preview with.</param>
 public abstract void UpdatePreview(NUISkeleton skeleton, Vector3 position);