Example #1
0
        static GameObject CreateGameObject(string modelPath, FBXScenePtr scene)
        {
            // get unity scale factor and scale to meter for Unity
            float unitScaleFactor = 1.0f / scene.GetGlobalSettings().GetUnitScaleFactor();

            FBXModelPtr model = scene.GetModel();

            if (model == null)
            {
                return(null);
            }

            // skip scene root if only one node under root, same as Unity does
            if (model.GetChildCount() == 1)
            {
                model = model.GetChild(0);
            }

            GameObject ret = CreateGameObject(modelPath, model, unitScaleFactor, null, Vector3.zero);

            if (ret != null)
            {
                ret.gameObject.name = Path.GetFileNameWithoutExtension(modelPath);
                ret.gameObject.transform.localPosition /= unitScaleFactor;
                ret.gameObject.transform.localScale    /= unitScaleFactor;
            }

            return(ret);
        }
Example #2
0
        static GameObject CreateGameObject(string modelPath, FBXModelPtr model, float unitScaleFactor, Transform parent, Vector3 parentScalePivot)
        {
            GameObject obj = new GameObject();

            // GameObject obj = GameObject.Instantiate<GameObject>();
            if (obj == null)
            {
                return(null);
            }

            // set parent
            obj.transform.SetParent(parent, false);

            // set name
            obj.name = model.GetName();

            // set transform
            Vector3 pivotP = Vector3.zero;
            Vector3 pivotR = Vector3.zero;
            Vector3 pivotS = Vector3.one;

            Vector3    modelP = Vector3.zero;
            Quaternion modelR = Quaternion.identity;
            Vector3    modelS = Vector3.one;

            GetPivotTransform(model, out pivotP, out pivotR, out pivotS);
            GetTrasform(model, out modelP, out modelR, out modelS);
            obj.transform.localPosition = (
                FBXVector3ToUnityVector3(model.GetTranslation()) +
                FBXVector3ToUnityVector3(model.GetPivotRotation()) +
                FBXVector3ToUnityVector3(model.GetRotationOffset()) -
                parentScalePivot
                ) * unitScaleFactor;
            obj.transform.localRotation = FBXVector3ToUnityQuaternion(model.GetPreRotation()) * modelR * Quaternion.Inverse(FBXVector3ToUnityQuaternion(model.GetPostRotation()));
            obj.transform.localScale    = modelS;

            // set mesh
            Mesh mesh = GetMesh(model, unitScaleFactor, FBXVector3ToUnityVector3(model.GetRotationOffset()));

            if (mesh != null)
            {
                mesh.name = model.GetName();
                MeshFilter meshFilter = obj.AddComponent <MeshFilter>();
                meshFilter.mesh = mesh;
            }

            // set material
            List <Material> materials = new List <Material>();

            GetMaterial(modelPath, model, ref materials);
            if (materials.Count > 0 && mesh != null)
            {
                MeshRenderer meshRenderer = obj.AddComponent <MeshRenderer>();
                if (materials.Count == 1)
                {
                    if (meshRenderer.material != null)
                    {
                        GameObject.DestroyImmediate(meshRenderer.material);
                    }

                    meshRenderer.material = materials[0];
                }
                else
                {
                    for (int i = 0; i < materials.Count; ++i)
                    {
                        if (i < meshRenderer.materials.Length)
                        {
                            if (meshRenderer.materials[i] != null)
                            {
                                GameObject.DestroyImmediate(meshRenderer.materials[i]);
                            }
                        }

                        meshRenderer.materials = materials.ToArray();
                    }
                }
            }

            // create childs
            for (int i = 0; i < model.GetChildCount(); ++i)
            {
                CreateGameObject(modelPath, model.GetChild(i), unitScaleFactor, obj.transform, FBXVector3ToUnityVector3(model.GetPivotScale()));
            }

            return(obj);
        }