Multi-Touch 3D camera sample added to a new version of Ab3d.PowerToys

by abenedik 7. May 2011 23:20

A new version of Ab3d.PowerToys was just published.

The work on a new version was started as a response to the user post on the forum that asked how it would be possible to control a 3D camera from Ab3d.PowerToys library with his fingers on a multi-touch tablet pc. This looked as a simple an interested task but ended with discovering a probable bug in .Net 4.0 and creating a workaround for it. Also a discovered problems with WPF Browser applications were also fixed.

The following is a short list of new things in this release:

  • Added multi-touch camera controller sample (for .Net 4.0).
  • Fixed using MaterialTypeConverter in .Net 4.0 applications (for example using the following in XAML: Materila="Blue").
  • Fixed problems in WPF Browser applications (fixed showing licensing windows in partially trusted environment).
  • Added Ab3d.PowerToys WPF Browser applications sample.

 

The following screenshot shows the working multi-touch camera sample.

Because the support for multi-touch devices is available in .Net Framework 4.0 I did not want to put the new code into the library as it would raise the requirements from .Net 3.5 to .Net 4.0. So I have decided to create a sample .Net 4.0. project that would reference Ab3d.PowerToys library and use the multi-touch capabilities of the framework to adjust the camera.

Cameras and camera controllers in Ab3d.PoweToys library are very extendable, so it was really easy to create a multi-touch camera controller.

The following code shows the event handler that handles the touch data:

void ViewportBorder_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    ManipulationDelta delta = e.DeltaManipulation;

    if (delta.Scale.X != 1 || delta.Scale.Y != 1)
    {
        // Scale with adjusting camera distance
        Camera1.Distance *= 1 / ((delta.Scale.X + delta.Scale.Y) / 2);
                
        e.Handled = true;
    }

    if (delta.Translation.X != 0 || delta.Translation.Y != 0)
    {
        // Rotate and move with calling RotateCamera or MoveCamera methods on MouseCameraController1.
        // Both methods take mouse dx and dy as parameters.
        // The amount of rotation is controller by EventsSourceElementRotationChange property on MouseCameraController1: Gets or sets the double value that specifies for how many degrees the camera is rotates when the mouse moves from one side of the EventsSourceElement to another size. The default value is 270.
        // The amount of movement is calculated by MouseCameraController1 that tries to make the movements in 3D just as big to follow the mouse / hand movements
        if (RotateRadioButton.IsChecked ?? false)
            MouseCameraController1.RotateCamera(-delta.Translation.X, -delta.Translation.Y);
        else
            MouseCameraController1.MoveCamera(-delta.Translation.X, -delta.Translation.Y);

        e.Handled = true;
    }
}

As the code shows the pinch zooming is represented as Scale in the DeltaManipulation. The scale is simply used to adjust the camera's distance.

Handling of the translation depends on the state of the RadioButtons that specify if we are rotating or moving the camera. In case of rotating, the RotateCamera method on camera controller is called. It takes the mouse difference as input parameter. In case a camera movement mode is selected, MoveCamera method is called - again it accepts mouse changes.

When I wanted to test the fist version of the code that would rotate a simple box, I got a runtime error that the value "#247589" cannot be assigned to Material. The error pointed to the following line of xaml:

<visuals:BoxVisual3D CenterPosition="0 20 0" Size="40 40 40" Material="#247589"/>

The xaml was copied from other samples for Ab3d.PowerToys library. When the project with other samples was started, there was no runtime exception. But in the new multi-touch sample the same line suddenly produces an exception. This is very strange.

The line defines a 3D box with specified center position, its size and material color. The material color is set by a help of the MaterialTypeConverter that comes with Ab3d.PowerToys library and enables setting the material by simply writing the name of the color (Blue), its hex number (#247589) or specifying image file name that should be used as texture. Without the MaterialTypeConverter the previous line should be written:

<visuals:BoxVisual3D CenterPosition="0 20 0" Size="40 40 40">
    <visuals:BoxVisual3D.Material>
        <DiffuseMaterial>
            <DiffuseMaterial.Brush>
                <SolidColorBrush Color="#247589"/>
            </DiffuseMaterial.Brush>
        </DiffuseMaterial>
    </visuals:BoxVisual3D.Material>
</visuals:BoxVisual3D>

I have removed the Material="#247589" and use the standard WPF was to set the material (as written above). This time the sample worked without errors.

The difference between the working and not working project was the target framework version. The not working was using 4.0.

It looked like that the baml parser (parser that reads the compiled xaml) in .Net 4.0 works does not want to use the custom type converter (MaterialTypeConverter). It was time to search the web. There were many problems with custom type converters, but none was similar to mine. The web did not have a clue about my problems. To make things easier, I created two new project were I wanted to reproduce the problem with as less code as possible. The first attemp showed that type converters on properties and dependancy properties work correctly in .Net 4.0. After some additional checking I have discovered that the Material property in Ab3d.PowerToys library was not created from stretch with Register method, but was created with AddOwner method - I wanted to reuse the already defined property on my own class. That was a breakthrough. When I changed the Register method with the AddOwner on a simplified project, the same error occurred.

After that discovery the workaround was simple - define the Material property from stretch.

This showed a case where assemblies that are created for .Net 3.5 does not work the same when they run with .Net 4.0. I think this should not happen. I am going to report this as a bug to Microsoft Connect.

So a rather simple task lead to interesting discoveries.

 

 

The multi-touch sample is available with the new version of Ab3d.PowerToys library. If you already have the library and do not want to upgrade to latest version just to see the sample, you can also download just the sample from the following link: Ab3d.PowerToys.Multitouch.zip (remember to uncomment the old style 3d box definition in xaml).

EDIT:

From Ab3d.PowerToys version 5.2 on, the multi-touch support is already build into the core Ab3d.PowerToys library into its .Net 4.0 version. This means that you do not need to use special MultiTouchMouseCameraController class for that. All you need to do is to make sure that you are referencing the .Net 4 version of the library from the ".NET 4" subfolder (the .Net 3.5 version does not support multi-touch).

Tags:

Ab3d.PowerToys