tags:

views:

41

answers:

3

Hi All,

I use 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

A: 

(Sorry due to this writing way. Unfortunately, there is no "comment" button on my window. I don't know what the reason is.)

Yes, I use WPF. The object is 3D cube.

B.Joe
You should be able to edit your own post. Look for an "edit" at the lower left hand side instead of answering this questions.
CrimsonX
"edit" and the others are just on the window, that is, after sending the question :-)
B.Joe
Addition to my question : The opacity value of the 3D object must change after clicking on the object by mouse.
B.Joe
You have two different accounts with the same name
SLaks
+1  A: 

You can manipulate the opacity of the material in terms of the brush it contains.

Zyon
This is the right idea. You must make sure you manipulate both the Material and BackMaterial and deal with updating the actual material. I've added an answer that shows how to do this.
Ray Burns
A: 

Assuming your 3D object is a Model3DGroup or GeometryModel3D within a ModelVisual3D or ModelUIElement3D, changing the opacity is a matter of iterating the individual GeometryModel3Ds 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.

Ray Burns