Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        private void ProcessLayer(FbxNode node, string layer, int[] indices, List <IOVertex> verts)
        {
            var normalLayers = node.GetNodesByName($"LayerElement{layer}");

            foreach (var lay in normalLayers)
            {
                // layer index
                var index = (int)lay.Properties[0];


                // get data
                var      dataName = GetLayerNodeName(layer);
                double[] data;
                if (lay[$"{dataName}"].Value is double[])
                {
                    data = (double[])lay[$"{dataName}"].Value;
                }
                else
                {
                    data = lay[$"{dataName}"].Properties.Select(e => (double)e).ToArray();
                }


                // indexed
                Dictionary <int, int> indexToDirect = new Dictionary <int, int>();
                if (lay["ReferenceInformationType"].Properties[0].ToString() == "IndexToDirect")
                {
                    int[] ind;
                    if (lay[$"{layer}Index"].Value is int[])
                    {
                        ind = (int[])lay[$"{layer}Index"].Value;
                    }
                    else
                    {
                        ind = lay[$"{layer}Index"].Properties.Select(e => (int)e).ToArray();
                    }

                    for (int i = 0; i < ind.Length; i++)
                    {
                        indexToDirect.Add(i, ind[i]);
                    }
                }


                // process map
                for (int i = 0; i < indices.Length; i++)
                {
                    var idx = i;

                    switch (lay["MappingInformationType"].Properties[0].ToString())
                    {
                    case "ByVertice":
                        idx = indices[i];
                        if (idx < 0)
                        {
                            idx = Math.Abs(idx) - 1;
                        }
                        break;

                    case "ByPolygonVertex":
                        break;
                    }

                    if (indexToDirect.ContainsKey(idx))
                    {
                        idx = indexToDirect[idx];
                    }

                    switch (layer)
                    {
                    case "Normal":
                        verts[i].Normal = new Vector3((float)data[idx * 3], (float)data[idx * 3 + 1], (float)data[idx * 3 + 2]);
                        break;

                    case "Tangent":
                        verts[i].Tangent = new Vector3((float)data[idx * 3], (float)data[idx * 3 + 1], (float)data[idx * 3 + 2]);
                        break;

                    case "Binormal":
                        verts[i].Binormal = new Vector3((float)data[idx * 3], (float)data[idx * 3 + 1], (float)data[idx * 3 + 2]);
                        break;

                    case "UV":
                        verts[i].SetUV((float)data[idx * 2], (float)data[idx * 2 + 1], index);
                        break;

                    case "Color":
                        verts[i].SetColor((float)data[idx * 4], (float)data[idx * 4 + 1], (float)data[idx * 4 + 2], (float)data[idx * 4 + 3], index);
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        private IOBone CreateBoneFromNode(FbxNode l)
        {
            IOBone bone = new IOBone()
            {
                Name = GetNameWithoutNamespace(l.Properties[NodeDescSize - 2].ToString())
            };

            // scan and extract info from properties
            var properties = l.GetNodesByName("P");

            if (properties.Length == 0)
            {
                properties = l.GetNodesByName("Property");
            }

            Quaternion preRot  = new Quaternion(0, 0, 0, 1);
            Quaternion postRot = new Quaternion(0, 0, 0, 1);

            foreach (var prop in properties)
            {
                if (prop.Properties.Count < PropertyDescSize)
                {
                    continue;
                }

                if (prop.Properties[0].ToString() == "PreRotation")
                {
                    preRot = MathExt.FromEulerAngles(
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 1]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 2]));
                }

                if (prop.Properties[0].ToString() == "PostRotation")
                {
                    postRot = MathExt.FromEulerAngles(
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 1]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 2]));
                }

                if (prop.Properties[0].ToString() == "Lcl Translation")
                {
                    bone.Translation = new Vector3((float)(double)prop.Properties[PropertyDescSize], (float)(double)prop.Properties[PropertyDescSize + 1], (float)(double)prop.Properties[PropertyDescSize + 2]);
                }

                if (prop.Properties[0].ToString() == "Lcl Rotation")
                {
                    bone.RotationEuler = new Vector3(
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 1]),
                        MathExt.DegToRad((float)(double)prop.Properties[PropertyDescSize + 2]));
                }

                if (prop.Properties[0].ToString() == "Lcl Scaling")
                {
                    bone.Scale = new Vector3((float)(double)prop.Properties[PropertyDescSize], (float)(double)prop.Properties[PropertyDescSize + 1], (float)(double)prop.Properties[PropertyDescSize + 2]);
                }
            }

            bone.Rotation = preRot * bone.Rotation * postRot;

            return(bone);
        }