Adding removing objects to the scene dynamically
#1
.Hi,

In my application I need to allow the user to chose which objects are visible in the display (and they change frequently).

Right now I am adding/removing ModelVisual3D objects from Viewport3D.Children,using Add and Remove methods on the collection.

This seems to work some of the time. But often it throws an exception:
Code:
DXEngine Fatal:     Unhandled exception in Update: Object reference not set to an instance of an object.
    Object reference not set to an instance of an object.
    NullReferenceException
       at Ab3d.DirectX.MeshObjectNode.OnInitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SharedDXSceneResource.InitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SceneNode.(SceneNode , Int32 , Boolean , Boolean )
       at Ab3d.DirectX.Models.WpfModelVisual3DNode.RecreateContentSceneNode()
       at Ab3d.DirectX.Models.WpfModelVisual3DNode.OnInitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SharedDXSceneResource.InitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SceneNode.(SceneNode , Int32 , Boolean , Boolean )
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.e(Visual3D fq)
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.d()
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.OnInitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SharedDXSceneResource.InitializeResources(DXScene dxScene)
       at Ab3d.DirectX.Models.WpfModelVisual3DNode.e(SceneNodeDirtyFlags& eh)
       at Ab3d.DirectX.Models.WpfModelVisual3DNode.RecreateContentSceneNode()
       at Ab3d.DirectX.Models.WpfModelVisual3DNode.OnInitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SharedDXSceneResource.InitializeResources(DXScene dxScene)
       at Ab3d.DirectX.SceneNode.(SceneNode , Int32 , Boolean , Boolean )
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.e(Visual3D fq)
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.b()
       at Ab3d.DirectX.Models.WpfVisual3DCollectionNode.Update()
       at Ab3d.DirectX.DXScene.Update()
       at Ab3d.DirectX.Controls.DXView.Update()
       at Ab3d.DirectX.Controls.DXView.h(Object ng, EventArgs nh)

I assume there is some difference in the objects known about by the dxscene and the viewport?

How do I *safely* modify which ModelVisual3Ds are shown?  In particular removing (permanently) and then adding new models.

Any help is most appreciated.

regards,

Simon
#2
Thank you for posting error details.

From the call stack I was able to determine that you were adding and removing a PixelVisual3D (MeshObjectNode is used only by PixelVisual3D). I was also able to reproduce the exception - it happens when you show PixelVisual3D, then remove it from its parent and add the same PixelVisual3D instance back to the visual tree.

The problem is that current version of PixelVisual3D disposes its resources when it is removed from the visual tree and the resources are not recreated when it is added back to the visual tree - this causes the exception.

I will fix that in the next version of DXEngine (I do not plan to release it very soon).


To solve this you will need to create a new instance of PixelVisual3D when you will want to show it again.


PS:
I have a very complex test that adds, removes, transforms and otherwise changes the objects in a 3D scene. But unfortunately PixelVisual3D was not included in this test - I have already fixed that.
Andrej Benedik
  


Forum Jump:


Users browsing this thread:
1 Guest(s)