How to draw primitives with CustomCommand on RenderTexture

How to draw primitives with CustomCommand on RenderTexture
0.0 0


I’m trying to draw some primitives to a RenderTexture (cocos2d-x 3.0 rc0)

I tried this:
Create CustomCommand:

renderCommand = new CustomCommand();

Then add command in draw() function

void TestPrimitiveOnRT::draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated) {
    auto director = Director::getInstance();
    renderCommand->func = CC_CALLBACK_0(TestPrimitiveOnRT::onDraw, this);
void TestPrimitiveOnRT::onDraw() {
// stuff to draw

visit() is called from the parent node like this, while grabbing to RenderTexture :

rt->beginWithClear(0, 0, 0, 1);

It draws all children Sprites properly into the RenderTexture.
But it doesn’t draw TestPrimitiveOnRT.

It does draw TestPrimitiveOnRT, but on the screen, not on the RenderTexture.


I’m pretty sure that you have to put

rt->beginWithClear(0, 0, 0, 1);

inside TestPrimitiveOnRT::onDraw() (at the beginning and at the end correspondingly)


I am having the same issue, unless there is something I’m missing… are DrawPrimitives the correct way to draw a primitive onto a RenderTexture?

The following code doesn’t draw to the RenderTexture, but instead directly onto the screen.

rt->beginWithClear(0, 0, 0, 1);
DrawPrimitives::drawLine( Point(0,0), Point(100,100));

Setting rt->visible(false) will hide the beginWithClear rectangle, but the line will still be onscreen.