user7990701
user7990701

Reputation: 11

Multiple viewport with same model in wpf

I referenced from this Rendering the same model in multiple WPF Viewports

I'm a wpf beginner and have the similar question with that link. I want to show three models which would move relatively and show in different viewport with different camera setting.

At first I tried to add ModelVisual3D to different viewport, and it didn't work obviously.

So I tried to do this below, here is the code:

Model3DGroup model1 = read(path1);
Model3DGroup model2 = read(path2);
Model3DGroup model3 = read(path3);

modelVisual1.Content = model1 ;
modelVisual2.Content = model2 ;
modelVisual3.Content = model3 ;

mainModelVisual.Children.Add(modelVisual1);
mainModelVisual.Children.Add(modelVisual2);
mainModelVisual.Children.Add(modelVisual3);

Viewport1.Children.Add(mainModelVisual);

The reason why I don't use just one Model3DGroup is that I have assign different Transform3D and I will add the hittest in future.(Is that a appropriate reason?)

And now I want to add the model to different viewport like this:

otherMainModelVisual2.Content = mainModelVisual.Content;
otherMainModelVisual3.Content = mainModelVisual.Content;

Viewport2.Children.Add(otherMainModelVisual2);
Viewport3.Children.Add(otherMainModelVisual3);

The viewport1 will show the model normally, but other viewport didn't show anything.

The question is why I can't do this like the link? Or do I have to copy two models and add to different modelvisual like the viewport1?

I'm a beginner, hope anyone could show me in simple code.

Upvotes: 0

Views: 1355

Answers (1)

Rob Hill
Rob Hill

Reputation: 371

As mentioned in the first comment in the code of the question you referenced, each Viewport3D must have its own ModelVisual3D. However, to be more clear:

In WPF, a Visual, including a Visual3D, can only exist in one place in the Visual Tree. To put it another way, a Visual can only have one parent. Your Window (or whatever) is the root of the Tree. So you'll have something like this:

Window
|
|--Grid
   |
   |--ViewPort1
      |
      |--mainModelVisual
         |
         |--modelVisual1
         |--modelVisual2
         |--modelVisual3
   |
   |--ViewPort2
   |--ViewPort3

The reason you are only seeing the model in the first viewport is that the modelVisual1, modelVisual2, and modelVisual3, which are all Visual3D objects, have been assigned as (grand)children of ViewPort1. Assigning them as children of more than one thing simply won't work. When you assign them to be the child of the mainModelVisual, you are locking their place in the Visual Tree, until such time as you remove them.

Notice, however, that the model1, model2, and model3 elements are not shown on the tree. This is because they are not Visual3D objects, but rather ModelGroup3D objects, which is a kind of content, essentially data that instructs the Visual objects on how to draw themselves. You can assign the models to as many ModelVisual3D objects as you'd like. This is what's happening in the linked question. The same content is being used multiple times, but each ViewPort3D has a unique set of Visual objects assigned to it.

You would have the same problem if, for example, you created a Button and tried to add it to two different StackPanel objects. Only one StackPanel would render the button. You could make two different buttons though, and they could both have the same text, assigned from the same string variable.

Upvotes: 2

Related Questions