Lukasz Madon
Lukasz Madon

Reputation: 15004

Transparency of ModelVisual3D with DiffuseMaterial

I come across a problem which seems a bug for me. I'm making an app that visualizes atoms in a crystal. That problem is that it draws a transparent object and hides the object behind.

enter image description here

Here is the code:

        foreach (var atom in filteredAtoms)
        {
            var color = new Color();

            color.ScR = (float)atom.AluminiumProbability;
            //color.G = 50;
            color.ScB = (float)atom.MagnesiumProbability;
            //setting alpha channel but Opacity doens't work as well
            color.ScA = (float)(1.0 - atom.VacancyProbability); //(float)1.0;//
            DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(color));
            //material.Brush.Opacity = 1.0 - atom.VacancyProbability;

            // make visuals and add them to
            atomBuldier.Add(new Point3D(atom.X * Atom.ToAngstrom, atom.Y * Atom.ToAngstrom, atom.Z * Atom.ToAngstrom), material);
        }

When I change the material to e.g. EmissiveMaterial there are no "cut" atoms. I googled this post, but the advices given don't apply to this case.

Is this a bug with 2D brush applied to 3D?

The full source code can be found here http://alloysvisualisation.codeplex.com the dll and a test file http://alloysvisualisation.codeplex.com/releases beta link.

Steps to reproduce:

  1. Lunch app
  2. Click Open file button
  3. Open test file (xyzT2000.chmc)
  4. Click Mask button
  5. Check 11 (series of atoms are almost transparent)
  6. Ckick Redraw

Upvotes: 6

Views: 4642

Answers (1)

Torious
Torious

Reputation: 3424

For the transparent atoms, you must disable z-buffer-writing. I'm unfamiliar with WPF, but you can probably set this in an Appearance or Material object or so.

The problem occurs because of the following:

When a transparent atom is rendered, it writes its depth to the z-buffer. Subsequent non-transparent atoms that are rendered and should appear, do not get written to the frame buffer, because their z-values fail the z-test, because of the z-values already in the z-buffer of the transparent atom.

In short, the graphics card treats the transparent atom as opaque and hides anything behind it.

Edit: Upon looking into WPF it seems pretty high-level, without direct control of z-buffer behavior.

According to this link, the emissive and specular materials do not write to the z-buffer, so using those is your solution when working with transparent objects.

Upvotes: 10

Related Questions