Trying out the new RenderTarget:getPixel(). It's really slow, even with a RenderTarget of 1x1 pixel. I seem to remember reading this is expected, but can't find it in the forum.
Is there an efficient way of getting the colour of a pixel nowadays?
Yes, it is slow because texture data is stored in GPU memory, and is not directly accessible by CPU. What is your use case ? If you need to access Bitmap data, that is something that shouldn't change, then you get grab all pixels once in a lua array (with RenderTarget:getPixels()) and look them up at will afterwise.
My use-case isn't entirely straightforward, but since you asked!
1. Load tilemap 2. Merge all passive tiles into one background layer 3. Get pixel from background layer to determine which sound to play when walking on surface
You should pre-calculate these and have an array the same as the map with the tile detail data.
Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!). https://deluxepixel.com
local _backgroundcolor=0function colorPicker(x,y)ifnot rtColorPicker then rtColorPicker=RenderTarget.new(1,1)end
rtColorPicker:clear(_backgroundcolor)
rtColorPicker:draw(stage,-x,-y)local col=rtColorPicker:getPixel(0,0)return col
end
Is there a way to do this without writing the file back? I currently have a red Samurai in a file, and want to make him green. I have the following code based on the accepted answer above and the example in Github gmedia
An easier way that doesn't require media would work, too. The reason I need bitmaps at the end is that I actually have a Samurai animation, and I add them to a MovieClip
local media = Media.new("images/Samurai_02.png")for x =0, media:getWidth()dofor y =0, media:getHeight()dolocal r,g,b,a = media:getPixel(x,y)
media:setPixel(x, y, g, r, b, a)-- note order change, switch red and greenendend
media:save()
path = media:getPath()local greenSamurai = Bitmap.new(Texture.new(path, true))
stage:addChild(greenSamurai)
Yeah, i wasn't specific enough. I have an existing asset with red armor, and I just want the armor to turn green. I tried ColorTransform first. Problem was the face turned green, too, and I couldn't figure out how not make him look sick. Sword, etc all turn green, too.
My current heuristic is slightly more complicated, I don't change all colors, only strong reds (r>120, b<120). I can create a little separate lua project that generates all these assets and then copy them over, but it inflates the assets directory. Plan B.
I could learn how to use shaders... Any good pointers on where to start? Maybe this is Plan A.
PS: I don't have access to the original art files to recreate, and it's a pain to change 80 frames for multiple characters. Would be better to start from scratch, which I might eventually, but it's a bit of a cost issue, as I'd outsource it. That is plan C. Would still be neat to change uniform color on the fly, instead of duplicating raster assets.
@MoKaLux 's idea is the most natural but if you already have your assets fixed then a simple shader will be easier to use. try out the blur shader example in gideros and change the fShader.glsl to:
i think from this you will get how it works. note that the blur example has an input fRad which you can modify so that there you can input which color you want to replace to etc. also you don't need complicated range checks as i did, only equality. in the provided texture no single color appeared many times, so i had to do it this way so that you see what happens.
Wow, thanks @keszegh that is helping a lot. I see it work, so this is a reasonable way to go, and it's only a few lines of code I can play with. Time to read up on shaders.
After spending some time on better and better heuristics, I don't think it's the way to solve my problem. But thanks for usual incredibly quick turn around on my question and the shader example code.
(My source full res artwork is high resolution and cartoon style with only a few actual colors. But reducing the resolution increases colors. If I swizzle the reduced size asset I get artifacts. I can do even better heuristics, but it's pretty much on a per asset base. I achieve much better results swizzling the few colors on the original asset and pre-compute new assets. Plan B it is. Until I splurge on new artwork. At that point, I could do plan D. I would have to do the animations on the fly, then, instead of precomputing. That seems like a lot more involved. At that point I probably want to go to 3D, too :-))
Comments
1) using media plugin:
http://giderosmobile.com/forum/discussion/749/gideros-getpixel/p1
Likes: totebo
Is there an efficient way of getting the colour of a pixel nowadays?
If you need to access Bitmap data, that is something that shouldn't change, then you get grab all pixels once in a lua array (with RenderTarget:getPixels()) and look them up at will afterwise.
1. Load tilemap
2. Merge all passive tiles into one background layer
3. Get pixel from background layer to determine which sound to play when walking on surface
https://deluxepixel.com
Likes: SinisterSoft, MoKaLux
i guess shaders would also work (i want to morph/warp the image). any other method?
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: antix, SinisterSoft
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Apollo14
Fragmenter - animated loop machine and IKONOMIKON - the memory game
An easier way that doesn't require media would work, too. The reason I need bitmaps at the end is that I actually have a Samurai animation, and I add them to a MovieClip
My current heuristic is slightly more complicated, I don't change all colors, only strong reds (r>120, b<120). I can create a little separate lua project that generates all these assets and then copy them over, but it inflates the assets directory. Plan B.
I could learn how to use shaders... Any good pointers on where to start? Maybe this is Plan A.
PS: I don't have access to the original art files to recreate, and it's a pain to change 80 frames for multiple characters. Would be better to start from scratch, which I might eventually, but it's a bit of a cost issue, as I'd outsource it. That is plan C. Would still be neat to change uniform color on the fly, instead of duplicating raster assets.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
The following is helpful to play around with colors: https://thebookofshaders.com/06/
After spending some time on better and better heuristics, I don't think it's the way to solve my problem. But thanks for usual incredibly quick turn around on my question and the shader example code.
(My source full res artwork is high resolution and cartoon style with only a few actual colors. But reducing the resolution increases colors. If I swizzle the reduced size asset I get artifacts. I can do even better heuristics, but it's pretty much on a per asset base. I achieve much better results swizzling the few colors on the original asset and pre-compute new assets. Plan B it is. Until I splurge on new artwork. At that point, I could do plan D. I would have to do the animations on the fly, then, instead of precomputing. That seems like a lot more involved. At that point I probably want to go to 3D, too :-))
Likes: SinisterSoft