Exemple #1
0
        private BabylonMatrix ConvertMayaToBabylonMatrix(MMatrix mMatrix)
        {
            var transformationMatrix = new MTransformationMatrix(mMatrix);

            // Retreive TRS vectors from matrix
            float[] position           = transformationMatrix.getTranslation();
            float[] rotationQuaternion = transformationMatrix.getRotationQuaternion();
            float[] scaling            = transformationMatrix.getScale();

            // Switch coordinate system at object level
            position[2]           *= -1;
            rotationQuaternion[0] *= -1;
            rotationQuaternion[1] *= -1;

            // Apply unit conversion factor to meter
            position[0] *= scaleFactorToMeters;
            position[1] *= scaleFactorToMeters;
            position[2] *= scaleFactorToMeters;

            // The composed matrix
            return(BabylonMatrix.Compose(new BabylonVector3(scaling[0], scaling[1], scaling[2]),                                                            // scaling
                                         new BabylonQuaternion(rotationQuaternion[0], rotationQuaternion[1], rotationQuaternion[2], rotationQuaternion[3]), // rotation
                                         new BabylonVector3(position[0], position[1], position[2])                                                          // position
                                         ));
        }
        private void GetTransform(MFnTransform mFnTransform, ref float[] position, ref float[] rotationQuaternion, ref float[] rotation, ref BabylonVector3.EulerRotationOrder rotationOrder, ref float[] scaling)
        {
            var transformationMatrix = new MTransformationMatrix(mFnTransform.transformationMatrix);
            var mayaRotationOrder    = 0;

            MGlobal.executeCommand($"getAttr {mFnTransform.fullPathName}.rotateOrder", out mayaRotationOrder);
            rotationOrder = Tools.ConvertMayaRotationOrder((MEulerRotation.RotationOrder)mayaRotationOrder);

            position           = transformationMatrix.getTranslation();
            rotationQuaternion = transformationMatrix.getRotationQuaternion();
            rotation           = transformationMatrix.getRotation();
            scaling            = transformationMatrix.getScale();

            // Switch coordinate system at object level
            position[2]           *= -1;
            rotationQuaternion[0] *= -1;
            rotationQuaternion[1] *= -1;
            rotation[0]           *= -1;
            rotation[1]           *= -1;
            rotationOrder          = Tools.InvertRotationOrder(rotationOrder);

            // Apply unit conversion factor to meter
            position[0] *= scaleFactorToMeters;
            position[1] *= scaleFactorToMeters;
            position[2] *= scaleFactorToMeters;
        }
Exemple #3
0
        /// <summary>
        /// Default space is transform
        /// </summary>
        /// <param name="mTransformationMatrix"></param>
        /// <returns></returns>
        public static float[] getRotationQuaternion(this MTransformationMatrix mTransformationMatrix)
        {
            double x = 0, y = 0, z = 0, w = 0;

            mTransformationMatrix.getRotationQuaternion(ref x, ref y, ref z, ref w);
            return(new float[] { (float)x, (float)y, (float)z, (float)w });
        }
Exemple #4
0
        /// <summary>
        /// Default space is transform
        /// Default rotation order is YXZ
        /// </summary>
        /// <param name="mTransformationMatrix"></param>
        /// <returns></returns>
        public static float[] getRotation(this MTransformationMatrix mTransformationMatrix)
        {
            double x = 0, y = 0, z = 0, w = 0;

            mTransformationMatrix.getRotationQuaternion(ref x, ref y, ref z, ref w);
            // Maya conversion algorithm is bugged when reaching limits (angle like (-90,89,90))
            // Convert quaternion to vector3 using Babylon conversion algorithm
            BabylonQuaternion babylonQuaternion = new BabylonQuaternion((float)x, (float)y, (float)z, (float)w);

            return(babylonQuaternion.toEulerAngles().ToArray());
        }
        private void GetTransform(MFnTransform mFnTransform, ref float[] position, ref float[] rotationQuaternion, ref float[] rotation, ref float[] scaling)
        {
            var transformationMatrix = new MTransformationMatrix(mFnTransform.transformationMatrix);

            position           = transformationMatrix.getTranslation();
            rotationQuaternion = transformationMatrix.getRotationQuaternion();
            rotation           = transformationMatrix.getRotation();
            scaling            = transformationMatrix.getScale();

            // Switch coordinate system at object level
            position[2]           *= -1;
            rotationQuaternion[0] *= -1;
            rotationQuaternion[1] *= -1;
            rotation[0]           *= -1;
            rotation[1]           *= -1;
        }
