Question

How can I disable the following strange resize / rescaling behaviour of a Viewport3d in WPF? Notice that I do not change the window height:

http://i.stack.imgur.com/jCgPe.png

http://i.stack.imgur.com/Q0V6X.png

How can I disable this "feature" of WPF (or why does it do this?)

Was it helpful?

Solution

To anyone stumbling upon this question, here's the answer by Mike Danes: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/642253a6-e7a5-4ce2-bc08-e91f2634605b/

This auto scaling is a consequence of the way perspective works.

The default field of view is 45 and the default near plane distance is 0.125. This means that a point (x, 0, 0.125) will be visible at the right side of the viewport when its x coordinate is tan(45/2) * 0.125 = 0.051776. Note how the viewport width doesn't play a part in this computation, if you make it wider the point (0.051776, 0, 0.125) will still be visible at the right side of the viewport, that's why the teapot appears larger.

You could compensate for a width increase by changing the field of view and near plane distance, something along these lines:

double newWidth = grid.ActualWidth;
double originalWidth = 509.0;
double originalNearPlaneDistance = 0.125;
double originalFieldOfView = 45.0;
double scale = newWidth / originalWidth;

double fov = Math.Atan(Math.Tan(originalFieldOfView / 2.0 / 180.0 * Math.PI) * scale) * 2.0;
camera.FieldOfView = fov / Math.PI * 180.0;
camera.NearPlaneDistance = originalNearPlaneDistance * scale;

OTHER TIPS

I know this is an old post, but I've come across this a few times, and I'm not sure the accepted answer is correct. Near Plane Distance defines at which point objects are too close too render. I'm not sure how this might affect the scale of the viewport3d and its children. What I have come across is that modifying Viewport3d.Width affects the scale of the 3D scene, while modifying the Height only causes clipping (or adding white space if getting larger). This is not intuitive in my opinion, but maybe its a 3D programming convention I'm not aware of.

My solution for the above problem would be to remove the Viewport3D from the Grid its in, and instead place it inside a canvas. This will prevent the window resize from changing the width of the viewport, and it should stay the same size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top