6

我正在尝试在 HelixViewport3D 中加载和显示 3d 模型。

我可以加载模型(OBJ),但我不明白如何将模型放入视口。

这是我的 WPF 表单的屏幕截图...主要形式

viewprot 被命名为“myView”——我想我可以加入它来添加我的模型,但我看不出有什么明显的用处。

这是我的 XAML 形式:

 <Window x:Class="HelixTrial.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
        <HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
            <!-- Remember to add light to the scene -->
            <HelixToolkit:SunLight/>
            <!-- You can also add elements here in the xaml -->
            <HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
        </HelixToolkit:HelixViewport3D>
    </Grid>
</Grid>

这是我的表格的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace HelixTrial
{

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ObjReader CurrentHelixObjReader = new ObjReader();
        Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");

       // Now how to load it into the viewport... ?
    }    
}

}

你可以看到我卡在哪里了。有人可以帮我走上正轨吗?

4

2 回答 2

8

经过一些实验,我找到了解决方案。

我将以下内容添加到我的 XAML 中:

 <ModelVisual3D x:Name="foo"/>

诀窍是给它一个名字,例如'foo'。XAML 现在看起来像这样:

 <Window x:Class="HelixTrial.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
        <HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
            <!-- Remember to add light to the scene -->
            <HelixToolkit:SunLight/>
            <ModelVisual3D x:Name="foo"/>
            <!-- You can also add elements here in the xaml -->
            <HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
        </HelixToolkit:HelixViewport3D>
    </Grid>
</Grid>

然后在代码中(根据我在上面的原始问题中发布的内容)你可以这样做:

 ObjReader CurrentHelixObjReader = new ObjReader();
        Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");

        // Display the model
        foo.Content = MyModel;

当您了解如何操作时很容易;)

于 2013-12-05T14:19:11.440 回答
-3

# 这是我的代码,运行良好 #

<Window x:Class="Helix_Car_Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Helix_Car_Demo"
    xmlns:helix="http://helix-toolkit.org/wpf"
    mc:Ignorable="d"
    Background="Black"
    WindowState="Maximized" WindowStyle="None"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <helix:HelixViewport3D Name="viewport3D" ZoomExtentsWhenLoaded="True" MouseDoubleClick="viewport3D_MouseDoubleClick">
        <helix:SunLight/>
    </helix:HelixViewport3D>
</Grid>

这是我的 .cs 文件

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ModelVisual3D device = new ModelVisual3D();
        device.Content = getModel("hearse.3ds");
        viewport3D.Children.Add(device);
    }

    public Model3D getModel(string path)
    {
        Model3D device = null;
        try
        {
            viewport3D.RotateGesture = new MouseGesture(MouseAction.LeftClick);
            ModelImporter import = new ModelImporter();
            device = import.Load(path);
        }
        catch (Exception e)
        {                   }
        return device;
    }

} }

于 2017-02-06T10:12:22.343 回答