0 Favourites

[Request] RGB value at X,Y

  • Would absolutely love to be able to get the RGB colour info from a specific X,Y coordinate (either of a image/sprite or layer, whichever's easiest to implement!).

    I'd be using it to get the values of where one sprites origin is overlapping another image (thing heightmap).

    Will delve in to the murky art and have a go myself but it's probably super simple when you know what you're doing already

    Thanks in advance,

    Flibble

  • If at all possible, it's best to avoid this type of thing, since it is incredibly slow. The GPU may have a long queue of rendering commands in-progress, and normally it is working in parallel to the CPU and things are going super fast. However when you ask for a pixel back, the CPU has to synchronise: it will have to sit and wait while the GPU finishes its current queue, then when it's finished read a single pixel then send it back to the CPU. Then the GPU is sitting idle waiting for the CPU to finish whatever it's doing with the logic and get back to issuing rendering commands at the end of the tick before it can start doing any work again. So normally the CPU and GPU are both busy and working at the same time, but as soon as you start asking for pixels back they both end up doing a lot of waiting and doing nothing. For this reason reading back just a single pixel per frame alone can drop you to 20 FPS or less. On mobile, reading back a single pixel once-off could noticably jank the game. If there's any conceivable way you can do it differently, you should.

  • The way I've done it is snapshot to canvas then load the snapshot into a temp canvas then use.

    tempcanvas.getImageData(x,y,1,1).data

    Problem then becomes having the temp canvas matching the scaling / size & alignment of the c2 canvas otherwise the coordinates aren't accurate.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Ahh, ok thanks.

    To an outsider it seems like a really simple thing for a CPU to do but when you explain it it makes sense as to how much more it takes and how wrong I was

    Cheers for the response I'll go work around it!

    Thanks again

    Flib

  • Rojo's Canvas plugin has Expression:redAt, Expression:greenAt, Expression:blueAt, even Expression:alphaAt.

  • I found using those expressions in the canvas object can be slow, but by sticking the whole image into a json string via the canvas.asJSON expression, then getting the values from that, it is much faster.

  • Arima

    Agree, it will copy the image data again for each expression call.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)