Search code examples
javalibgdx

How to map TextureRegion into Pixmap correctly?


I am trying to make my texture region into a pixmap but by following the prepared method it is copying the whole atlas into the pixmap , so I am suggested to loop each pixel and map it into another pixmap manually

    Pixmap emptyPixmap = new Pixmap(trLine.getRegionWidth(), trLine.getRegionHeight(), Pixmap.Format.RGBA8888);

    Texture texture = trLine.getTexture();
    texture.getTextureData().prepare();
    Pixmap pixmap = texture.getTextureData().consumePixmap();

    for (int x = 0; x < trLine.getRegionWidth(); x++) {
        for (int y = 0; y < trLine.getRegionHeight(); y++) {
            int colorInt = pixmap.getPixel(trLine.getRegionX() + x, trLine.getRegionY() + y);
            emptyPixmap.drawPixel( trLine.getRegionX() + x , trLine.getRegionY() + y , colorInt );
        }
    }

    trBlendedLine=new Texture(emptyPixmap);

But the texture generated is drawing nothing , which means the getPixel is not getting the correct pixel. Please advice.


Solution

  • You are drawing your pixel outside of the pixmap by using the trLine.getRegionX() + x and trLine.getRegionY() + y . What you should have instead is:

    for (int x = 0; x < trLine.getRegionWidth(); x++) {
        for (int y = 0; y < trLine.getRegionHeight(); y++) {
            int colorInt = pixmap.getPixel(trLine.getRegionX() + x, trLine.getRegionY() + y);
            emptyPixmap.drawPixel(x , y , colorInt );
        }
    }