public static void EmitMeshReferenceSO(SceneSerializer s, IOutputStream o, MeshReferenceSO so)
        {
            o.AddAttribute(IOStrings.ASOName, so.Name);
            o.AddAttribute(IOStrings.ASOUuid, so.UUID);
            s.EmitTransform(o, so);
            // [TODO] be smarter about paths
            o.AddAttribute(IOStrings.AReferencePath, so.MeshReferencePath);

            StringBuilder rel_path = new StringBuilder(260); // MAX_PATH

            if (PathRelativePathTo(rel_path,
                                   Path.GetDirectoryName(s.TargetFilePath), FILE_ATTRIBUTE_DIRECTORY,
                                   so.MeshReferencePath, FILE_ATTRIBUTE_NORMAL) == 1)
            {
                o.AddAttribute(IOStrings.ARelReferencePath, rel_path.ToString());
            }
        }
示例#2
0
        public MeshReferenceSO GetMeshReference(SOMaterial defaultMaterial)
        {
            MeshReferenceSO ref_group = new MeshReferenceSO();

            ref_group.MeshReferencePath = sSourcePath;
            ref_group.Create();
            ref_group.Name = UniqueNames.GetNext("MeshReference");

            foreach (ImportedObject obj in SceneObjects)
            {
                DMeshSO meshSO = new DMeshSO();
                meshSO.Create(obj.mesh,
                              (obj.material == null) ? defaultMaterial : obj.material);
                meshSO.Name = UniqueNames.GetNext("ImportMesh");
                ref_group.AddChild(meshSO);
            }

            return(ref_group);
        }
        // add the meshes we have read into SceneObjects list to the given Scene
        public void AppendReadMeshesToScene(FScene scene, bool bSaveHistory)
        {
            if (ImportBehavior == ImportMode.AsMeshReference)
            {
                MeshReferenceSO ref_group = GetMeshReference(scene.DefaultMeshSOMaterial);

                var change = new AddSOChange()
                {
                    scene = scene, so = ref_group
                };
                if (bSaveHistory)
                {
                    scene.History.PushChange(change);
                }
                else
                {
                    change.Apply();
                }
            }
            else
            {
                foreach (ImportedObject obj in SceneObjects)
                {
                    MeshSO meshSO = new MeshSO();
                    meshSO.Create(obj.mesh,
                                  (obj.material == null) ? scene.DefaultMeshSOMaterial : obj.material);
                    meshSO.Name = UniqueNames.GetNext("ImportMesh");

                    var change = new AddSOChange()
                    {
                        scene = scene, so = meshSO
                    };
                    if (bSaveHistory)
                    {
                        scene.History.PushChange(change);
                    }
                    else
                    {
                        change.Apply();
                    }
                }
            }
        }
示例#4
0
        public virtual SceneObject BuildMeshReference(FScene scene, string sSceneFilePath, TypedAttribSet attributes)
        {
            string sAbsPath = "", sRelPath = "";
            bool   bAbsPathOK = safe_set_property_s(attributes, IOStrings.AReferencePath, (str) => { sAbsPath = str; });
            bool   bRelPathOK = safe_set_property_s(attributes, IOStrings.ARelReferencePath, (str) => { sRelPath = str; });

            string sScenePathDir = Path.GetDirectoryName(sSceneFilePath);

            // ok we are going to try really hard to find references...
            string sUsePath      = "";
            string sBaseFilename = "";

            if (bRelPathOK)
            {
                sBaseFilename = Path.GetFileName(sRelPath);

                // first we check if relative path exists
                string sAbsRelPath = Path.Combine(sScenePathDir, sRelPath);
                if (System.IO.File.Exists(sAbsRelPath))
                {
                    DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using relative path " + sRelPath);
                    sUsePath = sAbsRelPath;
                }

                // if not, we try appending filename to scene path
                if (sUsePath == "")
                {
                    string sLocalPath = Path.Combine(sScenePathDir, sBaseFilename);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }

                // if that fails we accumulate relative path segments (from last folder backwards)
                // and see if we can find the file in any of those
                List <string> subdirs    = new List <string>(sRelPath.Split(Path.DirectorySeparatorChar).Reverse());
                int           N          = subdirs.Count;
                string        sAccumPath = "";
                for (int i = 1; i < N && sUsePath == ""; ++i)
                {
                    sAccumPath = Path.Combine(subdirs[i], sAccumPath);
                    string sLocalPath = Path.Combine(Path.Combine(sScenePathDir, sAccumPath), sBaseFilename);
                    DebugUtil.Log(2, "trying " + sLocalPath);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }
            }

            // ok if all that failed, try absolute path
            if (sUsePath == "" && bAbsPathOK)
            {
                sBaseFilename = Path.GetFileName(sAbsPath);
                if (System.IO.File.Exists(sAbsPath))
                {
                    DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using absolute path " + sAbsPath);
                    sUsePath = sAbsPath;
                }

                // try appending filename to scene path in case we didn't have a relative path at all (bRelPathOK = false)
                if (sUsePath == "")
                {
                    string sLocalPath = Path.Combine(sScenePathDir, sBaseFilename);
                    if (System.IO.File.Exists(sLocalPath))
                    {
                        DebugUtil.Log(2, "[UnitySerialization.BuildMeshReference] using local path " + sLocalPath);
                        sUsePath = sLocalPath;
                    }
                }
            }

            if (sUsePath == "")
            {
                emit_message("referenced mesh does not exist at path [" + sAbsPath + "] or [" + sRelPath + "] ");
                return(null);
            }

            SceneMeshImporter import = new SceneMeshImporter();
            bool bOK = import.ReadFile(sUsePath);

            if (bOK == false && import.LastReadResult.code != g3.IOCode.Ok)
            {
                emit_message("import of mesh [" + sUsePath + "] failed: " + import.LastReadResult.message);
                // [TODO] how can we show this message?
                //HUDUtil.ShowCenteredStaticPopupMessage("popups/error_reading_file", activeCockpit);
                return(null);
            }
            MeshReferenceSO refSO = import.GetMeshReference(scene.DefaultMeshSOMaterial);

            safe_set_property_s(attributes, IOStrings.ASOName, (s) => { refSO.Name = s; });
            RestoreTransform(refSO, attributes);
            return(refSO);
        }
 public static void Emit(this SceneSerializer s, IOutputStream o, MeshReferenceSO so)
 {
     o.AddAttribute(IOStrings.ASOType, IOStrings.TypeMeshReference);
     EmitMeshReferenceSO(s, o, so);
 }