Convert cc.texture2d in base64 or bytearray

I created a screenshot of a game and I want to send it to the server. I tried different methods. but they don’t work. Please help
var renderTexture = new cc.RenderTexture(cc.winSize.width, cc.winSize.height);
renderTexture.begin();
cc.director.getRunningScene().visit();
renderTexture.end();
var texture=renderTexture.getSprite().getTexture();
How to send a texture to the server (then to save) ? I use Cocos Creator 1.3.1. and code on JS

I think you should hack the engine to do it:

  • bind the framebuffer used in rendertexture
  • use glReadPixels to read the data
  • bind framebuffer to previous one
  • send the data to server

If it is a JSB game, you can use RenderTexture::saveToFile() to save it to file and send file to server.

You can refer to the implementation of RenderTexture::saveToFile. coocs2d-html5’s implementation is different, but the idea is similar.

It will be quite tricky, we implement RenderTexture differently in WebGL and Canvas.

In Canvas, you can get texture’s data with toDataURL

renderTexture.sprite.getTexture().getHtmlElementObj().toDataURL()

In WebGL, you can access to the gl texture in this way:

renderTexture.sprite.getTexture()._webTextureObj

Then try the way described in this thread to access data

1 Like

I think there need a very important api for convert sprite to base64. In other game engines, I can convert the image to base64 easily, but I can’t this hard in cocos creator.