【发布时间】:2016-12-06 08:12:22
【问题描述】:
我正在尝试基于另一个Sprite 创建一个黑白Sprite。由于在创建和缓存后我无法访问精灵像素数据,因此我正在通过基于 的创建新的Image 来解决它,然后更改图像数据,然后将Image 转换为@ 987654325@ 然后终于又变成了Sprite,现在是黑白的。
问题是,当我使用从RenderTexture 生成的Image 时,它实际上并没有绘制它,但是当我使用RenderTexture::saveToFile 生成的png 图像之后,它工作正常。
似乎我应该能够在 scene 上看到 output_sprite 就好了,因为它的构建方式大致相同,唯一的区别是 Image 是从磁盘上的现有文件与右侧构建的来自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);
也许 GPU 在 Sprite 中使用之前没有机会渲染出纹理,我不确定会发生什么。我正在寻找选项 A 和 B 以相同的方式呈现,但现在只有一个以预先存在的文件作为 Image 的源的选项正在工作。
【问题讨论】:
-
我正在使用 v3.10,我想这可能是一个错误,从现在到 v3.14 之间会得到解决,但我一直坚持到他们为 Cocos Creator 添加 C++ 支持。
标签: c++ cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x