Take any image that has transparent pixels in it and try to save it as a png using renderTexture. The transparent pixels will get added with the background color of the renderTexture despite setting the background’s alpha channel to zero with clear(0,0,0,0). Below is the code I used:
auto sprite = Sprite::create("original.png");
auto renderTexture = RenderTexture::create(64, 64, Texture2D::PixelFormat::RGBA8888);
renderTexture->clear(0,0,0,0);
renderTexture->begin();
sprite->setAnchorPoint(Vec2(0, 0));
sprite->setPosition(Vec2(0,0));
sprite->visit();
renderTexture->end();
renderTexture->saveToFile("rendertexture.png", Image::Format::PNG);
So in the above case because I cleared with rgba(0,0,0,0), my transparent pixels will end up being darker. One would assume that having an alpha of zero shouldn’t change the image at all.
Here is an example of the effect. The first image is my original, a square with the light green color rbga(55,255,0,100). The second image is the renderTexture image I get after running the above code. You can see it is now a dark green color rgba(21,100,0,100).
img->setBlendFunc(BlendFunc::DISABLE); as you see.
But anyway result is(notice left side and bottom with some grayscale line):
So, I’m trying to slice a sprite and get new part as file.
As it was reported - some kind of grayscale pixels around rendered image. It’s not should be there. It’s a bug. Can someone from cocos2d-x developers comment this and help me? Thank you in advance.
My workaround: do not use ClippingNode and RenderTexture (bugs, performance issues). Use some image library (opencv in my case), keep image in memory, transform in memory. Use Texture2D::initWithData to display
Hello? Anyone? It seems that only after @slackmoehrle asks someone like @zhangxm or @drelaptop who as I now understand a cocos2d-x developers, only after that they arrive here. Otherwise not response at all. Thats sad for me, I’m developing my game with this engine, but now I realize that there are not help. Is this will be changed? Please.
For anyone facing this issue, I’ve just created a pull request with changes to Cocos2d-x to allow users to save files with non-premultiplied alpha. It works by reversing the PMA, so it’ll be pretty close to the original image, but there’s no way to actually get perfect RGB values since some information is lost in the initial PMA process.
The new method is RenderTexture::saveToFileAsNonPMA().
There will be no strange artifacts in a transparent image, like the grey borders usually seen in PMA output.