4

我在一个小项目上工作,我使用 helix-3DToolkit。我想在运行时添加一些 3D 对象。

当我添加一个元素并尝试从 MeshBuilder 添加 MeshGeometry3D 时,每次都会得到相同的 nullreference 异常,并带有以下堆栈跟踪:

bei HelixToolkit.Wpf.SharpDX.MeshGeometryModel3D.Attach(IRenderHost host) in c:\TeamCity\buildAgent\work\4ff441572eb3435d\Source\HelixToolkit.Wpf.SharpDX\Model\Elements3D\MeshGeometryModel3D.cs:Zeile 57.
bei HelixToolkit.Wpf.SharpDX.GeometryModel3D.OnGeometryChanged(DependencyPropertyChangedEventArgs e) in c:\TeamCity\buildAgent\work\4ff441572eb3435d\Source\HelixToolkit.Wpf.SharpDX\Model\Elements3D\AbstractElements3D\GeometryModel3D.cs:Zeile 59.
bei HelixToolkit.Wpf.SharpDX.MaterialGeometryModel3D.OnGeometryChanged(DependencyPropertyChangedEventArgs e) in c:\TeamCity\buildAgent\work\4ff441572eb3435d\Source\HelixToolkit.Wpf.SharpDX\Model\Elements3D\AbstractElements3D\MaterialGeometryModel3D.cs:Zeile 167.
bei HelixToolkit.Wpf.SharpDX.GeometryModel3D.GeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in c:\TeamCity\buildAgent\work\4ff441572eb3435d\Source\HelixToolkit.Wpf.SharpDX\Model\Elements3D\AbstractElements3D\GeometryModel3D.cs:Zeile 35.
bei System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
bei System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
bei System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
bei HelixToolkit.Wpf.SharpDX.GeometryModel3D.set_Geometry(Geometry3D value) in c:\TeamCity\buildAgent\work\4ff441572eb3435d\Source\HelixToolkit.Wpf.SharpDX\Model\Elements3D\AbstractElements3D\GeometryModel3D.cs:Zeile 26.
bei Metromec.Qrt.CollisionDetection.Domain.Bullet.ViewControl.BulletControl.UpdateView(MeshBuilder meshBuilder) in j:\quartis\_dev\src\CarriageComposite\Source\Services\CollisionDetection\Domain\Bullet\Control\BulletControl.xaml.cs:Zeile 137.

xaml 看起来像这样:

<htx:Viewport3DX             
        x:Name="viewPort" 
        Title="Test"
        SubTitle="subtest"
        TextBrush="Black"            
        ShowCoordinateSystem="True"
        CoordinateSystemLabelForeground="Red"
        UseDefaultGestures="True">
        <htx:AmbientLight3D Color="{Binding AmbientLightColor}"/>
        <htx:DirectionalLight3D Color="{Binding DirectionalLightColor}" Direction = "{Binding DirectionalLightDirection}"/>
        <htx:MeshGeometryModel3D x:Name="test" Geometry="{Binding Model}" Transform = "{Binding Model1Transform}" Material="{Binding RedMaterial}"   />

    </htx:Viewport3DX>

视图模型:

public class BulletViewModel 
{
  //...

  public MeshGeometry3D Model
  {
    get { return _model; }
    set 
    { 
      //SetValue(ref _model, value, "Model"); 
      _model = value;
    }
}

在我的表格中,我有这个:

public void Initialize()
{
  //..
  _timer.Interval = 500;
  _timer.Tick += _timer_Elapsed;
  _timer.Start();

}

private void _timer_Elapsed(object sender, EventArgs eventArgs)
{
    _timer.Stop(); //not needed, should work without
    var meshBuilder = GetMeshBuilder();
    AddBox(meshBuilder, new Vector3(1,1,1), new Vector3(1,1,1) );
    UpdateView(meshBuilder);
    _timer.Start();
}

private void AddBox(MeshBuilder meshBuilder, Vector3 centre, Vector3 size)
{
  meshBuilder.AddBox(centre,size.X, size.Y, size.Z, BoxFaces.All);
}

private MeshBuilder GetMeshBuilder()
{
  return new MeshBuilder();
}

private void UpdateView(MeshBuilder meshBuilder)
{

  try
  {
    viewPort.RenderTechnique = Techniques.RenderBlinn;
    var geom = meshBuilder.ToMeshGeometry3D();
    if (geom != null)
    {
      geom.Colors = new Color4Collection(geom.TextureCoordinates.Select(x => x.ToColor4()));
      _viewModel.RedMaterial = PhongMaterials.Red;
      _viewModel.Model1Transform = new TranslateTransform3D(0, 0, 0);
      _viewModel.Model = geom;
      viewPort.DataContext = null;
      viewPort.DataContext = _viewModel;
      test.Geometry = geom; //--> this doesn't work and gives a exception
    }
  }
  catch (Exception e)
  {
    Console.WriteLine(e);
  }
}

这段代码有什么问题?如何在运行时将一些元素添加到我的视口?为此使用 MeshGeometryModel3D 是否正确?我试图添加一个新的 MeshGeometryModel3D 几何,我改变了现有的但没有任何效果。

4

0 回答 0