Reputation: 12747
I'm trying to create a black and white Sprite
based on another Sprite
. Since I don't have access to sprite pixel data after it's been created and cached, I'm working around it by creating a new Image
based on the , then alter the image data, then turn the Image
into a Texture
then finally into a Sprite
again, now black and white.
The problem is that when I use an Image
generated from the RenderTexture
, it doesn't actually draw it, but when I use the png image generated by RenderTexture::saveToFile
afterwards, it works fine.
Seems like I should be able to see output_sprite
on the scene
just fine, since it's being built much the same way, the only difference is the Image
is built from an existing file on the disk versus right from the RenderTexture
.
cocos2d::Sprite* anvil_sprite = cocos2d::Sprite::createWithSpriteFrameName("anvil.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);
anvil_sprite->setPosition(0,0);
RenderTexture* rt = RenderTexture::create(anvil_sprite->getContentSize().width, anvil_sprite->getContentSize().height);
rt->begin();
anvil_sprite->visit();
rt->end();
//generates a file so I can confirm it's generating correctly in the OS's
//image viewer
rt->saveToFile("test_FILE.png");
Image* output_image = new Image;
// Either use the png generated from a previous run's RenderTexture (which works)
// or use a new Image generated on this run.
// If I use the existing png, this works as intended, but I'd like to be
// able to use the texture I generate right away instead of needing to save
// it to disk first
/* OPTION A */
output_image->initWithImageFile("test_FILE.png"); // works
/* OPTION B */
output_image = rt->newImage(); //doesn't render anything
auto output_texture = new Texture2D;
output_texture->initWithImage(output_image);
Sprite* output_sprite = cocos2d::Sprite::createWithTexture(output_texture);
scene->addChild(output_sprite);
Beyond maybe GPU not having a chance to render out the texture before it's being used in the Sprite
, I'm not really sure what's going to happen. I'm looking for Option A and B to both render the same way, but right now only the one with the preexisting file as a source for the Image
is working.
Upvotes: 0
Views: 793
Reputation: 1386
Try this:
auto anvil_sprite = cocos2d::Sprite::create("player.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);
auto rt = RenderTexture::create(anvil_sprite->getTexture()->getPixelsWide(), anvil_sprite->getTexture()->getPixelsHigh());
rt->begin();
//Draw
anvil_sprite->visit();
rt->end();
cocos2d::Director::getInstance()->getRenderer()->render();
auto img = rt->newImage(); //Generate Image
auto texture = new cocos2d::Texture2D();
texture->initWithImage(img); //Texture sprite
auto sp = cocos2d::Sprite::createWithTexture(texture); //New Sprite
sp->setPosition(cocos2d::Director::getInstance()->getWinSize().width, cocos2d::Director::getInstance()->getWinSize().height);
this->addChild(sp);
It is possible to access the pixels of the same sprite using "getData" and editing this vector using a loop or a memset as you want to work it ...
auto anvil_sprite = cocos2d::Sprite::create("player.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);
auto rt = RenderTexture::create(anvil_sprite->getTexture()->getPixelsWide(), anvil_sprite->getTexture()->getPixelsHigh());
rt->begin();
anvil_sprite->visit();
rt->end();
cocos2d::Director::getInstance()->getRenderer()->render();
auto img = rt->newImage(); //Generate Image
auto data = img->getData();
for (size_t i = 0; i < img->getDataLen(); i++)
{
data[i] = 400;
}
//memset(data, 400, img->getDataLen());
I do not know much about pixel "colors", but you can document yourself a little more in colors if you want ;0
Upvotes: 1