[Request] RGB value at X,Y

Post your work in progress addons and get feedback

Post » Sat Jun 14, 2014 3:28 pm

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
B
6
S
2
Posts: 23
Reputation: 712

Post » Sat Jun 14, 2014 3:47 pm

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.
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842

Post » Sat Jun 14, 2014 3:54 pm

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.
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sat Jun 14, 2014 3:56 pm

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
B
6
S
2
Posts: 23
Reputation: 712

Post » Fri Jun 20, 2014 9:51 am

Rojo's Canvas plugin has Expression:redAt, Expression:greenAt, Expression:blueAt, even Expression:alphaAt.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Fri Jun 20, 2014 6:22 pm

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.
Moderator
B
94
S
33
G
33
Posts: 3,006
Reputation: 27,749

Post » Fri Jun 20, 2014 11:57 pm

@Arima

Agree, it will copy the image data again for each expression call.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747


Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 2 guests