Search code examples
libgdxtextures

MeshPartBuilder transparent TextureRegions


I'm drawing ModelInstances in a ModelBatch with LibGDX, but the textures of the models are deformed and their transparent pixels are opaque. I'm using the texture packer like this to produce an atlas. I then assign the TextureRegion from the atlas to a Material in order to create the MeshPartBuilder:

GameModels() {
    int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;

    TextureRegion texture = textureAtlas.findRegion("cactus");

    modelBuilder.begin();
    MeshPartBuilder meshPartBuilder = modelBuilder.part("box", GL20.GL_TRIANGLES, attr,
            new Material(TextureAttribute.createDiffuse(texture),
                    ColorAttribute.createDiffuse(ItemType.CACTUS.color)));

    BoxShapeBuilder.build(meshPartBuilder, 1f,1f,1f);

    cubeModel = modelBuilder.end();
}

But the rendering from the created Model produces this: A cropped and opaque texture

I want to produce a rendering of the texture that respects the texture and is transparent, like the second image from the second link. How can I achieve that ?


Solution

  • By adding a BlendingAttribute to the Material the texture supports now the transparency from the TextureRegion.

    BlendingAttribute blendingAttribute = new BlendingAttribute();
    blendingAttribute.opacity = 1f;
    
    ...
    
    MeshPartBuilder meshPartBuilder = modelBuilder.part("box", GL20.GL_TRIANGLES, attr,
                new Material(textureAttribute, blendingAttribute));
    

    Corrected rendering of the texture with blending