/// <summary> /// Loads the specified file stream. User must provider custom texture loader to load texture files. /// </summary> /// <param name="fileStream">The file stream.</param> /// <param name="filePath">The filePath. Used to load texture.</param> /// <param name="formatHint">The format hint.</param> /// <param name="texturePathResolver">The custom texture path resolver</param> /// <param name="scene">The scene.</param> /// <returns></returns> public ErrorCode Load(Stream fileStream, string filePath, string formatHint, out HelixToolkitScene scene, ITexturePathResolver texturePathResolver = null) { path = filePath; ErrorCode = ErrorCode.None; AssimpContext importer = null; var useExtern = false; if (Configuration.ExternalContext != null) { importer = Configuration.ExternalContext; useExtern = true; } else { importer = new AssimpContext(); } configuration.TexturePathResolver = texturePathResolver; Clear(); scene = null; try { if (!importer.IsImportFormatSupported(formatHint)) { return(ErrorCode.FileTypeNotSupported | ErrorCode.Failed); } if (!useExtern && Configuration.AssimpPropertyConfig != null) { foreach (var config in Configuration.AssimpPropertyConfig) { importer.SetConfig(config); } } importer.Scale = configuration.GlobalScale; var postProcess = configuration.AssimpPostProcessSteps; if (configuration.FlipWindingOrder) { postProcess |= PostProcessSteps.FlipWindingOrder; } var assimpScene = importer.ImportFileFromStream(fileStream, postProcess, formatHint); return(BuildScene(assimpScene, out scene)); } catch (Exception ex) { logger.LogError(ex.Message); ErrorCode = ErrorCode.Failed; AssimpExceptionOccurred?.Invoke(this, ex); return(ErrorCode); } finally { if (!useExtern) { importer.Dispose(); } } }
/// <summary> /// Exports to file. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="root">The root.</param> /// <param name="formatId">The format identifier. <see cref="SupportedFormats"/></param> /// <returns></returns> public ErrorCode ExportToFile(string filePath, HxScene.SceneNode root, string formatId) { Clear(); AssimpContext exporter = null; var useExtern = false; if (Configuration.ExternalContext != null) { exporter = Configuration.ExternalContext; useExtern = true; } else { exporter = new AssimpContext(); } if (!exporter.IsExportFormatSupported(Path.GetExtension(filePath))) { return(ErrorCode.Failed | ErrorCode.FileTypeNotSupported); } var scene = CreateScene(root); var postProcessing = configuration.PostProcessing; if (configuration.FlipWindingOrder) { postProcessing |= PostProcessSteps.FlipWindingOrder; } try { if (!exporter.ExportFile(scene, filePath, formatId, postProcessing)) { logger.LogError("Export failed. FilePath: {}; Format: {}", filePath, formatId); return(ErrorCode.Failed); } return(ErrorCode.Succeed); } catch (Exception ex) { logger.LogError(ex.Message); AssimpExceptionOccurred?.Invoke(this, ex); } finally { if (!useExtern) { exporter.Dispose(); } } return(ErrorCode.Failed); }
/// <summary> /// Exports to BLOB. /// </summary> /// <param name="root">The root.</param> /// <param name="formatId">The format identifier.</param> /// <param name="blob">The BLOB.</param> /// <returns></returns> public ErrorCode ExportToBlob(HxScene.SceneNode root, string formatId, out ExportDataBlob blob) { Clear(); AssimpContext exporter = null; var useExtern = false; if (Configuration.ExternalContext != null) { exporter = Configuration.ExternalContext; useExtern = true; } else { exporter = new AssimpContext(); } var scene = CreateScene(root); var postProcessing = configuration.PostProcessing; if (configuration.FlipWindingOrder) { postProcessing |= PostProcessSteps.FlipWindingOrder; } blob = null; try { blob = exporter.ExportToBlob(scene, formatId, postProcessing); return(ErrorCode.Succeed); } catch (Exception ex) { logger.LogError(ex.Message); AssimpExceptionOccurred?.Invoke(this, ex); } finally { if (!useExtern) { exporter.Dispose(); } } return(ErrorCode.Failed); }