Click or drag to resize
Reader3ds Class
Ab3d.Reader3ds is a class library for importing 3D objects from 3ds files.
Inheritance Hierarchy

Namespace: Ab3d
Assembly: Ab3d.Reader3ds (in Ab3d.Reader3ds.dll) Version: 9.0.5590.1040
Syntax
C#
public class Reader3ds : DependencyObject, ISupportInitialize

The Reader3ds type exposes the following members.

Constructors
  NameDescription
Public methodReader3ds
Constructor
Top
Properties
  NameDescription
Public propertyAddDefaultLight
Add default light (Direction=0, 0, -1) if there are no lights defined in 3ds file. Default value is false.
Public propertyAnimator
Gets and instance of a Animator3ds created with this Reader3ds.
Public propertyCameras
Gets list of all the cameras read from 3ds file. Used because Model3DGroup can have only one camera defined, but 3ds file can have more then one.
Public propertyCurrentFrameNumber
Gets the current frame number that was get by the GetFrame method.
Public propertyDefaultMaterial
Gets or Sets the material that will be applied to the read objects if the object does not have its own material set. Default value is Silver SolidColorBrush.
Public propertyDisableTextureOffset
If set to true than texture offsets and scales are not processed. The default value is true, because many times the textures are not shown correctly if offsets and scales data from 3ds are used.
Public propertyDisableTransparencies
Gets or sets if Transparencies are used in Materials or not.
Public propertyForceTwoSidedMaterials
If true than all the GeometryModels have both Material and BackMaterial set. This means that all the TriangleIndices can be visible from both sides.
Public propertyFramesCount
Number of frames in the read 3ds file. If FramesCount is 1 than the 3ds file does not contain animation data but only one static frame. To get the index of the last frame you have to subtract 1 from the FramesCount. For example if the last animation takes place from frame 0 to frame 30, than the FramesCount will return 31.
Public propertyImportCameras
Gets or sets if Cameras are imported.
Public propertyImportLights
Gets or sets if Lights are imported.
Public propertyImportSpecularMaterial
Gets or sets if the SpecularMaterial is imported - used. If false DiffuseMaterial is always used.
Public propertyStatic memberCode exampleInstance
Gets static instance of Reader3ds so you do not need to create your own instance of it.
Public propertyIsBroken
Gets a boolean that specifies if the 3ds file was broken.
Public propertyIsCameraAnimated
Gets a Boolean that specifies if the 3ds file defined camera animation.
Public propertyIsTransparencySortingEnabled
Gets or sets a boolean that specifies if simple transparency sorting is performed after the 3D objects are read. If true the transparent objects are moved after non-transparent objects. Default value is true.
Public propertyStatic memberLicenseInfo
License information
Public propertyLights
Gets list of all the lights read from 3ds file.
Public propertyMaterials
Gets the Dictionary that can be used to access all the WPF materials defined in 3ds file by their name.
Public propertyMaterials3ds
Gets the Dictionary that can be used to access all the Material3ds defined in 3ds file. The Material3ds contain all the material properties that are stored in 3ds file.
Public propertyMissingTextureFileName
Gets or sets the file name with full path of the image that will be used when the texture image is not present. Default value is null.
Public propertyCode exampleNamedObjects

Gets the Dictionary that can be used to access all the objects in 3ds file by its name as defined in 3d modeling application.

