Ejemplo n.º 1
0
 public override void Load()
 {
     var loader = new TextureLoader(_file, _baseDir);
     Callback(_file, loader.Image, loader.ActualLocation, loader.Result);
 }
Ejemplo n.º 2
0
        private void SetImage(Image image, TextureLoader.LoadResult result)
        {
            Debug.Assert(State == TextureState.LoadingPending);

            lock (_lock)
            {
                _image = image;
                // set proper state
                State = result != TextureLoader.LoadResult.Good ? TextureState.LoadingFailed : TextureState.WinFormsImageCreated;
                Debug.Assert(result != TextureLoader.LoadResult.Good || _image != null);

                
#if DETECT_ALPHA_EARLY
                if (_image != null)
                {
                    TryDetectAlpha();
                }
#endif
            }
        }
Ejemplo n.º 3
0
        private void DoExport(Scene scene, string id)
        {
            var overwriteWithoutConfirmation = checkBoxNoOverwriteConfirm.Checked;

            var path = textBoxPath.Text.Trim();

            path = (path.Length > 0 ? path : scene.BaseDir);
            var name     = textBoxFileName.Text.Trim();
            var fullPath = Path.Combine(path, name);

            if (!overwriteWithoutConfirmation && Path.GetFullPath(fullPath) == Path.GetFullPath(scene.File))
            {
                if (MessageBox.Show("This will overwrite the current scene's source file. Continue?", "Warning",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    PushLog("Canceled");
                    return;
                }
            }

            var copyTextures          = checkBoxCopyTexturesToSubfolder.Checked;
            var relativeTexturePaths  = checkBoxUseRelativeTexturePaths.Checked;
            var includeAnimations     = checkBoxIncludeAnimations.Checked;
            var includeSceneHierarchy = checkBoxIncludeSceneHierarchy.Checked;

            var textureCopyJobs          = new Dictionary <string, string>();
            var textureDestinationFolder = textBoxCopyTexturesToFolder.Text;

            PushLog("*** Export: " + scene.File);

            if (copyTextures)
            {
                try
                {
                    Directory.CreateDirectory(Path.Combine(path, textureDestinationFolder));
                }
                catch (Exception)
                {
                    PushLog("Failed to create texture destination directory " + Path.Combine(path, textureDestinationFolder));
                    return;
                }
            }

            progressBarExport.Style = ProgressBarStyle.Marquee;
            progressBarExport.MarqueeAnimationSpeed = 5;

            // Create a shallow copy of the original scene that replaces
            // all the texture paths with their corresponding output paths,
            // and omits animations if requested.
            var sourceScene = new Assimp.Scene();

            sourceScene.Textures   = scene.Raw.Textures;
            sourceScene.SceneFlags = scene.Raw.SceneFlags;
            sourceScene.RootNode   = scene.Raw.RootNode;
            sourceScene.Meshes     = scene.Raw.Meshes;
            sourceScene.Lights     = scene.Raw.Lights;
            sourceScene.Cameras    = scene.Raw.Cameras;

            if (includeAnimations)
            {
                sourceScene.Animations = scene.Raw.Animations;
            }

            var uniques        = new HashSet <string>();
            var textureMapping = new Dictionary <string, string>();

            PushLog("Locating all textures");
            foreach (var texId in scene.TextureSet.GetTextureIds())
            {
                var destName = texId;

                // Broadly skip over embedded (in-memory) textures
                if (destName.StartsWith("*"))
                {
                    PushLog("Ignoring embedded texture: " + destName);
                    continue;
                }

                // Locate the texture on-disk
                string diskLocation;
                try
                {
                    TextureLoader.ObtainStream(texId, scene.BaseDir, out diskLocation).Close();
                } catch (IOException)
                {
                    PushLog("Failed to locate texture " + texId);
                    continue;
                }

                if (copyTextures)
                {
                    var count = 1;
                    // Enforce unique output names
                    do
                    {
                        destName = Path.Combine(path, Path.Combine(textureDestinationFolder,
                                                                   Path.GetFileNameWithoutExtension(diskLocation) +
                                                                   (count == 1
                                                     ? ""
                                                     : (count.ToString(CultureInfo.InvariantCulture) + "_")) +
                                                                   Path.GetExtension(diskLocation)
                                                                   ));
                        ++count;
                    } while (uniques.Contains(destName));
                    uniques.Add(destName);
                }

                if (relativeTexturePaths)
                {
                    textureMapping[texId] = GetRelativePath(path + "\\", destName);
                }
                else
                {
                    textureMapping[texId] = destName;
                }
                textureCopyJobs[diskLocation] = destName;

                PushLog("Texture " + texId + " maps to " + textureMapping[texId]);
            }

            foreach (var mat in scene.Raw.Materials)
            {
                sourceScene.Materials.Add(CloneMaterial(mat, textureMapping));
            }

            var t = new Thread(() =>
            {
                using (var v = new AssimpContext())
                {
                    PushLog("Exporting using Assimp to " + fullPath + ", using format id: " + id);
                    var result = v.ExportFile(sourceScene, fullPath, id, includeSceneHierarchy
                        ? PostProcessSteps.None
                        : PostProcessSteps.PreTransformVertices);
                    _main.BeginInvoke(new MethodInvoker(() =>
                    {
                        progressBarExport.Style = ProgressBarStyle.Continuous;
                        progressBarExport.MarqueeAnimationSpeed = 0;

                        if (!result)
                        {
                            // TODO: get native error message
                            PushLog("Export failure");
                            MessageBox.Show("Failed to export to " + fullPath, "Export error",
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            if (copyTextures)
                            {
                                PushLog("Copying textures");
                                foreach (var kv in textureCopyJobs)
                                {
                                    PushLog(" ... " + kv.Key + " -> " + kv.Value);
                                    try
                                    {
                                        File.Copy(kv.Key, kv.Value, false);
                                    }
                                    catch (IOException)
                                    {
                                        if (!File.Exists(kv.Value))
                                        {
                                            throw;
                                        }
                                        if (!overwriteWithoutConfirmation && MessageBox.Show("Texture " + kv.Value +
                                                                                             " already exists. Overwrite?", "Overwrite Warning",
                                                                                             MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                                        {
                                            PushLog("Exists already, skipping");
                                            continue;
                                        }
                                        PushLog("Exists already, overwriting");
                                        File.Copy(kv.Key, kv.Value, true);
                                    }
                                    catch (Exception ex)
                                    {
                                        PushLog(ex.Message);
                                    }
                                }
                            }
                            if (checkBoxOpenExportedFile.Checked)
                            {
                                _main.AddTab(fullPath, true, false);
                            }
                            PushLog("Export completed");
                        }
                    }));
                }
            });

            t.Start();
        }