WPF使用HelixToolkit加载显示3D模型

By qq84628151 没有评论

1.NuGet安装HelixToolkit.Wpf

2.界面添加3D视图控件,并绑定模型变量

<helix:HelixViewport3D x:Name="view1">
    <ModelVisual3D>
        <helix:GridLinesVisual3D />
        <helix:DefaultLights />
        <ModelVisual3D Content="{Binding CurrentModel}" />
    </ModelVisual3D>
</helix:HelixViewport3D>

3.后台代码加载3d模型文件。

public class MainWindowViewModel : INotifyPropertyChanged
{
    public Model3D CurrentModel { get; set; }

    private readonly IHelixViewport3D viewport;

    public MainWindowViewModel(IHelixViewport3D viewport)
    {
        this.viewport = viewport;
        var mi = new ObjReader();
        CurrentModel = mi.Read(@"D:\xxxxxxxx\汽车.obj");

        Task.Run(async () =>
        {
            await Task.Delay(10);
            App.Current.Dispatcher.Invoke(() =>
            {
                this.viewport.ZoomExtents(0);
            });
        });
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

效果图: