RenderTexture* ShowStar::visitNode(DiySaveType type, Node *node) { int index = (int)type; SaveItemTemplate object = SaveItemTemplateTem(WJUtils::stringAddInt("Save_", index, 3).c_str()); //定义一个自定义大小的渲染纹理 float scale = object.scale; const Size &size = node->getContentSize() * scale; RenderTexture *renderTexture = RenderTexture::create(size.width, size.height); renderTexture->setAnchorPoint(Vec2(0.5f, 0.5f)); renderTexture->ignoreAnchorPointForPosition(false); Vec2 ancPoint = node->getAnchorPoint(); float scaleOld = node->getScale(); Vec2 point = node->getPosition(); // 按照高度的比例缩放节点到当前的尺寸 node->setAnchorPoint(Vec2(0.5f, 0.5f)); node->ignoreAnchorPointForPosition(false); node->setScale(scale); renderTexture->beginWithClear(0, 0, 0, 0); node->setPosition(Vec2(size.width / 2, size.height / 2)); node->visit(); renderTexture->end(); Director::getInstance()->getRenderer()->render(); node->setAnchorPoint(ancPoint); node->setPosition(point); node->setScale(scaleOld); return renderTexture; }
Sprite* HelloWorld::spriteWithColor(Color4F bgColor, float textureWidth, float textureHeight) { // 1: Create new RenderTexture RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight); // 2: Call RenderTexture:begin rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a); // 3: Draw into the texture //// gradient _gradient_command.init(rt->getGlobalZOrder()); _gradient_command.func = std::bind(&HelloWorld::onDrawGradient, this, textureWidth, textureHeight); auto renderer = Director::getInstance()->getRenderer(); renderer->addCommand(&_gradient_command); //// noise cloud BlendFunc blendFunc; blendFunc.src = GL_DST_COLOR; blendFunc.dst = GL_ZERO; Sprite *noise = Sprite::create("Noise.png"); noise->setBlendFunc(blendFunc); noise->setPosition(Vec2(textureWidth / 2, textureHeight / 2)); noise->visit(); // 4: Call CCRenderTexture:end rt->end(); // 5: Create a new Sprite from the texture return Sprite::createWithTexture(rt->getSprite()->getTexture()); }
void DecorateScene::saveTheScreenShot(){ NotificationCenter::getInstance()->postNotification(kShotScreenEvent, __String::create("No")); RenderTexture* saveImageTexture = RenderTexture::create(canEatLayer->getContentSize().width, canEatLayer->getContentSize().height, Texture2D::PixelFormat::RGBA8888); saveImageTexture->begin(); canEatLayer->visit(); saveImageTexture->end(); saveImageTexture->getSprite()->setFlippedY(false); saveImageTexture->getSprite()->getTexture()->setAntiAliasTexParameters(); Director::getInstance()->getRenderer()->render(); log("the path is %s", (STFileUtility::getStoragePath()+"temp.png").c_str()); Image* pImage = saveImageTexture->newImage(); pImage->saveToFile(STFileUtility::getStoragePath()+"temp.png", false); pImage->release(); saveImageTexture->beginWithClear(1.0, 1.0, 1.0, 0); cannotEatLayer->visit(); saveImageTexture->end(); saveImageTexture->getSprite()->setFlippedY(false); saveImageTexture->getSprite()->getTexture()->setAntiAliasTexParameters(); Director::getInstance()->getRenderer()->render(); Image* pImage2 = saveImageTexture->newImage(); pImage2->saveToFile(STFileUtility::getStoragePath()+"temp1.png", false); pImage2->release(); }
virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) override { m_renderTexture->beginWithClear(0, 0, 0, 0); Node::visit(renderer, parentTransform, parentFlags); m_renderTexture->end(); m_renderTexture->getSprite()->setBlendFunc(m_blendFunc); m_renderTexture->setLocalZOrder(getLocalZOrder()); m_renderTexture->visit(renderer, Mat4::IDENTITY, 0); }
Sprite *HelloWorld::spriteWithColor(Color4F color, float texWidth, float texHeight) { RenderTexture *rt = RenderTexture::create(texWidth, texHeight); rt->beginWithClear(color.r, color.g, color.b, color.a); // 3: Draw into the texture // You'll add this later setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); _customCommand.init(_globalZOrder); _customCommand.func = [texWidth, texHeight, this]() { float gradientAlpha = 0.7f; Vec2 vertices[4]; Color4F colors[4]; int nVertices = 0; vertices[nVertices] = Vec2 {0, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0 }; vertices[nVertices] = Vec2 {texWidth, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0}; vertices[nVertices] = Vec2 {0, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; vertices[nVertices] = Vec2 {texWidth, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; getGLProgram()->use(); getGLProgram()->setUniformsForBuiltins(); cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR); glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors); // glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); cocos2d::GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST); glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices); }; auto renderer = Director::getInstance()->getRenderer(); renderer->addCommand(&_customCommand); Sprite *noise = Sprite::create("Noise-iphone5hd.png"); noise->setBlendFunc(BlendFunc {GL_DST_COLOR, GL_ZERO}); noise->setPosition(texWidth/2, texHeight/2); noise->visit(); rt->end(); return Sprite::createWithTexture(rt->getSprite()->getTexture()); }
CCMask* createMasked(Sprite* mask, Color3B color) { auto size = mask->getContentSize(); RenderTexture* pRenderTexture = RenderTexture::create(size.width, size.height); auto r = color.r / 255.0f; auto g = color.g / 255.0f; auto b = color.b / 255.0f; pRenderTexture->beginWithClear(r, g, b, 1); pRenderTexture->end(); auto sprite = Sprite::createWithTexture(pRenderTexture->getSprite()->getTexture()); sprite->setPosition(Point(mask->getContentSize().width/2, mask->getContentSize().height/2)); CCMask* masked = CCMask::create(mask , sprite); return masked; }
Sprite* HelloWorld::stripedSpriteWithColor(Color4F c1, Color4F c2, float textureWidth, float textureHeight, int nStripes) { // 1: Create new RenderTexture RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight); // 2: Call RenderTexture:begin rt->beginWithClear(c1.r, c1.g, c1.b, c1.a); // 3: Draw into the texture // we have to run custom command on renderer _stripes_command.init(rt->getGlobalZOrder()); _stripes_command.func = std::bind(&HelloWorld::onDrawStripes, this, c2, textureWidth, textureHeight, nStripes); auto renderer = Director::getInstance()->getRenderer(); renderer->addCommand(&_stripes_command); // gradient _gradient_command.init(rt->getGlobalZOrder()); _gradient_command.func = std::bind(&HelloWorld::onDrawGradient, this, textureWidth, textureHeight); renderer->addCommand(&_gradient_command); // gradient _tophighlight_command.init(rt->getGlobalZOrder()); _tophighlight_command.func = std::bind(&HelloWorld::onDrawTopHighlight, this, textureWidth, textureHeight); renderer->addCommand(&_tophighlight_command); // noise cloud BlendFunc blendFunc; blendFunc.src = GL_DST_COLOR; blendFunc.dst = GL_ZERO; Sprite *noise = Sprite::create("Noise.png"); noise->setBlendFunc(blendFunc); noise->setPosition(Vec2(textureWidth / 2, textureHeight / 2)); noise->visit(); // 4: Call CCRenderTexture:end rt->end(); // 5: Create a new Sprite from the texture return Sprite::createWithTexture(rt->getSprite()->getTexture()); }
// 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); }
Sprite *HelloWorld::spriteWithColor1(Color4F color1, Color4F color2, float texWidth, float texHeight, int nStripes) { RenderTexture *rt = RenderTexture::create(texWidth, texHeight); rt->beginWithClear(color1.r, color1.g, color1.b, color1.a); // 3: Draw into the texture // You'll add this later setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP)); _customCommand.init(_globalZOrder); _customCommand.func = [texWidth, texHeight, nStripes, color2, this]() { Vec2 *vertices = new Vec2[6*nStripes]; Color4F *colors = new Color4F[6*nStripes]; int nVertices = 0; float x1 = -texHeight; float x2; float y1 = texHeight; float y2 = 0; float dx = texWidth / nStripes * 2; float stripeWidth = dx/2; for (int i=0; i<nStripes; i++) { x2 = x1 + texHeight; vertices[nVertices] = Vec2 {x1, y1}; colors[nVertices++] = Color4F{color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x1+stripeWidth, y1}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x2, y2}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = vertices[nVertices-2]; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = vertices[nVertices-2]; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; vertices[nVertices] = Vec2 {x2+stripeWidth, y2}; colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a}; x1 += dx; } getGLProgram()->use(); getGLProgram()->setUniformsForBuiltins(); cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR); // glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_TRUE, 0, colors); glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices); float gradientAlpha = 0.7f; nVertices = 0; vertices[nVertices] = Vec2 {0, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0 }; vertices[nVertices] = Vec2 {texWidth, 0}; colors[nVertices++] = Color4F {0, 0, 0, 0}; vertices[nVertices] = Vec2 {0, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; vertices[nVertices] = Vec2 {texWidth, texHeight}; colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha}; glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors); // glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); cocos2d::GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST); glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices); // layer 3: top highlight float borderHeight = texHeight/16; float borderAlpha = 0.3f; nVertices = 0; vertices[nVertices] = Vec2 {0, 0}; colors[nVertices++] = Color4F {1, 1, 1, borderAlpha}; vertices[nVertices] = Vec2 {texWidth, 0}; colors[nVertices++] = Color4F {1, 1, 1, borderAlpha}; vertices[nVertices] = Vec2 {0, borderHeight}; colors[nVertices++] = Color4F {0, 0, 0, 0}; vertices[nVertices] = Vec2 {texWidth, borderHeight}; colors[nVertices++] = Color4F {0, 0, 0, 0}; glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors); glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices); CC_SAFE_DELETE_ARRAY(vertices); CC_SAFE_DELETE_ARRAY(colors); }; auto renderer = Director::getInstance()->getRenderer(); renderer->addCommand(&_customCommand); Sprite *noise = Sprite::create("Noise-iphone5hd.png"); noise->setBlendFunc(BlendFunc {GL_DST_COLOR, GL_ZERO}); noise->setPosition(texWidth/2, texHeight/2); noise->visit(); rt->end(); return Sprite::createWithTexture(rt->getSprite()->getTexture()); }