private void SetModelTransparency(double transparency)
{
foreach (var modelItem in DrawingControl.Viewport.Children.OfType<ModelVisual3D>())
{
SetTransparency(modelItem.Content, transparency);
}
}
private void SetTransparency(Model3D model, double transparency)
{
if (model is GeometryModel3D geometryModel)
{
var newMaterialGroup = new MaterialGroup();
ApplyTransparencyToMaterial(geometryModel.Material, transparency, newMaterialGroup);
geometryModel.Material = newMaterialGroup;
}
else if (model is Model3DGroup modelGroup)
{
foreach (var child in modelGroup.Children)
{
SetTransparency(child, transparency);
}
}
}
private void ApplyTransparencyToMaterial(Material material, double transparency, MaterialGroup newMaterialGroup)
{
if (material is MaterialGroup materialGroup)
{
foreach (var subMaterial in materialGroup.Children)
{
ApplyTransparencyToMaterial(subMaterial, transparency, newMaterialGroup);
}
}
else if (material is DiffuseMaterial diffuseMaterial)
{
var color = ((SolidColorBrush)diffuseMaterial.Brush).Color;
color.A = (byte)(transparency * 255);
/*diffuseMaterial.Brush = new SolidColorBrush(color)
{
Opacity = transparency
};*/
var newDiffuseMaterial = new DiffuseMaterial(new SolidColorBrush(color));
newMaterialGroup.Children.Add(newDiffuseMaterial);
}
else
{
newMaterialGroup.Children.Add(material.Clone());
}
}
I use XbimWindowsUI library to render the IFC file. The above code transparents the x,y grid lines but not transparenting the models. I tried with DrawingControl.Opacity = e.NewValue();
but it’s also not working. Is there a way to work around?