Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency of ModelVisual3D with DiffuseMaterial

Tags:

c#

wpf

3d

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
like image 338
Lukasz Madon Avatar asked Apr 13 '12 23:04

Lukasz Madon


1 Answers

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.

like image 138
Torious Avatar answered Sep 26 '22 12:09

Torious