Creation of isTransparent() javascript function in cocos2d-x v.3.x

Hi,

I want to create a JavaScript function that will work in cocos2d-x v.3.x that returns true if a pixel at the specified position of a sprite (node) is transparent like the following:

function isTransparent(node, pos) {
          ...
          ...
         return isPixelTransparent;
}

It is important that the function will work through JSB and can be cross compiled for iOS and Android.

@grimfate provided such a function in Detect touch in sprite thread but unfortunately it does not work in v.3.x of cocos2d-x.

Detecting transparent pixels is an annoying problem that I wish cocos2d-x would have a solution to built-in. I’m not sure the best way to do it, although people have mentioned (if you are doing it for touch) manually creating polygons that cover the non-transparent parts and detecting if the touch is inside any of the polygons, or using a callback when the RenderTexture is drawn so that you can use glReadPixels() then.

If you wish to use the code I posted in the thread you linked, it doesn’t work with 3.x because the renderer doesn’t immediately draw the RenderTexture, so gl.readPixels() will probably be looking at a blank screen. In this thread I gave a solution that seemed to work. It says how to create the bindings for Javascript to call the renderer’s render() function, which will force the renderer to render the RenderTexture. Then you just have to call the function after drawing to the RenderTexture, but before calling gl.readPixels().