Newbie question on how to draw a custom texture

I have a simple question for which I do not seem to find the answer to.

I want to create a cube with a 16x16 pixel texture. I can load the texture with the asset manager. The material has a texture with contains an image which contains a byte buffer.

I want to draw my own texture in real time so I need a way to write in the image or the byte buffer of the image. So I can create my own textures from data in the game.

I need some type of method:

image.setPixelColor (int x, int y, Color color)

What is the best and most efficient way to do that?

Example code:


int width = 16;

int height = 16;

ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);

//(not sure if this is correct)

Image image = new Image(Format.BGR8 , width, height, buffer) ;

// Method to set the color of a pixel of the image




material.setTexture("m_DiffuseMap", texture);


You can use BufferUtils.createByteBuffer(data) instead of creating an empty buffer.

The format of the data would either be “r,g,b,r,g,b…”, or with 4 values per pixel if you use alpha.

If you want to specify the color of a specific pixel you would replace its values in the buffer and use image.setData(buffer);

You can find the position of the pixel in the buffer with (x + y * imageWidth) * 3 (or * 4)

Yeah, i got it to work.

For anyone who is interested I wrote a helper class

You can paint the texture by using setPixel(int x, int y, Color color)

Or setBackground(Color color)

And you get the new texture bij getTexture();



testMaterial = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");

PaintebleTexture paint = new PaintebleTexture(16,16);

paint.setBackground(new Color(0,0,0,0));

paint.setPixel(10, 10, new Color(0,0,255,255));

testMaterial.setTexture("m_DiffuseMap", paint.getTexture());

testMaterial.setFloat("m_Shininess", 128f); // [0,128]




The paintable texture class:


import java.awt.Color;

import java.nio.ByteBuffer;

import com.jme3.texture.Image;

import com.jme3.texture.Texture;

import com.jme3.texture.Image.Format;

import com.jme3.texture.Texture2D;

import com.jme3.util.BufferUtils;

public class PaintebleTexture {

protected byte[] data;

protected Image image;

protected Texture2D texture;

int width;

int height;

public PaintebleTexture(int width, int height) {

this.width = width;

this.height = height;

// create black image

data = new byte[width * height * 4];

setBackground (new Color(0,0,0,255));

// set data to texture

ByteBuffer buffer = BufferUtils.createByteBuffer(data);

image = new Image(Format.RGBA8, width, height, buffer);

texture = new Texture2D(image);



public Texture getTexture() {

ByteBuffer buffer = BufferUtils.createByteBuffer(data);


return texture;


public void setPixel(int x, int y, Color color) {

int i = (x + y * width) * 4;

data = (byte) color.getRed(); // r

data = (byte) color.getGreen(); // g

data = (byte) color.getBlue(); // b

data = (byte) color.getAlpha(); // a


public void setBackground(Color color) {

for (int i = 0; i < width * height * 4; i += 4) {

data = (byte) color.getRed(); // r

data = (byte) color.getGreen(); // g

data = (byte) color.getBlue(); // b

data = (byte) color.getAlpha(); // a



public void setMagFilter(MagFilter filter) {






Cool, I suggest adding the class here so it doesnt get buried in the forums :slight_smile:

Hi, I know this post is 3 years old. I tried this method today and its not working anymore. I changed the Color to ColorRGBA class and hoped thats it. Any suggestions whats wrong or missing to get this work with the current version of jme?