Get pixel color of an image

Continuing from an eight-year-old theme How to get pixel color of an image

I have a sprite (mask). I don’t want to render it. How do I, knowing the coordinates of a point, get its color from this sprite. Possible browser solution (I use texture here):

        const canvas = document.createElement("canvas");

        const img = as HTMLImageElement;
        canvas.width = img.width;
        canvas.height = img.height;

        canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height);
        console.dir(canvas.getContext("2d").getImageData(250, 250, 1, 1));

But this, I assume, is not a cross-platform solution. What is the correct way to make it work, for example, for android. I know that in Unity, young Godot and other engines, this is solved with one line and one command. What features in this regard are possible in Cocos Creator version 3.5 in 2022? Or is it the same as eight years ago?

I found the readPixels method in the RenderTexture class, but I don’t quite understand how, after new RenderTexture() and setting the width and height, I can fill in the texture I need there.

Thank you!

1 Like

i have same problem.
if got solution help me.

Perhaps something has changed since then, but then I used this code, which is in the post above. This is not a cross-platform solution and only works in the browser.

You can pass your render texture to Camera as a targetTexture. You can use second camera for it. Or even use special custom layers for nodes that should be visible by camera with render texture target.