Example #1
0
        public void NodesInHierarchy_CalcGlobalTransform_ChildsIsTimesParent()
        {
            // given:
            var node1 = new FbxNode("node1");
            var node2 = new FbxNode("node2");

            node1.AddChild(node2);
            var r1 = new FbxVector4(45, 0, 0);
            var r2 = new FbxVector4(0, 60, 0);

            node1.LclRotation.Set(r1);
            node2.LclRotation.Set(r2);

            // when:
            var g1 = node1.EvaluateGlobalTransform();
            var g2 = node2.EvaluateGlobalTransform();
            var l1 = node1.EvaluateLocalTransform();
            var l2 = node2.EvaluateLocalTransform();

            Assert.AreEqual(l1.Get(0, 0), g1.Get(0, 0), 0.000001f);
            Assert.AreEqual(l1.Get(0, 1), g1.Get(0, 1), 0.000001f);
            Assert.AreEqual(l1.Get(0, 2), g1.Get(0, 2), 0.000001f);
            Assert.AreEqual(l1.Get(0, 3), g1.Get(0, 3), 0.000001f);
            Assert.AreEqual(l1.Get(1, 0), g1.Get(1, 0), 0.000001f);
            Assert.AreEqual(l1.Get(1, 1), g1.Get(1, 1), 0.000001f);
            Assert.AreEqual(l1.Get(1, 2), g1.Get(1, 2), 0.000001f);
            Assert.AreEqual(l1.Get(1, 3), g1.Get(1, 3), 0.000001f);
            Assert.AreEqual(l1.Get(2, 0), g1.Get(2, 0), 0.000001f);
            Assert.AreEqual(l1.Get(2, 1), g1.Get(2, 1), 0.000001f);
            Assert.AreEqual(l1.Get(2, 2), g1.Get(2, 2), 0.000001f);
            Assert.AreEqual(l1.Get(2, 3), g1.Get(2, 3), 0.000001f);
            Assert.AreEqual(l1.Get(3, 0), g1.Get(3, 0), 0.000001f);
            Assert.AreEqual(l1.Get(3, 1), g1.Get(3, 1), 0.000001f);
            Assert.AreEqual(l1.Get(3, 2), g1.Get(3, 2), 0.000001f);
            Assert.AreEqual(l1.Get(3, 3), g1.Get(3, 3), 0.000001f);

            var m = l1 * l2;

            Assert.AreEqual(m.Get(0, 0), g2.Get(0, 0), 0.000001f);
            Assert.AreEqual(m.Get(0, 1), g2.Get(0, 1), 0.000001f);
            Assert.AreEqual(m.Get(0, 2), g2.Get(0, 2), 0.000001f);
            Assert.AreEqual(m.Get(0, 3), g2.Get(0, 3), 0.000001f);
            Assert.AreEqual(m.Get(1, 0), g2.Get(1, 0), 0.000001f);
            Assert.AreEqual(m.Get(1, 1), g2.Get(1, 1), 0.000001f);
            Assert.AreEqual(m.Get(1, 2), g2.Get(1, 2), 0.000001f);
            Assert.AreEqual(m.Get(1, 3), g2.Get(1, 3), 0.000001f);
            Assert.AreEqual(m.Get(2, 0), g2.Get(2, 0), 0.000001f);
            Assert.AreEqual(m.Get(2, 1), g2.Get(2, 1), 0.000001f);
            Assert.AreEqual(m.Get(2, 2), g2.Get(2, 2), 0.000001f);
            Assert.AreEqual(m.Get(2, 3), g2.Get(2, 3), 0.000001f);
            Assert.AreEqual(m.Get(3, 0), g2.Get(3, 0), 0.000001f);
            Assert.AreEqual(m.Get(3, 1), g2.Get(3, 1), 0.000001f);
            Assert.AreEqual(m.Get(3, 2), g2.Get(3, 2), 0.000001f);
            Assert.AreEqual(m.Get(3, 3), g2.Get(3, 3), 0.000001f);
        }
