Jump to content

Get pixel value in a Sprite?


tackle
 Share

Recommended Posts

I have a sprite with a PNG enemy badguy in it. PNG has transparancy.

I want to track whether or not a certain x/y position inside of this Sprite is transparant or not.

 

Is there a way to get the pixel value of a Sprite?

 

I noticed in earlier threads there was some talk about using the Canvas Context 'getImageData' however that was perhaps not very smart performance-wise. Also the problem I would face is that this Sprite will be overlaying something that isn't transparant.

 

This is all regarding Canvas mode btw.

Link to comment
Share on other sites

yeah, any time you read from the GPU (like getImageData does), it potentially creates a pipeline stall (almost certainly, according to the chrome engineers - if you google on 'chrome game performance' or the like you can find the talks and/or papers where they talk about this)

 

so if you do this (read from GPU), you want to do it as the very first thing in the render pipeline. even then it could cause performance to degrade. but you're better off trying it and testing than assuming the worst - it may not matter for you.

 

if it does turn out to be unacceptable, you're left trying to track this information yourself. you could create a map of transparent pixels in memory when you first load the image and then check the sprite's current location against that each frame. depending on how many pixels you have to check this may end up being just as slow or slower than hitting GPU memory...

 

good luck!

Link to comment
Share on other sites

  • 3 months later...

Ok, this is a bit rough & ready, but I've pulled it from something that is working... Not sure yet about performance.

var bmd = game.make.bitmapData(100, 100);bmd.draw(game.cache.getImage('yourImage'), 0, 0);bmd.update();var sprite = game.add.sprite(0, 0, bmd);// in your onclick fnvar posX, posY;posX = pointer.x - sprite.x;posY = pointer.y - sprite.y;var hex = bmd.getPixel32(posX,posY);//r = ( hex       ) & 0xFF; // get the r//g = ( hex >>  8 ) & 0xFF; // get the g//b = ( hex >> 16 ) & 0xFF; // get the ba = ( hex >> 24 ) & 0xFF; // get the alphaif (a > 0) {// hit here!}
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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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