I would like to change color of 3D model loaded from .stl file.
Problem is that it is loaded as Model3DGroup. Model3DGroup has no property material which allows to change color. I tried to convert Model3DGroup to GeometryModel3D (it has material property). Geometry is empty after conversion and viewport is showing nothing.
What can I improve in this code to be able to change color of model from stl file? Maybe is there another way to make it?
I found simillar issue here also without solution, so maybe this time someone could help. Helix 3d-toolkit Displayed .STL model color change
XAML:
<helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="True">
<helix:DefaultLights/>
<ModelVisual3D x:Name="ViewPortModel" Content="{Binding Actual3dModel}"/>
</helix:HelixViewport3D>
C#:
private Model3DGroup actual3dModel;
public Model3DGroup Actual3dModel
{
get {
return actual3dModel;
}
set
{
actual3dModel = value;
OnPropertyChanged("Actual3dModel");
}
}
private void LoadModel()
{
var importer = new HelixToolkit.Wpf.ModelImporter();
var model = importer.Load("Assets/Model.stl");
var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;
var outsideMaterial = MaterialHelper.CreateMaterial(Colors.Red, 0.1);
var insideMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
var modelGroup = new Model3DGroup();
modelGroup.Children.Add(new GeometryModel3D { Geometry = m0, Material = outsideMaterial, BackMaterial = insideMaterial });
Actual3dModel = modelGroup;
}
Tried also in this way (again empty model is shown).
private void LoadModel2()
{
this.dispatcher = Dispatcher.CurrentDispatcher;
var loader = new ModelImporter();
var model = loader.Load("Assets/Model.stl", this.dispatcher);
GeometryModel3D gmodel = model.Children[0] as GeometryModel3D;
DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
gmodel.Material = material;
gmodel.BackMaterial = material;
var modelGroup = new Model3DGroup();
modelGroup.Children.Add(gmodel);
Actual3dModel = modelGroup;
}
CodePudding user response:
Model3DGroup
contains a group of GeometryModel3D
. Material property is in GeometryModel3D
. You need to change GeometryModel3D.Material
.
CodePudding user response:
I've found solution.
I dont know why but changing model.Children[0]
to model.Children[1]
solved issue.
Now model is shown and material color is changed.