我正在尝试根据另一个Sprite
制作黑白Sprite
。由于我在创建和缓存之后无法访问精灵像素数据,因此我通过创建一个新的Image
来解决它,然后更改图像数据,然后转动{{1} }再次进入Image
然后再进入Texture
,现在又变成了黑白。
问题在于,当我使用从Sprite
生成的Image
时,它实际上并没有绘制它,但是当我使用RenderTexture
之后生成的png图像时,它工作良好。
似乎我应该能够在RenderTexture::saveToFile
上看到output_sprite
就好了,因为它的构建方式大致相同,唯一的区别是scene
是从现有构建的磁盘上的文件与Image
中的文件相对。
RenderTexture
除了GPU在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);
中使用纹理之前没有机会渲染纹理,我真的不确定会发生什么。我正在寻找选项A和B两者以相同的方式呈现,但是现在只有具有预先存在的文件作为Sprite
的源的那个正在工作。
答案 0 :(得分:1)
试试这个:
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);
可以使用“getData”访问同一精灵的像素,并使用循环或memset编辑此向量,因为你想要它...
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());
我对像素“颜色”了解不多,但如果你愿意,你可以在colors中记录一些自己; 0