Exemple #6
0
        private void ExportTransform(BabylonAbstractMesh babylonAbstractMesh, MFnDagNode mFnDagNode)
        {
            // Position / rotation / scaling
            var transformationMatrix = new MTransformationMatrix(mFnDagNode.transformationMatrix);

            babylonAbstractMesh.position = transformationMatrix.getTranslation();

            if (_exportQuaternionsInsteadOfEulers)
            {
                babylonAbstractMesh.rotationQuaternion = transformationMatrix.getRotationQuaternion();
            }
            else
            {
                babylonAbstractMesh.rotation = transformationMatrix.getRotation();
            }

            babylonAbstractMesh.scaling = transformationMatrix.getScale();
        }
Exemple #7
0
        private List <BabylonAnimation> GetAnimationsFrameByFrame(MFnTransform mFnTransform)
        {
            int start = Loader.GetMinTime();
            int end   = Loader.GetMaxTime();

            // Animations
            List <BabylonAnimation> animations = new List <BabylonAnimation>();

            string[] babylonAnimationProperties = new string[] { "scaling", "rotationQuaternion", "position", "visibility" };


            Dictionary <string, List <BabylonAnimationKey> > keysPerProperty = new Dictionary <string, List <BabylonAnimationKey> >();

            keysPerProperty.Add("scaling", new List <BabylonAnimationKey>());
            keysPerProperty.Add("rotationQuaternion", new List <BabylonAnimationKey>());
            keysPerProperty.Add("position", new List <BabylonAnimationKey>());
            keysPerProperty.Add("visibility", new List <BabylonAnimationKey>());

            // get keys
            for (int currentFrame = start; currentFrame <= end; currentFrame++)
            {
                // get transformation matrix at this frame
                MDoubleArray mDoubleMatrix = new MDoubleArray();
                MGlobal.executeCommand($"getAttr -t {currentFrame} {mFnTransform.fullPathName}.matrix", mDoubleMatrix);
                mDoubleMatrix.get(out float[] localMatrix);
                MMatrix matrix = new MMatrix(localMatrix);
                var     transformationMatrix = new MTransformationMatrix(matrix);

                // Retreive TRS vectors from matrix
                var position           = transformationMatrix.getTranslation();
                var rotationQuaternion = transformationMatrix.getRotationQuaternion();
                var scaling            = transformationMatrix.getScale();

                // Switch coordinate system at object level
                position[2]           *= -1;
                rotationQuaternion[0] *= -1;
                rotationQuaternion[1] *= -1;

                // create animation key for each property
                for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++)
                {
                    string babylonAnimationProperty = babylonAnimationProperties[indexAnimation];

                    BabylonAnimationKey key = new BabylonAnimationKey();
                    key.frame = currentFrame;
                    switch (indexAnimation)
                    {
                    case 0:     // scaling
                        key.values = scaling.ToArray();
                        break;

                    case 1:     // rotationQuaternion
                        key.values = rotationQuaternion.ToArray();
                        break;

                    case 2:     // position
                        key.values = position.ToArray();
                        break;

                    case 3:     // visibility
                        key.values = new float[] { Loader.GetVisibility(mFnTransform.fullPathName, currentFrame) };
                        break;
                    }

                    keysPerProperty[babylonAnimationProperty].Add(key);
                }
            }

            // create animation for each property
            for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++)
            {
                string babylonAnimationProperty = babylonAnimationProperties[indexAnimation];

                List <BabylonAnimationKey> keys = keysPerProperty[babylonAnimationProperty];

                var keysFull = new List <BabylonAnimationKey>(keys);

                // Optimization
                OptimizeAnimations(keys, true);

                // Ensure animation has at least 2 frames
                if (IsAnimationKeysRelevant(keys))
                {
                    int dataType = 0;                               // "scaling", "rotationQuaternion", "position", "visibility"
                    if (indexAnimation == 0 || indexAnimation == 2) // scaling and position
                    {
                        dataType = (int)BabylonAnimation.DataType.Vector3;
                    }
                    else if (indexAnimation == 1)    // rotationQuaternion
                    {
                        dataType = (int)BabylonAnimation.DataType.Quaternion;
                    }
                    else   // visibility
                    {
                        dataType = (int)BabylonAnimation.DataType.Float;
                    }
                    // Create BabylonAnimation
                    animations.Add(new BabylonAnimation()
                    {
                        dataType       = dataType,
                        name           = babylonAnimationProperty + " animation",
                        framePerSecond = Loader.GetFPS(),
                        loopBehavior   = (int)BabylonAnimation.LoopBehavior.Cycle,
                        property       = babylonAnimationProperty,
                        keys           = keys.ToArray(),
                        keysFull       = keysFull
                    });
                }
            }
            return(animations);
        }
