RenderTexture multiple draws

Hi there!

I am using a RenderTexture to visit a bunch of sprite that are part of a animation node. The purpose of this is to apply a single shader to the render texture once it has visited each texture from the animation.

My problem is currently it is now drawing the animation twice, once for the actual animation node and agian for the render texture. If I try and remove the original animation from its parent or set its visibilty to false, the renderTexure doesn’t capture the new animation frames.

Is there a good way to keep my animation playing while but not increase my draw calls?

Can you show us?

I start by creating the render texture and registering the nodes it needs to visit for update

ShaderUtil.bindRenderTexture = function(nodes, parentNode, shaderConst)
    {

        var screenSize = cc.director.getWinSize();
        var ssInPixels = cc.director.getWinSizeInPixels();

        var initRect = new cc.Rect(0,0,screenSize.width, screenSize.height);
        var finalRect = new cc.Rect(0,0,ssInPixels.width,ssInPixels.height);

        var rt = new cc.RenderTexture(screenSize.width, screenSize.height);
        rt.setAutoDraw(false);
        rt.setVirtualViewport(cc.p(0,0),initRect ,finalRect);

        var shader = ShaderUtil.initShader(rt.sprite, shaderConst);

        parentNode.addChild(rt);
        ShaderUtil.RenderTextures.push(
            {
                rt: rt,
                nodes: nodes
            }
        );

        return shader;

    
};

Next I visit the anim.node in an update loop

ShaderUtil.updateRenderTextures = function() {
        for(var i = 0; i < ShaderUtil.RenderTextures.length; i++)
        {
            ShaderUtil.RenderTextures[i].rt.beginWithClear(0, 0, 0, 0);
            for(var j = 0; j < ShaderUtil.RenderTextures[i].nodes.length; j++) {
                ShaderUtil.RenderTextures[i].nodes[j].visit();
            }
            ShaderUtil.RenderTextures[i].rt.end();
        }
    };

This is a good question, curious for anyone’s answers…

You might need to dig deeper in the render pipeline to achieve this.

1 Like

If you take a minute to visit the cocos website and scroll down to Cocos2dx you will see that Cocos2dx includes C++, JavaScript and Lua. In fact when making a new topic on the forum there is no option just to tag it as just JavaScript, the only option is cocos2dx and JavaScript…

As for your answer normally this would be a perfectly acceptable way to accomplish a single draw call to the render texture. Unfortunately this does not work in my case. When you turn of the visibility or remove a node from its parent, it will no longer render, check. However cocos animation actions will not trigger either when the visibility is turned off. So even though this works for capturing the frame data, the frames are not updating because when animations actions are triggered elsewhere in the project, the visibility is set to false, so the actions do not get performed

Just throwing this out there, so I’m not sure if it would work, but what happens if you add those Sprites to a cocos2d::Vector, and leave them un-parented, so don’t actually add them to the scene as a child nodes. The purpose of the cocos2d::Vector is to stop the sprites from being freed, since it calls retain() on them internally.

The Sprites would still be set to enabled and visible internally, but not actually part of any rendering loop. You would still need a way to update them though, by looping through the list and calling the relevant function to ensure actions are still run on the sprite (which is what the Animation is).

If that doesn’t work, then you may need to subclass the relevant cocos2d classes and adjust them to allow for the functionality you’re after.

1 Like

It was always tagged JS and cocos2dx… I appreciate C++ code it is still helpful for problem solving

I’m glad you are so sure it “definitely works” but I think you are missing the problem. Everything is rendering fine, and I am perfectly able to achieve my post processing effects I want. The issue is I am still drawing the pre-processed graphics also. The issue doesn’t stem from the render texture and the visit function. It has to do with Cocos “Actions”

This would be great unfortunately the retain() method doesn’t seem work

I’m probably going to have to dig a bit deeper