expo / expo-phaser

Use Phaser.js on mobile with Expo GL
MIT License
224 stars 30 forks source link

EXGL: Invalid pixel data argument for gl.texImage2D()! #11

Open justinTime225 opened 4 years ago

justinTime225 commented 4 years ago

The instantiation of Phaser.game({ context }) causes this error to display on android only.

justinTime225 commented 4 years ago

Got it working now, leaving the solution here for anybody that may run into this problem.

So the arguments for gl.texImage2D is as followed...

[ 3553, 0, 6408, 1, 1, 0, 6408, 5121, {} ]

The one breaking android is the last argument. In android, we have to pass in null instead. So the solution is to go to the source code and check if the last argument is an empty object, if it is then pass in null instead.

1 ) Go to @expo/browser-polyfill/lib/api/CanvasRenderingContext2D.js 2) Look at method putImageData 3) Look at the last argument where gl.texImage2D is being called. 4) If imagedata.data is an empty object then pass in null instead

const dataToPass =
      imagedata.data &&
      typeof imagedata.data === 'object' &&
      Object.keys(imagedata.data).length === 0
        ? null
        : imagedata.data;

    gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGBA,
      dirtyWidth,
      dirtyHeight,
      0,
      gl.RGBA,
      gl.UNSIGNED_BYTE,
      dataToPass,
    );

Since this is most likely not commit-able, we'll have to override this file with base64 when the app runs on production.

base64 -i node_modules/@expo/browser-polyfill/lib/API/CanvasRenderingContext2D.js

This will return a string output, take that output and run

echo "long output" | base64 -D > node_modules/@expo/browser-polyfill/lib/API/CanvasRenderingContext2D.js