Exemple #8
0
        private List <BabylonAnimation> GetAnimationsFrameByFrame(MFnTransform mFnTransform)
        {
            int start = GetMinTime()[0];
            int end   = GetMaxTime()[0];

            // Animations
            List <BabylonAnimation> animations = new List <BabylonAnimation>();

            string[] babylonAnimationProperties = new string[] { "scaling", "rotationQuaternion", "position" };


            Dictionary <string, List <BabylonAnimationKey> > keysPerProperty = new Dictionary <string, List <BabylonAnimationKey> >();

            keysPerProperty.Add("scaling", new List <BabylonAnimationKey>());
            keysPerProperty.Add("rotationQuaternion", new List <BabylonAnimationKey>());
            keysPerProperty.Add("position", new List <BabylonAnimationKey>());

            // get keys
            for (int currentFrame = start; currentFrame <= end; currentFrame++)
            {
                // get transformation matrix at this frame
                MDoubleArray mDoubleMatrix = new MDoubleArray();
                MGlobal.executeCommand($"getAttr -t {currentFrame} {mFnTransform.fullPathName}.matrix", mDoubleMatrix);
                mDoubleMatrix.get(out float[] localMatrix);
                MMatrix matrix = new MMatrix(localMatrix);
                var     transformationMatrix = new MTransformationMatrix(matrix);

                // Retreive TRS vectors from matrix
                var position           = transformationMatrix.getTranslation();
                var rotationQuaternion = transformationMatrix.getRotationQuaternion();
                var scaling            = transformationMatrix.getScale();

                // Switch coordinate system at object level
                position[2]           *= -1;
                rotationQuaternion[0] *= -1;
                rotationQuaternion[1] *= -1;

                // create animation key for each property
                for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++)
                {
                    string babylonAnimationProperty = babylonAnimationProperties[indexAnimation];

                    BabylonAnimationKey key = new BabylonAnimationKey();
                    key.frame = currentFrame;
                    switch (indexAnimation)
                    {
                    case 0:     // scaling
                        key.values = scaling.ToArray();
                        break;

                    case 1:     // rotationQuaternion
                        key.values = rotationQuaternion.ToArray();
                        break;

                    case 2:     // position
                        key.values = position.ToArray();
                        break;
                    }

                    keysPerProperty[babylonAnimationProperty].Add(key);
                }
            }

            // create animation for each property
            for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++)
            {
                string babylonAnimationProperty = babylonAnimationProperties[indexAnimation];

                List <BabylonAnimationKey> keys = keysPerProperty[babylonAnimationProperty];

                // Optimization
                OptimizeAnimations(keys, true);

                // Ensure animation has at least 2 frames
                if (keys.Count > 1)
                {
                    var animationPresent = true;

                    // Ensure animation has at least 2 non equal frames
                    if (keys.Count == 2)
                    {
                        if (keys[0].values.IsEqualTo(keys[1].values))
                        {
                            animationPresent = false;
                        }
                    }

                    if (animationPresent)
                    {
                        // Create BabylonAnimation
                        animations.Add(new BabylonAnimation()
                        {
                            dataType       = indexAnimation == 1 ? (int)BabylonAnimation.DataType.Quaternion : (int)BabylonAnimation.DataType.Vector3,
                            name           = babylonAnimationProperty + " animation",
                            framePerSecond = GetFPS(),
                            loopBehavior   = (int)BabylonAnimation.LoopBehavior.Cycle,
                            property       = babylonAnimationProperty,
                            keys           = keys.ToArray()
                        });
                    }
                }
            }

            return(animations);
        }