2016-12-06 87 views
0

我試圖根據另一個Sprite創建黑色和白色Sprite。由於在創建和緩存後我無法訪問精靈像素數據,因此我正在通過創建基於該精確像素數據的新Image,然後更改圖像數據,然後將Image變成Texture,然後最終成爲再次,現在是黑色和白色。RenderTexture創建的圖片不可見

問題是,當我使用從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); 

超越它在Sprite在使用前也許GPU沒有機會呈現出質感,我真的不知道發生了什麼事情發生。我正在尋找選項A和B以同樣的方式呈現,但是現在只有那個已經存在的文件作爲Image的源代碼正在工作。

+0

我使用的是V3.10,我想,也許這是一個錯誤,它會被解決補間現在和v3.14,但我堅持下去,直到他們將C++支持添加到Cocos Creator。 – TankorSmash

回答

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