Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VertexColorMaterial transparency not blending properly
#1
When an alpha value is applied to colors in SharpEngine's standard VertexColorMaterial it seems to blend wrongly. 

You can see this for yourself by adjusting line 346 in Ab4d.SharpEngine.Samples.Common/Advanced/MultiSceneNodeExample.cs

Is there an option I'm missing to change the blend mode?
#2
Hm, the MultiSceneNodeSample shows that using alpha values smaller than one work correctly. See screenshot:

   

The marked box is created in line 396 (all the colors have alpha value set to 0.3 a few lines above).
The screenshot shows that the objects correctly visible through this box.

Also, is the alpha value in line 364 is changed, this also works correctly.


The blending mode (AlphaBlend or Opaque) and the RenderingLayer (transparent objects are put to Transparent RenderingLayer so they are rendered after all other objects and are also sorted by their distance to the camera) are controlled by the VertexColorMaterial.HasTransparency property.

When the VertexColorMaterial is created by passing an array of colors to the constructor, then the value of  HasTransparency property is automatically calculated by checking individual colors (for alpha < 1). To prevent this check (improve performance) you can also use constructor that also takes hasTransparency parameter.

You can also manually change the value of HasTransparency property.

If you first initialize the VertexColorMaterial with only opaque colors (alpha == 1) and later change the alpha values, then you need to call UpdatePositionColors method for the changes to take effect. If you have not manually set the HasTransparency, then the colors are checked again for any transparent color and  HasTransparency property value is updated.


Anyway, if you have a case when this does not work correctly, please provide some additional information on how to reproduce that and what is expected.
Andrej Benedik
#3
Apologies for the bad example. Here is a better one (see attached zip). This example demonstrates something wrong with the blending of vertex colours. After running the demo, see lines 79-87 in MainWindow.axaml.cs

The problem seems to occur when the vertex colour alpha is less than one and the rgb values are not black.

   

   

Also in the screenshot you attached in your previous post, half of the circle is missing behind the rectangle. This is an instance of the transparency sorting issues which we emailed you about.

Good luck tracking down these bugs and keep up the great work!
#4
First, I am sorry for the late reply. It seems that there are some problems with the emails from the forum - I get some of them, but not others.

After checking your demo app, I can say that the VertexColorMaterial is correctly rendered.

The problem is that the colors for the VertexColorMaterial should be alpha-premultiplied. This means that if you take a Color3 with red, green and blue and add an alpha value to create Color4, you need to multiply all the color components with alpha: Color4(red * alpha, green * alpha, blue * alpha, alpha).

I guess that in your sample you wanted to use red colors with increasing transparency. You should use the following:
vertColors[idx] = new Color4(thing, 0, 0, thing);

So, a red color (1,0,0) that is premultiplied by the alpha value (thing in your sample) = Color4(thing, 0, 0, thing)

Instead you used "new Color4(128, thing, thing, thing)".
Also note that the valid values for the color components in Color4 are in range from 0 to 1 and not from 0 to 255 (those are float values and not byte values; you can use FromByteRgba static method to create Color4 with byte values).

When such value (128,0.5,0.5,0.5) is alpha-blended with the exiting value, the alpha value is multiplied by 128 and added to the existing color multiplied by 1-alpha. This almost always gives the value of the red component bigger than 1 - so you alost always get fully red color. This is also seen in your demo.


I will improve the documentation and sample to provide information about using alpha pre-multiplied color values. I will also consider adding a property that will define if the colors are alpha pre-multiplied.

I am sorry that this was not more clear.


About your comment:
"Also in the screenshot you attached in your previous post, half of the circle is missing behind the rectangle."

If you look closely enough, you will see that everything is correctly rendered. When the colors of the box with vertex color material and the sphere are blended, the color is almost the same as the background color, but on the right side it is possible to see the edge of the sphere.

If you start the sample and rotate the camera, you easily get the proof that everything is correct:
   


PS: I have removed your attachment with the demo app because the source had your license key. Please send such code by email or remove the license.
Andrej Benedik
  


Forum Jump:


Users browsing this thread:
1 Guest(s)