Public propertyReadLightRange
Gets or sets a Boolean that specifies if Range for PointLight and SpotLight is read. Default value is false.
Public propertyResolveAllTextureFiles
Gets or sets a Boolean that specifies if all texture files are resolved with ResolveTextureFileCallback (if value is true). If false (by default) the ResolveTextureFileCallback is called only for the missing texture files.
Public propertyRootModel3DGroup
The root Model3DGroup from the last read 3ds file.
Public propertyShading
Defines the smoothing type that is applied to objects. Default value is SmoothingGroups that uses the Smoothing Groups values stored in 3ds file to define which edges are smooth and which are flat. The SmoothingGroups value produces the results as in the 3d model designer. However if you wish to force the flat or smooth looking objects choose either Flat or Smooth. Also if performance is much more important than rendering quality choose None.
Public propertyCode exampleSource
Gets or sets the Source of the 3ds file.
Public propertyTargetViewport
Gets the target Viewport3D that was used with ReadFile, GetFrame, etc.
Public propertyTexturesPath
Gets or sets the path where the textures are located. If null or "" the path of the 3ds file is used. It is also possible to set TexturesPath to url of the textures (http://...) or to the application resources ("pack://application:,,,/XAMLBrowserApplication1;component/models") Default value is null.
Public propertyThrowMissingTextureException
Gets or sets if Reader3ds.MissingTextureException is thrown if the texture is not found. If false then if the MissingTextureFileName is set it is used, if not set the simple SolidColorBrush material is used instead of texture (color is read from material). Default value is false.
Public propertyTransformer
Gets and instance of a Transformer3ds created with this Reader3ds. Transformer3ds is a helper class that helps animate and transform (rotate, scale and translate) the objects read with Reader3ds.
Public propertyTransparentObjectsCount
Gets the number of transparent objects in the read 3ds file.
Public propertyTryReadBrokenFiles
Gets or sets a boolean that specifies if Reader3ds tries to read broken 3ds files. Default value is false which throws an exception when the structure of the 3ds file is broken.
Public propertyUseModelTransforms
Default is false. If true than the TranslateTransform3D, ScaleTransform3D and RotateTransform3D are applied to each GeometryModel3D as they were applied in 3D modeling application. If false than the positions are already fully transformed. Setting it to true can be used to get different transformations for each frames.
Public propertyUseOldCoordinateAxis Obsolete.
The previous version has wrongly convert position from 3ds coordinate system to WPF coordinate system. The difference is that the X axis now points to the right instead of the left and the Z axis points to the viewer and not away from it. This version by default uses new coordinate system, but if your application depends on the old one, you can simple set this property to true. But note that this can only be a temporary solution.
Caution note Caution
Please note that this property is marked as obsolete and is valid only for backward compatibility and will be removed after version 4.2
Top
Methods
  NameDescription
Public methodBeginInit
Signals the line 3D that initialization is starting.
Public methodDumpNamedObjects
If run in immediate window the method displays the hierarchy of all objects, lights and Model3DGroups in this instance of Reader3ds. To get the NamedObjects description text use GetNamedObjectsText() method.
Public methodEndInit
Signals the line 3D that initialization is complete.
Public methodGetCameraForFrame
Gets the camera with index cameraIndex for the frameNo. Note: frameNo is double - so it is possible to create smoother animation with getting the positions within frames. See GetFrame for more details.
Public methodCode exampleGetFrame(Double)
Gets the Model3DGroup for frameNo. The frameNo can also be a not integer value to display model between frames - for example frameNo = 1.5 will get the model as it it in the middle between 2st and 3nd frame.
Public methodCode exampleGetFrame(Int32)
Gets the Model3DGroup for frame. No The frameNo can also be a not integer value to display model between frames - for example frameNo = 1.5 will get the model as it it in the middle between 2st and 3nd frame.
Public methodCode exampleGetFrame(Double, Viewport3D)
Gets the Model3DGroup for frameNo. No The frameNo can also be a not integer value to display model between frames - for example frameNo = 1.5 will get the model as it it in the middle between 2st and 3nd frame.
Public methodCode exampleGetFrame(Int32, Viewport3D)
Gets the Model3DGroup for frameNo
Public methodCode exampleGetFrame(Double, Viewport3D, Int32)
Gets the Model3DGroup for frameNo. No The frameNo can also be a not integer value to display model between frames - for example frameNo = 1.5 will get the model as it it in the middle between 2st and 3nd frame.
Public methodCode exampleGetFrame(Int32, Viewport3D, Int32)
Gets the Model3DGroup for frameNo. No The frameNo can also be a not integer value to display model between frames - for example frameNo = 1.5 will get the model as it it in the middle between 2st and 3nd frame.
Public methodGetKeyFramesData
Returns a collection of key frame tracks defined for the specified modelName. GetKeyFramesData return null if modelName does not exist.
Public methodGetNamedObjectsText
Returns a hierarchy text of all objects, lights and Model3DGroups in this instance of Reader3ds. Used in DumpNamedObjects which displays the hierarchy of named objects (when run in immediate window).
Public methodGetOptimizedMeshGeometry3D
Returns OptimizedMeshGeometry3D struct for the specified model name. OptimizedMeshGeometry3D contains Normals, Positions, TextureCoordinates and TriangleIndices lists that can be used to get quick access to mesh data (access to data is much faster than when using MeshGeometry3D).
Public methodGetXaml
Gets the XAML of the last read objects with default settings.
Public methodGetXaml(String)
Gets the XAML of the objectName from the last read objects with default settings.
Public methodGetXaml(WpfXamlWriterSettings)
Gets the XAML of the last read objects with custom settings.
Public methodGetXaml(WpfXamlWriterSettings, String)
Gets the XAML of the objectName from the last read objects with custom settings.
Public methodStatic memberRead(String) Obsolete.
This method is obsolete - use ReadFile(String) instead.
Public methodStatic memberRead(String, Viewport3D) Obsolete.
This method is obsolete - use ReadFile(String, Viewport3D) instead.
Public methodStatic memberRead(String, Viewport3D, Int32) Obsolete.
This method is obsolete - use ReadFile(String, Viewport3D, Int32) instead.
Public methodCode exampleReadFile(Stream)
Reads 3ds file from stream and returns its data in Model3DGroup object.
Public methodReadFile(String)
Reads 3ds file and returns its data in Model3DGroup object
Public methodCode exampleReadFile(Stream, Viewport3D)
Reads the 3ds file from stream and adds the 3d objects (as MeshGeometry3D, Lights, Materials) to Viewport3D Also if the 3ds file contains camera information set the Viewport3D's camera to the first camera in 3ds file
Public methodCode exampleReadFile(String, Viewport3D)
Reads the 3ds file and adds the 3d objects (as MeshGeometry3D, Lights, Materials) to Viewport3D Also if the 3ds file contains camera information set the Viewport3D's camera to the first camera in 3ds file
Public methodCode exampleReadFile(Stream, Viewport3D, Int32)
Reads the 3ds file from stream and adds the 3d objects (as MeshGeometry3D, Lights, Materials) to Viewport3D If cameraIndex >= 0 than also sets the camera (if there is one in 3ds file) to Viewport3D
Public methodReadFile(String, Viewport3D, Int32)
Reads the 3ds file and adds the 3d objects (as MeshGeometry3D, Lights, Materials) to Viewport3D If cameraIndex >= 0 than also sets the camera (if there is one in 3ds file) to Viewport3D
Top
Events
  NameDescription
Public eventProgressChanged
ProgressChanged event can be used to get notifications of how much the 3ds file has been read.
Public eventReadingComplete
Event that is raised when the reading of 3ds file is complete
Top
Fields
  NameDescription
Public fieldStatic memberAllLightsGroupName
Key used in NamedObjects dictionary for Model3DGroup that contains all lights
Public fieldStatic memberAllModelsGroupName
Key used in NamedObjects dictionary for Model3DGroup that contains all models
Public fieldCreateTextureCallback
Callback that can be used to customize the process of creating the texture images.
Public fieldStatic memberDefaultMaterialName
Name of the default material - used when there is no material defined
Public fieldStatic memberGroupNameSuffix
Suffix used to name the object groups - for example if there is a GeometryModel3D object named "head" its group has name "head__Group"
Public fieldCode exampleResolveTextureFileCallback
Callback that can be used to resolve missing texture file name or all texture file names (if ResolveAllTextureFiles is set to true).
Public fieldStatic memberRootModelGroupName
Model3DGroup that contains all models and lights - can also be accesses by RootModel3DGroup property
Public fieldStatic memberSourceProperty
SourceProperty
Top
Remarks

It is possible to use Ab3d.Reader3ds in XAML or in code.

To use Reader3ds in XAML define the Reader3ds as Resource:

XAML
<Page.Resources>
    <ab3d:Reader3ds x:Key="MyReader3ds" Source="/Resources/robotarm.3ds"/>
<Page.Resources>

After defining the Reader3ds as resource it can be used with binding to its properties:

XAML
<Viewport3D Camera="{Binding Source={StaticResource MyReader3ds}, Path=Cameras[0]}">
   <ModelVisual3D Content="{Binding Source={StaticResource MyReader3ds}, Path=RootModel3DGroup}"/>
</Viewport3D>

It is also possible to use 3D models from 3ds files with Model3ds class.

The most simple use of Reader3ds in code is with its static Instance property which returns an static instance of the Reader3ds. So you do no need to create your own instance of the class to use its methods So you can write:

Ab3d.Reader3ds.Instance.ReadFile("sample_scene.3ds", myViewport);

This code uses the ReadFile(String, Viewport3D) that reads the sample_scene.3ds from the application folder and imports all the models, its materials and all the lights and adds them to myViewport. If there are any cameras defined in simple_scene.3ds the first camera is assigned to myViewport. myViewport can be defined in code:

Viewport3D myViewport = new Viewport3D();
dockPanel1.Children.Add(myViewport);
or in XAML:
XAML
<Viewport3D Name="myViewport">
</Viewport3D>

After the file has been read you can use all the public properties of the Reader3ds simply by accessing them with the Instance:

GeometryModel3D head = Ab3d.Reader3ds.Instance.NamedObjects["head"] as GeometryModel3D;

The upper code is using NamedObjects to get the 3D objects named as "head" and sets a variable head with its GeometyModel3D.

3ds file can also contain animation data - with Reader3ds it is possible to get the simple animation data from 3ds file. For more see GetFrame(Int32, Viewport3D)

Examples
For advanced use it is recommended to create an instance of Reader3ds - for example:
Ab3d.Reader3ds newReader3ds;
DiffuseMaterial defaultMaterial;

defaultMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Green));

