void ClippingToRenderTextureTest::reproduceBug() { auto director = Director::getInstance(); Size visibleSize = director->getVisibleSize(); Point origin = director->getVisibleOrigin(); // add "HelloWorld" splash screen" auto sprite = Sprite::create("Images/grossini.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); // container node that will contain the clippingNode auto container = Node::create(); container->retain(); auto stencil = DrawNode::create(); Point triangle[3]; triangle[0] = Point(-50, -50); triangle[1] = Point(50, -50); triangle[2] = Point(0, 50); Color4F green(0, 1, 0, 1); stencil->drawPolygon(triangle, 3, green, 0, green); auto clipper = ClippingNode::create(); clipper->setAnchorPoint(Point(0.5, 0.5)); clipper->setPosition( Point(visibleSize.width/2, visibleSize.height/2) ); clipper->setStencil(stencil); clipper->setInverted(true); container->addChild(clipper, 1); auto img = DrawNode::create(); triangle[0] = Point(-200, -200); triangle[1] = Point(200, -200); triangle[2] = Point(0, 200); Color4F red(1, 0, 0, 1); img->drawPolygon(triangle, 3, red, 0, red); clipper->addChild(img); // container rendered on Texture the size of the screen and because Clipping node use stencil buffer so we need to // create RenderTexture with depthStencil format parameter RenderTexture* rt = RenderTexture::create(visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8); rt->setPosition(visibleSize.width/2, visibleSize.height/2); this->addChild(rt); rt->begin(); container->visit(); rt->end(); }
std::string ProfileScreen::saveScreenshot() const { Size screen = Director::getInstance()->getWinSize(); RenderTexture *tex = RenderTexture::create(int(screen.width), int(screen.height)); tex->retain(); tex->setPosition(Vec2(screen.width/2.0f, screen.height/2.0f)); tex->begin(); Director::getInstance()->getRunningScene()->visit(); tex->end(); __String *path = __String::createWithFormat("%s%d.png", "screenshot_", int(time(0))); tex->saveToFile(path->getCString(), Image::Format::PNG); return FileUtils::getInstance()->getWritablePath() + path->getCString(); }
bool MapRender::initRenderTexture() { // create a render texture, this is what we are going to draw into int perWidth = WIDTH / MAX_TEX_X; int perHeight = HEIGHT / MAX_TEX_Y; for (size_t i = 0; i < MAX_TEX_X; i++) { for (size_t j = 0; j < MAX_TEX_Y; j++) { RenderTexture* renderTexture = RenderTexture::create(perWidth, perHeight, Texture2D::PixelFormat::RGBA8888); renderTexture->retain(); renderTexture->setPosition(cocos2d::Vec2(perWidth / 2 + i * perWidth, perHeight / 2 + j * perHeight)); this->addChild(renderTexture); _renderTexture[i][j] = renderTexture; } } return true; }
// TransitionProgress void TransitionProgress::onEnter() { TransitionScene::onEnter(); setupTransition(); // create a transparent color layer // in which we are going to add our rendertextures Size size = Director::getInstance()->getWinSize(); // create the second render texture for outScene RenderTexture *texture = RenderTexture::create((int)size.width, (int)size.height); texture->getSprite()->setAnchorPoint(Vector2(0.5f,0.5f)); texture->setPosition(Vector2(size.width/2, size.height/2)); texture->setAnchorPoint(Vector2(0.5f,0.5f)); // render outScene to its texturebuffer texture->beginWithClear(0, 0, 0, 1); _sceneToBeModified->visit(); texture->end(); // Since we've passed the outScene to the texture we don't need it. if (_sceneToBeModified == _outScene) { hideOutShowIn(); } // We need the texture in RenderTexture. ProgressTimer *node = progressTimerNodeWithRenderTexture(texture); // create the blend action ActionInterval* layerAction = (ActionInterval*)Sequence::create( ProgressFromTo::create(_duration, _from, _to), CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)), nullptr); // run the blend action node->runAction(layerAction); // add the layer (which contains our two rendertextures) to the scene addChild(node, 2, kSceneRadial); }
void TransitionCrossFade::onEnter() { TransitionScene::onEnter(); // create a transparent color layer // in which we are going to add our rendertextures Color4B color(0,0,0,0); Size size = _director->getWinSize(); LayerColor* layer = LayerColor::create(color); // create the first render texture for inScene RenderTexture* inTexture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8); if (nullptr == inTexture) { return; } inTexture->getSprite()->setAnchorPoint( Vec2::ANCHOR_MIDDLE ); inTexture->setPosition(size.width/2, size.height/2); inTexture->setAnchorPoint( Vec2::ANCHOR_MIDDLE ); // render inScene to its texturebuffer inTexture->begin(); _inScene->visit(); inTexture->end(); // create the second render texture for outScene RenderTexture* outTexture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8); outTexture->getSprite()->setAnchorPoint( Vec2::ANCHOR_MIDDLE ); outTexture->setPosition(size.width/2, size.height/2); outTexture->setAnchorPoint( Vec2::ANCHOR_MIDDLE ); // render outScene to its texturebuffer outTexture->begin(); _outScene->visit(); outTexture->end(); // create blend functions BlendFunc blend1 = {GL_ONE, GL_ONE}; // inScene will lay on background and will not be used with alpha BlendFunc blend2 = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}; // we are going to blend outScene via alpha // set blendfunctions inTexture->getSprite()->setBlendFunc(blend1); outTexture->getSprite()->setBlendFunc(blend2); // add render textures to the layer layer->addChild(inTexture); layer->addChild(outTexture); // initial opacity: inTexture->getSprite()->setOpacity(255); outTexture->getSprite()->setOpacity(255); // create the blend action Action* layerAction = Sequence::create ( FadeTo::create(_duration, 0), CallFunc::create(CC_CALLBACK_0(TransitionScene::hideOutShowIn,this)), CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)), nullptr ); // run the blend action outTexture->getSprite()->runAction( layerAction ); // add the layer (which contains our two rendertextures) to the scene addChild(layer, 2, kSceneFade); }