MeshSaver is a static class containing methods used for saving and loading meshes.
Beispiel #1
0
        public static void ExportRoomToWavefront()
        {
            string selectedFile = EditorUtility.OpenFilePanelWithFilters("Select Room File", MeshSaver.MeshFolderName, new string[] { "Room", "room" });

            if (string.IsNullOrEmpty(selectedFile))
            {
                return;
            }

            string             fileName = Path.GetFileNameWithoutExtension(selectedFile);
            IEnumerable <Mesh> meshes   = null;

            try
            {
                meshes = MeshSaver.Load(fileName);
            }
            catch
            {
                // Handling exceptions, and null returned by MeshSaver.Load, by checking if meshes
                // is still null below.
            }

            if (meshes == null)
            {
                EditorUtility.DisplayDialog(ExportDialogErrorTitle, "Unable to parse selected file.", "Ok");
                return;
            }

            SaveMeshesToWavefront(fileName, meshes);

            // Open the location on where the mesh was saved.
            System.Diagnostics.Process.Start(ExportDirectory);
        }
        /// <summary>
        /// Loads the SpatialMapping mesh from the specified file.
        /// </summary>
        /// <param name="fileName">The name, without path or extension, of the file to load.</param>
        public void Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                Debug.Log("No mesh file specified.");
                return;
            }

            Cleanup();

            List <Mesh> storedMeshes = new List <Mesh>();

            try
            {
                storedMeshes.AddRange(MeshSaver.Load(fileName));

                foreach (Mesh mesh in storedMeshes)
                {
                    GameObject surface  = AddSurfaceObject(mesh, "storedmesh-" + surfaceObjects.Count, transform);
                    Renderer   renderer = surface.GetComponent <MeshRenderer>();

                    if (SpatialMappingManager.Instance.DrawVisualMeshes == false)
                    {
                        renderer.enabled = false;
                    }

                    if (SpatialMappingManager.Instance.CastShadows == false)
                    {
                        renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                    }

                    // Reset the surface mesh collider to fit the updated mesh.
                    // Unity tribal knowledge indicates that to change the mesh assigned to a
                    // mesh collider, the mesh must first be set to null.  Presumably there
                    // is a side effect in the setter when setting the shared mesh to null.
                    MeshCollider collider = surface.GetComponent <MeshCollider>();
                    collider.sharedMesh = null;
                    collider.sharedMesh = surface.GetComponent <MeshFilter>().mesh;
                }
            }
            catch
            {
                Debug.Log("Failed to load " + fileName);
            }
        }
Beispiel #3
0
        // Called every frame.
        private void Update()
        {
            // Keyboard commands for saving and loading a remotely generated mesh file.
#if UNITY_EDITOR
            // S - saves the active mesh
            if (Input.GetKeyUp(KeyCode.S))
            {
                MeshSaver.Save(MeshFileName, SpatialMappingManager.Instance.GetMeshes());
            }

            // L - loads the previously saved mesh into editor and sets it to be the spatial mapping source.
            if (Input.GetKeyUp(KeyCode.L))
            {
                SpatialMappingManager.Instance.SetSpatialMappingSource(this);
                Load(MeshFileName);
            }
#endif
        }
Beispiel #4
0
        // Called every frame.
        private void Update()
        {
            // There are a few keyboard commands we will add when in the editor.
#if UNITY_EDITOR
            // F - to use the 'file' sourced mesh.
            if (Input.GetKeyUp(KeyCode.B))
            {
                SpatialMappingManager.Instance.SetSpatialMappingSource(fileSurfaceObserver);
            }

            // S - saves the active mesh
            if (Input.GetKeyUp(KeyCode.M))
            {
                MeshSaver.Save(fileSurfaceObserver.MeshFileName, SpatialMappingManager.Instance.GetMeshes());
            }

            // L - loads the previously saved mesh into the file source.
            if (Input.GetKeyUp(KeyCode.L))
            {
                fileSurfaceObserver.Load(fileSurfaceObserver.MeshFileName);
            }
#endif
        }