Programmatically stitching textures

I’ve been struggling with this issue for a few weeks and haven’t been able to find a solution to it. I’m creating a 2D game and one of the components needs a texture to be tiled but the ends should have a different texture. All of the tiling information I’ve been able to find is for uniform tiling without edge images. I’ve been attempting to stitch together the textures manually, but all I get is a black box. Below I’ve attached my attempt at making a valid texture, but I’m open to any suggestions on a better way to do this. I’m need to keep it to 1 materially that is projected onto a box.

The hope of the code below is to create a texture that is the 4 initial textures stacked in 2D space along the Y axis.

        Texture texture1 = loadTexture("end");
        Texture texture2 = loadTexture("side");
        Texture texture3 = loadTexture("side");
        Texture texture4 = loadTexture("end");

        Image image1 = texture1.getImage();
        Image image2 = texture2.getImage();
        Image image3 = texture3.getImage();
        Image image4 = texture4.getImage();

        ByteBuffer buffer1 = image1.getData(0);
        ByteBuffer buffer2 = image2.getData(0);
        ByteBuffer buffer3 = image3.getData(0);
        ByteBuffer buffer4 = image4.getData(0);

        int size1 = buffer1.capacity();
        int size2 = buffer2.capacity();
        int size3 = buffer3.capacity();
        int size4 = buffer4.capacity();

        int totalSize = size1 + size2 + size3 + size4;

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(totalSize)
                .put(buffer1)
                .put(buffer2)
                .put(buffer3)
                .put(buffer4);

        Image image = new Image(image1.getFormat(), image1.getWidth(), image1.getHeight() * 4, byteBuffer, image1.getColorSpace());
        Texture texture = new Texture2D(image);

In order to specify a texture-coordinate you must have a vertex placed there. I presume you are trying to make a tile-based system - in which case have you heard of tile bitmasking?

1 Like

I hadn’t heard of bitmasking, but after reading up on it, its exactly what I’m trying to accomplish. Doing a cursory look through the jME source code and forum, I’m not seeing how this implemented. Is there native functionality for bitmasking that I’m missing?

No.

You will have to implement it yourself… but all of the tools are there to do that.

Should anyone run across this post in the future, this is simple working example where images are appended in 1 dimension. Its a rough implementation, but should serve to get the idea across.

public static Image convertImage(List<Image> images) {
        Image standard = images.get(0);

        int width = standard.getWidth();
        int singleHeight = standard.getHeight();
        int height = standard.getHeight() * images.size();

        ByteBuffer data = BufferUtils.createByteBuffer( (int)Math.ceil(standard.getFormat().getBitsPerPixel() / 8.0) * width * height);
        Image convertedImage = new Image(standard.getFormat(), width, height, data, null, standard.getColorSpace());

        List<ImageRaster> sources = images.stream()
                .sequential()
                .map(ImageRaster::create)
                .collect(Collectors.toList());

        ImageRaster targetWriter = ImageRaster.create(convertedImage);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int imageIndex = y / singleHeight;
                int innerY = y % singleHeight;
                ColorRGBA color = sources.get(imageIndex).getPixel(x, innerY);
                targetWriter.setPixel(x, y, color);
            }
        }

        return convertedImage;
    }
1 Like

or…

public int calculateTileIndex(boolean above, boolean below, boolean left, boolean right) {

        int sum = 0;
        if (above) sum += 1;
        if (left)  sum += 2;
        if (below) sum += 4;
        if (right) sum += 8;
        return sum;
    }