How do I alpha test a given color in an image to make it transparent?

Forums: 

On the 2D layer, I tried Sample2D_Image and AIS_TexturedShape and set their AlphaCutoff, but nothing happened. Here is my code:

    // create the textured shape
    Handle(AIS_TexturedShape) myTexturedShape = new AIS_TexturedShape(MakeIconShape(size, size));

    // set the image
    Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
    if (!anImage->Load(file_name))return false;
    myTexturedShape->SetTexturePixMap(anImage);

    // set the AlphaCutoff
    Handle(Prs3d_Drawer) hDrawer = myTexturedShape->Attributes();
    hDrawer->ShadingAspect()->Aspect()->SetAlphaMode(Graphic3d_AlphaMode_Mask, .3);

    // Place it on a 2D layer to display
    myTexturedShape->SetZLayer(Graphic3d_ZLayerId_TopOSD);
    Graphic3d_Vec2i offset(margin_left, size+maring_top);
    myAISContext->SetTransformPersistence(myTexturedShape, new Graphic3d_TransformPers(Graphic3d_TMF_2d, Aspect_TOTP_LEFT_UPPER, offset));

    // Update the display
    myAISContext->Display(myTexturedShape, Standard_True);

The MakeIconShape involved in the above code is as follows:

TopoDS_Face MakeIconShape(int width, int height)
{
    TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0, 0, 0.), gp_Pnt(width, 0, 0.));
    TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge(gp_Pnt(width, 0, 0.), gp_Pnt(width, height, 0.));
    TopoDS_Edge E3 = BRepBuilderAPI_MakeEdge(gp_Pnt(width, height, 0.), gp_Pnt(0, height, 0.));
    TopoDS_Edge E4 = BRepBuilderAPI_MakeEdge(gp_Pnt(0, height, 0.), gp_Pnt(0, 0, 0.));
    TopoDS_Wire anImageBounds = BRepBuilderAPI_MakeWire(E1, E2, E3, E4);
    TopoDS_Face result = BRepBuilderAPI_MakeFace(gp_Pln(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), anImageBounds);

    return result;
}

Attached is a PNG picture with transparent color

Attachments: 
gkv311 n's picture

You are changing properties of undefined aspect here - it will crash with NULL-dereference within OCCT 7.7.0 (in older versions it would lead to undefined results).

    // set the AlphaCutoff
    Handle(Prs3d_Drawer) hDrawer = myTexturedShape->Attributes();
    hDrawer->ShadingAspect()->Aspect()->SetAlphaMode(Graphic3d_AlphaMode_Mask, .3);

And it is better not using obsolete class AIS_TexturedShape, and use AIS_Shape instead.

liuzhongtu's picture

Thank you, just add this statement and the alpha test will be normal:

hDrawer->SetShadingAspect(new Prs3d_ShadingAspect());