I u开发者_开发问答se VS 2008 C# Express. I want to change the opacity value of a 3D object in window that has a lot of 3D objects. Changing process will be done by a code-behind.
Could you explain me how it is done.
Thanks
B.Joe
Assuming your 3D object is a Model3DGroup
or GeometryModel3D
within a ModelVisual3D
or ModelUIElement3D
, changing the opacity is a matter of iterating the individual GeometryModel3D
s within it and updating each one's Material
and BackMaterial
, something along these lines:
public void SetOpacity(Model3D model, double opacity)
{
var modelGroup = model as Model3DGroup;
var geoModel = model as GeometryModel3D;
if(modelGroup!=null)
foreach(var submodel in modelGroup.Children)
SetOpacity(submodel, opacity);
if(geoModel!=null)
{
geoModel.Material = SetOpacity(geoModel.Material, opacity);
geoModel.BackMaterial = SetOpacity(geoModel.BackMaterial, opacity);
}
}
public Brush SetOpacity(Brush brush, double opacity)
{
if(!GetIsOpacityControlBrush(brush)) // Use attached property to mark brush
{
brush = new VisualBrush
{
Visual = new Rectangle { Fill = brush, ... };
};
SetIsOpacityControlBrush(brush, true);
}
((Rectangle)((VisualBrush)brush).Visual).Opacity = opacity;
}
You will need to iterate through all the GeometryModel3D and ViewPort2DVisual3D in your object. For each GeometryModel3D, update the Material to a new opacity, using a VisualBrush if necessary. For each ViewPort2DVisual3D, simply set the Opacity
If your 3D object is a Visual3D such as a ContainerUIElement3D then you will have to first iterated down to the individual ModelVisual3D and ModelUIElement3D to get to the models comprising it. Also, if you encounter an ViewPort2DVisual3D you can set the opacity on the contained Visual directly.
You can manipulate the opacity of the material in terms of the brush it contains.
精彩评论