newReader3ds = new Ab3d.Reader3ds();
newReader3ds.DefaultMaterial = defaultMaterial;
newReader3ds.TexturesPath = "c:\\textures";

newReader3ds.ReadFile("c:\\models\\buttons.3ds", myViewport);

Camera defaultCamera = newReader3ds.Cameras[0]; 
GeometryModel3D head = Ab3d.Reader3ds.Instance.NamedObjects["playButton"] as GeometryModel3D;

The last line in the previous example gets the object that has been named as "playButton". This can be very useful because in 3D modeling application a name can be set for each of the objects. With NamedObjects it is possible to get the Model3DGroup, GeometryModel3D, Light or Camera from the 3ds file. After this you can simply transform the object by using Transformer3ds. You can also get all the named objects with its hierarchy - see DumpNamedObjects

Transforming (Translating, Scaling, Rotating) the read objects can be greatly simplified with Ab3d.Transformer3ds class. Reader3ds provides a Transformer property to get the instance of Transformer3ds for the current Reader3ds. Read class description for more info. The following samples demonstrates Transformer3ds usage:

// Read robotarm.3ds into myViewport
Ab3d.Reader3ds.Instance.ReadFile("robotarm.3ds", myViewport);

// Scales all the read objects by 10% (by factor 1.1)
Ab3d.Reader3ds.Instance.Transformer.ScaleObject(1.1, 1.1, 1.1);

// Rotates the 3D object with name "BaseMotor" for 10 degrees around Y axis. 
// The rotation is additive (last parameter is true) - so the rotation does not replace the previous rotation but adds it to the previous rotation - useful for animation.
Ab3d.Reader3ds.Instance.Transformer.RotateObject("BaseMotor", new Vector3D(0, 1, 0), 10, true);
The following example shows how to use CompositionTarget.Rendering event for creating a frame-base animation with Reader3ds and Animator3ds.
void StartAnimation()
{
    CompositionTarget.Rendering += new EventHandler(CompositionTarget_RenderingHandler);

    Ab3d.Reader3ds.Instance.Animator.AnimationDuration = new TimeSpan(0, 0, 10); // 10 seconds
       Ab3d.Reader3ds.Instance.ReadFile("SampleAnimation.3ds", myViewport);
}

void EndAnimation()
{
    CompositionTarget.Rendering -= new EventHandler(CompositionTarget_RenderingHandler);
       Ab3d.Reader3ds.Instance.Animator.Stop();
}     

void CompositionTarget_Rendering(object sender, EventArgs e)
{
    Ab3d.Reader3ds.Instance.Animator.DoAnimate();
}
See Also