Example #2
0
        AnimationLayer LoadLayer(FbxNode node, FbxAnimLayer animLayer, double[] keyTimes)
        {
            //ToDo : animLayer.BlendMode
            var ret = new AnimationLayer();

            ret.Name      = animLayer.GetName();
            ret.Transform = new KeyFrame[keyTimes.Length];
            for (int i = 0; i < keyTimes.Length; i++)
            {
                var t = new FbxTime();
                t.SetSecondDouble(keyTimes[i]);
                var transform = node.EvaluateLocalTransform(t);
                ret.Transform[i] = new KeyFrame {
                    TimeInSeconds = keyTimes[i], Value = transform.ToMatrix4()
                };
            }
            return(ret);
        }
            /// <summary>
            /// Process fbx scene by doing nothing
            /// </summary>
            public void ProcessNode(FbxNode fbxNode, GameObject unityParentObj = null, bool constructTransform = false)
            {
                string name = fbxNode.GetName();

                GameObject unityGo = new GameObject(name);

                NumNodes++;

                if (unityParentObj != null)
                {
                    unityGo.transform.parent = unityParentObj.transform;
                }

                FbxAMatrix fbxTransform = null;

                if (constructTransform)
                {
#if UNI_18844
                    // Construct rotation matrices
                    FbxVector4 fbxRotation  = new FbxVector4(fbxNode.LclRotation.Get());
                    FbxAMatrix fbxRotationM = new FbxAMatrix();
                    fbxRotationM.SetR(fbxRotation);

                    FbxVector4 fbxPreRotation  = new FbxVector4(fbxNode.PreRotation.Get());
                    FbxAMatrix fbxPreRotationM = new FbxAMatrix();
                    fbxPreRotationM.SetR(fbxPreRotation);

                    FbxVector4 fbxPostRotation  = new FbxVector4(fbxNode.PostRotation.Get());
                    FbxAMatrix fbxPostRotationM = new FbxAMatrix();
                    fbxPostRotationM.SetR(fbxPostRotation);

                    // Construct translation matrix
                    FbxAMatrix fbxTranslationM = new FbxAMatrix();
                    FbxVector4 fbxTranslation  = new FbxVector4(fbxNode.LclTranslation.Get());
                    fbxTranslationM.SetT(fbxTranslation);

                    // Construct scaling matrix
                    FbxAMatrix fbxScalingM = new FbxAMatrix();
                    FbxVector4 fbxScaling  = new FbxVector4(fbxNode.LclScaling.Get());
                    fbxScalingM.SetS(fbxScaling);

                    // Construct offset and pivot matrices
                    FbxAMatrix fbxRotationOffsetM = new FbxAMatrix();
                    FbxVector4 fbxRotationOffset  = fbxNode.RotationOffset.Get();
                    fbxRotationOffsetM.SetT(fbxRotationOffset);

                    FbxAMatrix fbxRotationPivotM = new FbxAMatrix();
                    FbxVector4 fbxRotationPivot  = fbxNode.RotationPivot.Get();
                    fbxRotationPivotM.SetT(fbxRotationPivot);

                    FbxAMatrix fbxScalingOffsetM = new FbxAMatrix();
                    FbxVector4 fbxScalingOffset  = fbxNode.ScalingOffset.Get();
                    fbxScalingOffsetM.SetT(fbxScalingOffset);

                    FbxAMatrix fbxScalingPivotM = new FbxAMatrix();
                    FbxVector4 fbxScalingPivot  = fbxNode.ScalingPivot.Get();
                    fbxScalingPivotM.SetT(fbxScalingPivot);

                    fbxTransform =
                        fbxTranslationM *
                        fbxRotationOffsetM *
                        fbxRotationPivotM *
                        fbxPreRotationM *
                        fbxRotationM *
                        fbxPostRotationM *
                        fbxRotationPivotM.Inverse() *
                        fbxScalingOffsetM *
                        fbxScalingPivotM *
                        fbxScalingM *
                        fbxScalingPivotM.Inverse();

                    lclTrs = fbxTransform.GetT();
                    lclRot = fbxTransform.GetQ();
                    lclScl = fbxTransform.GetS();
#endif
                }
                else
                {
                    fbxTransform = fbxNode.EvaluateLocalTransform();
                }

                if (fbxTransform == null)
                {
                    Debug.LogError(string.Format("failed to retrieve transform for node : {0}", fbxNode.GetName()));
                    return;
                }

                FbxVector4    lclTrs = fbxTransform.GetT();
                FbxQuaternion lclRot = fbxTransform.GetQ();
                FbxVector4    lclScl = fbxTransform.GetS();

                Debug.Log(string.Format("processing {3} Lcl : T({0}) R({1}) S({2})",
                                        lclTrs.ToString(),
                                        lclRot.ToString(),
                                        lclScl.ToString(),
                                        fbxNode.GetName()));

                // check we can handle translation value
                Debug.Assert(lclTrs.X <= float.MaxValue && lclTrs.X >= float.MinValue);
                Debug.Assert(lclTrs.Y <= float.MaxValue && lclTrs.Y >= float.MinValue);
                Debug.Assert(lclTrs.Z <= float.MaxValue && lclTrs.Z >= float.MinValue);

                unityGo.transform.localPosition = new Vector3((float)lclTrs.X, (float)lclTrs.Y, (float)lclTrs.Z);
                unityGo.transform.localRotation = new Quaternion((float)lclRot[0], (float)lclRot[1], (float)lclRot[2], (float)lclRot[3]);
                unityGo.transform.localScale    = new Vector3((float)lclScl.X, (float)lclScl.Y, (float)lclScl.Z);

                for (int i = 0; i < fbxNode.GetChildCount(); ++i)
                {
                    ProcessNode(fbxNode.GetChild(i), unityGo);
                }
            }