|
| 1 | +// Copyright © 2017-2025 QL-Win Contributors |
| 2 | +// |
| 3 | +// This file is part of QuickLook program. |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +using Assimp; |
| 19 | +using HelixToolkit.Wpf; |
| 20 | +using System; |
| 21 | +using System.IO; |
| 22 | +using System.Linq; |
| 23 | +using System.Windows.Media; |
| 24 | +using System.Windows.Media.Media3D; |
| 25 | + |
| 26 | +namespace QuickLook.Plugin.HelixViewer; |
| 27 | + |
| 28 | +public partial class HelixPanel |
| 29 | +{ |
| 30 | + private void Load() |
| 31 | + { |
| 32 | + var importerType = Importer.GetImporterType(_path); |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + if (importerType == ImporterType.Extended) |
| 37 | + { |
| 38 | + var context = new AssimpContext(); |
| 39 | + var scene = context.ImportFile(_path, PostProcessSteps.Triangulate); |
| 40 | + |
| 41 | + foreach (var mesh in scene.Meshes) |
| 42 | + { |
| 43 | + var geometry = new MeshGeometry3D() |
| 44 | + { |
| 45 | + Positions = [.. mesh.Vertices.Select(v => new Point3D(v.X, v.Y, v.Z))], |
| 46 | + TriangleIndices = [.. mesh.GetIndices()], |
| 47 | + }; |
| 48 | + var model = new GeometryModel3D() |
| 49 | + { |
| 50 | + Geometry = geometry, |
| 51 | + Material = Materials.Gray, |
| 52 | + }; |
| 53 | + |
| 54 | + modelVisual.Content = model; |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + var modelImporter = new ModelImporter(); |
| 60 | + var model3DGroup = modelImporter.Load(_path); |
| 61 | + var diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(0xA0, 0xA0, 0xA0))); |
| 62 | + |
| 63 | + foreach (GeometryModel3D child in model3DGroup.Children.Cast<GeometryModel3D>()) |
| 64 | + { |
| 65 | + child.Material = diffuseMaterial; |
| 66 | + child.BackMaterial = diffuseMaterial; |
| 67 | + } |
| 68 | + |
| 69 | + modelVisual.Content = model3DGroup; |
| 70 | + } |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + errorInfo.Text = $"[{nameof(ImporterType)}.{importerType}] {ex}"; |
| 75 | + errorInfo.Visibility = System.Windows.Visibility.Visible; |
| 76 | + viewer.Visibility = System.Windows.Visibility.Collapsed; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +file static class Importer |
| 82 | +{ |
| 83 | + public static ImporterType GetImporterType(string path) |
| 84 | + { |
| 85 | + if (string.IsNullOrEmpty(path)) |
| 86 | + return ImporterType.Unknown; |
| 87 | + |
| 88 | + return Path.GetExtension(path).ToLower() switch |
| 89 | + { |
| 90 | + ".stl" or ".obj" or ".3ds" or ".lwo" or ".ply" => ImporterType.Default, |
| 91 | + ".fbx" or ".3mf" or ".glb" or ".gltf" or ".dae" or ".dxf" => ImporterType.Extended, |
| 92 | + ".pmx" => ImporterType.Extended_MMD, |
| 93 | + _ => ImporterType.Unknown, |
| 94 | + }; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +file enum ImporterType |
| 99 | +{ |
| 100 | + Unknown, |
| 101 | + Default, |
| 102 | + Extended, |
| 103 | + Extended_MMD, |
| 104 | +} |
0 commit comments