Jump to content

Get access to pixel color from a texture ?


Recommended Posts

Hello, I'm trying to get used to Pixi. I'm pretty impressed by the library ! However, I'm having a hard time knowing the extent of everything it has to offer.

I'm trying to store a map in a single picture (with a tilemap system behind it ; each pixel stores a tile number through its color). But to use this, I would need to be able to read an image pixel by pixel, so that I can get the pixel colors and build my map through that. I've been browsing the documentation, but couldn't find any function doing that. So is there any way to get per-pixel details from a loaded texture ?


Thank you in advance for your help !

Link to comment
Share on other sites

Yes, I know the canvas can get me access to the displayed pixels, but this is not what I'm trying to do. I just want to load an image in memory, get its pixel, and be done with it - the image in itself is just a map, like a config file if you want. So I don't want to display it. 


I found an idea in Stack Overflow, though, which would give me the following implementation :


function MapLoader(source, context) {    var texture = context.createTexture();    texture.image = new Image();    texture.image.onload = function() {        var framebuffer = context.createFramebuffer();        context.bindFramebuffer(context.FRAMEBUFFER, framebuffer);        context.framebufferTexture2D(context.FRAMEBUFFER, context.COLOR_ATTACHMENT0, context.TEXTURE_2D, texture, 0);        var data = new Uint8Array(texture.image.width * texture.image.height * 4);        context.readPixels(0,0,texture.image.width,texture.image.height,context.RGBA, context.UNSIGNED_BYTE, data);        context.deleteFramebuffer(framebuffer);    };    texture.image.src = source;}

But oddly enough, this gives me the following error (on Chrome) :

[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glFramebufferTexture2D:



Firefox also throws an error ("Incomplete framebuffer"). I get my context directly from the renderer object of Pixie (its gl property). So, I'm still stuck... I guess I can do without the map stored in an image, but I find it much more practical than dealing with a JSON.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...