3D in WPF

Overview

If an application is using some 3D content it would be very handy if it can be used just as simple as images are used – by directly importing 3D models, materials, lights and cameras from a file into the application.

The most commonly used file format for storing 3D content is 3ds file format. This format was used by AutoDesk's 3D Studio program. Because of the great popularity of this program, the file format has become very widely used. Almost all 3D modeling programs support some kind of import from or export to this file format. Also, there are huge web libraries that are providing objects in this file format (see Links).

The idea is to be able to do something like:

Reader3ds.Read("sample_scene.3ds", myViewport);

With Reader3ds it is possible to read the model information directly from 3ds file and use it in WPF application. This is the main class library that can be downloaded from my website.

With the new version it is possible to import almost all information from 3ds files. So if you get a nice looking 3D object from the internet or have created your own 3D model (for a list of free 3D modeling applications with support for 3ds format see Links), with the new Reader3ds in most cases the imported model in WPF will look just as original. There are some differences between models description in 3ds file format and 3D objects in WPF. The new version adapts the readed models so they fit into WPF objects model. The Reader3ds is described in more details in the Features and Usage sections.

So let me quickly describe the other two items that can be downloaded from my web site. The first one is MeshUtility assembly that contains two classes: MeshFactory for preparing the model for different shading models and XamlExport for exporting Model3DGroup into xaml text.

MeshFactory class is used to prepare 3D objects for different shading types. Original models sometimes have some anomalies when they are shown in WPF. They can be repaired using the GetGouraudModelGroup method. If you want to have a flat shaded model, you can use the GetFlatModelGroup method. More details about MeshFactory can be found in my article on CodeProject. The difference can also be seen on the following picture:

XamlExporter class is used to export Model3DGroup into XAML string. Its main method ExportModel3DGroup takes two parameters: Model3DGroup and Camera. They are used to produce the XAML string. You can also set some additional parameters. With DoubleFormatString property you can set the format for all double values - its default value is "#0.#####" (decimal values are returned with five decimal digits). You can also set DefaultMaterialXaml that is used when there is no material set for the object. You can also set the XamlTemplate or load if from disk with LoadXamlTemplate. This template is used to produce the while xaml string with Viewport3D and other WPF controls, so it can be directly used in xaml page or in XamlPad.

MeshInfo class is used to create a simpel report from Mesh3DGroup - sample of a report can be seen here. It contains the description of all the used materials, lights, cameras and the count of all the read Positions, TriangleIndices, etc.

There is also Viewer3ds - a free 3ds file viewer and xaml application. It is also one of the first applications with animated 3D buttons and other WPF goodies like reflection, transparencies and work area zoom.

Viewer3ds screenshot

Copyright © 2007 by Andrej Benedik. All Rights Reserved.