create 3d texture on the fly

I need to create a 3d texture on the fly.

    def createHalfToneTexture(self,depth,width,height):
        tex = Texture()
        tex.setupTexture(Texture.TT2dTexture, width,height,depth, Texture.TUnsignedByte, Texture.FLuminance);
        p = tex.modifyRamImage()
        for z in range(depth):
            offset = width * height * z
            for y in range(height):
                for x in range(width):
                    index = (offset + width * y + x)
                    p.setElement(index, somevalue)
        return tex

If I create a 2d texture using above code (set depth = 1), the 2d texture is correctly created.

But it seems not working for creating 3d texture (set depth > 1). How can I fix it ?

Use tex.setup3dTexture instead.

Then how can I setup the other parameters ? (the other 2 dimensions, format and type ?) Since I am not reading it from a file.

Something like this should work:

tex.setup3dTexture(width,height,depth, Texture.TUnsignedByte, Texture.FLuminance)

Note that you can use the generic setupTexture() call as well, but you have to change the first parameter from Texture.TT2dTexture to Texture.TT3dTexture (in addition to setting a depth != 1).

But, yeah, you should probably use setup3dTexture instead. That’s what it’s for.


I do not realize I put Texture.TT2dTexture instead of Texture.TT3dTexture, stupid me. I will fix and test again. Thank you.