How to enable transparency in 3d Model rendering?

I am loading a model using the ofxAssimpModelLoader addon, and I am using drawFaces() call of the API to daw the model. the entire model is drawn opaque.
some parts of the model are transparent but they are still rendered opaque, how do I enable transparency?

not sure if this is your issue, but there is ofEnableAlphaBlending()

could also be that the transparency is lost when your model is loaded.
you should make sure that your model format supports transparency when loaded with assimp/ofxAssimpModelLoader.

Yes I agree that ofEnableAlphaBlending() is something to look at. I’m not sure, but alpha blending may be not compatible with ofEnableDepthTest().

Also, don’t forget to use colors that have an alpha channel specified! You can either make a color with the alpha channel specified, or change the alpha for an existing color:

ofColor color = ofColor(255, 0, 0, 127); // 50% red
color.a = 255; // color is now 100% opaque

If the transparency is getting lost when loading the model, you can always loop thru the colors in the ofMesh and set new values for each color:

// for an ofMesh called mesh
    for(size_t i{0}; i < mesh.getColors().size(); ++i)
        ofColor color = mesh.getColor(i);
        color.a = 20; // set a new alpha from 0 - 255
        mesh.setColor(i, color);

Transparency and depth sorting is not a trivial issue. I have run into some problems before and there are quite some mentions of it in this forum. I don’t know if there are definitive fixes for this in OF, however I did try to read some things that put it into perspective, like this:


I had some issues as well figuring out transparency for my workflow, which is using blender to create fbx models. I give you a quick overview of what I did, it might help you or someone else out. Below I attach an example project based on my findings.

In Blender I set the model as transparent and give an alpha value as in the image below.

On the coding side I inheritated the ofxAssimpModelLoader class to in order to have access to the internals.

After loading an object the meshes are checked if they have alpha < 1.0.
If this is the case, the mesh blend mode is set to OF_BLENDMODE_ADD, in order to allow transparency. Maybe this is the issue you encounter in your workflow as well, for me it didn’t work out of the box between Blender and OF.

When rendering, it is important to render solid objects first and transparent objects afterwards. If you don’t do it in this order, the solid objects behind transparent objects are not drawn.
This part is quite tricky, in theory you would have to render every vertice in the correct order regarding its distance to the camera, but you might be able to find some work-arounds depending on your project to make this easier.

Make sure to also check that the meshes inside your model are in the correct order, or a transparent mesh may hide another mesh as well. (141,3 KB)

1 Like