Click or drag to resize
AB4D logo

ObjModelVisual3D Class

ObjModelVisual3D is a Visual3D class that shows 3D models that are read from obj file specified with Source property.
Inheritance Hierarchy

Namespace: Ab3d.Visuals
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.1.8864.1045
Syntax
C#
public class ObjModelVisual3D : BaseVisual3D, 
	IUriContext

The ObjModelVisual3D type exposes the following members.

Constructors
 NameDescription
Public methodObjModelVisual3D Constructor
Public methodObjModelVisual3D(String) Constructor
Top
Properties
 NameDescription
Public propertyDefaultBackMaterial Gets or sets the default material that is used for BackMaterial when the back material is not defined in obj file. Default value is null.
Public propertyDefaultMaterial Gets or sets the default material that is used when the material is not defined in obj file. Default value is a Silver DiffuseMaterial.
Public propertyPosition Gets or sets the Position of the read obj model. The type of position is determined by PositionType property.
Public propertyPositionType Gets or sets the ObjModelVisual3DVisualPositionType value that specifies the type of the Position
Public propertyCode examplePreserveScaleAspectRatio Gets or sets a Boolean that specifies if the 3D model is scaled so its aspect ratio is preserved (the ratio between width, height and depth of the object).
Public propertyCode exampleSizeX Gets or sets the size in of the 3D model in X dimension.
Public propertyCode exampleSizeY Gets or sets the size in of the 3D model in Y dimension.
Public propertyCode exampleSizeZ Gets or sets the size in of the 3D model in Z dimension.
Public propertySource Gets or sets the Source of the obj file
Public propertyTexturesPath Gets or sets the path where the textures are located. If null or "" the path of the obj 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")
Public propertyUsedReaderObj Gets a Ab3d.ReaderObj instance that is used to read the obj file.
Top
Methods
 NameDescription
Protected methodCreateModel Creates this Model3D
(Overrides BaseVisual3DCreateModel)
Public methodReload Forces a reload of the obj file
Protected methodSetModel Sets the Content of the Visual3D
Protected methodSetModelScale SetModelScale set the modelScale as ScaleTransform3D. It is used to scale the model. The method can be overridden to provide custom scaling.
Protected methodSetModelTranslate SetModelTranslate sets the modelTranslate as TranslateTransform3D. It is used to position the model. The method can be overridden to provide custom positioning.
Top
Fields
 NameDescription
Public fieldStatic memberDefaultBackMaterialProperty DefaultBackMaterialProperty
Public fieldStatic memberDefaultMaterialProperty DefaultMaterialProperty
Protected fieldmodelScale ScaleTransform3D that is used to scale the model
Protected fieldmodelTransformGroup Transform3DGroup that is used to scale and position the model
Protected fieldmodelTranslate TranslateTransform3D that is used to position the model
Public fieldStatic memberPositionProperty PositionProperty
Public fieldStatic memberPositionTypeProperty PositionTypeProperty
Public fieldStatic memberPreserveScaleAspectRatioProperty PreserveScaleAspectRatioProperty
Public fieldStatic memberSizeXProperty SizeXProperty
Public fieldStatic memberSizeYProperty SizeYProperty
Public fieldStatic memberSizeZProperty SizeZProperty
Public fieldStatic memberSourceProperty SourceProperty
Public fieldStatic memberTexturesPathProperty TexturesPathProperty
Top
Extension Methods
 NameDescription
Public Extension MethodDumpHierarchy Display details about the hierarchy of Visual3D children to Debug console (for example to Visual Studio's Immediate window).
(Defined by Extensions)
Public Extension MethodForEachGeometryModel3D Performs the specified action on each GeometryModel3D inside the rootModelVisual3D.
(Defined by Extensions)
Public Extension MethodForEachVisual3D Performs the specified action on each ModelVisual3D inside the rootModelVisual3D.
(Defined by Extensions)
Public Extension MethodGetName Gets name of the Visual3D that was previously set by SetName extension method.
(Defined by Extensions)
Public Extension MethodSetName Sets Name property to Visual3D. Note that if name is not correct for WPF, it can be corrected (name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores). In this case the SetName method will set the corrected name to the object and return false (in this case you can call GetName to get the corrected name). True is returned when the original name is set to the object.
(Defined by Extensions)
Top
Remarks

ObjModelVisual3D can be used in XAML to show 3D models that are read from obj file.

The source of the obj file is specified with Source property.

The read 3D model is positioned according to the Position and PositionType properties

If the PositionType is set to Center (default), the 3D model will be position so that its center will be at the Position coordinates. If the PositionType is set to BottomCenter, the 3D model will be positioned above the Position coordinates.

The size of the shown object is controlled by the SizeX, SizeY, SizeZ and PreserveScaleAspectRatio properties.

By default all the SizeX, SizeY and SizeZ are set to -1. This means that the original size of the object is used. But if the SizeX is set to let's say 100, the object would be scaled so its SizeX would be 100.

If PreserveScaleAspectRatio is true (default), than the aspect ratio of the 3D model is preserved. This means that the model is stretched to one side mode than to the other - the scale is evenly set to all the axis. This also means that if all SizeX, SizeY and SizeZ are defined, the object will be scaled so it will not exceed and of the specified sizes.

If PreserveScaleAspectRatio is false, than the aspect ration of the 3D model will not be preserved. In this case the SizeX, SizeY and SizeZ will be applied so the object will be exactly the size of the specified sizes.

Example

Before using ObjModelVisual3D the following namespace declaration has to be added to the root xaml element:

C#
xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"

Now the ObjModelVisual3D can be used in XAML. The following example shown a 3D model from ab3d.obj file. The model's center is positioned at (-250, 0, 0) and its size is set so its SizeX is 80:

XAML
<visuals:ObjModelVisual3D Source="/Resources/ab3d.obj" 
         SizeX="80"
         Position="0 40 0" PositionType="Center"/>

The following code shows the same mode, but this time it is custom sized:

XAML
<visuals:ObjModelVisual3D Source="/Resources/ab3d.obj" 
         SizeX="50" SizeY="30" SizeZ="40"
         PreserveScaleAspectRatio="False"
         Position="0 40 0" PositionType="Center"/>

The following code shows how to use ObjModelVisual3D in code behine:

C#
var objModelVisual = new Ab3d.Visuals.ObjModelVisual3D()
{
    Source = new Uri(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/myModel.obj"), UriKind.Absolute),
    DefaultMaterial = new DiffuseMaterial(Brushes.Silver),
    Position = new Point3D(0, 0, 0),
    PositionType = Visuals.ObjModelVisual3D.VisualPositionType.BottomCenter,
    SizeX = 100
};

MainViewport3D.Children.Add(objModelVisual);
See Also