0

I'm having a strange issue with a ThreeJS scene in an OffscreenCanvas, but only with Safari. I create the OffscreenCanvas with this code:

let canvasElm = g('avatar-canvas');

const offscreen = canvasElm.transferControlToOffscreen();
console.log('renderWorker');
renderWorker = new Worker(new URL('workers/render-worker.js', import.meta.url), { type: 'module' });
renderWorker.mainThreadCtx = this;
renderWorker.postMessage(
  {
    task: 'prepareWorld',
    canvas: offscreen,
    worldParams: worldParams,
    canvasSize: { width: container.offsetWidth, height: container.offsetHeight },
  },
  [offscreen],
);

and then in my web worker, I have:

function prepareWorld(data) {
  canvas = data.canvas;
  worldParams = data.worldParams;

  renderer = new THREE.WebGLRenderer({
    canvas,
    antialias: true,
    alpha: true,
    powerPreference: 'high-performance',
    precision: 'mediump',
  });

  canvasSize.width = data.canvasSize.width;
  canvasSize.height = data.canvasSize.height;
  
  etc...

In Safari, I get the error

THREE.WebGLRenderer: Argument 1 ('contextType') to OffscreenCanvas.getContext must be one of: "2d", "webgl", "webgl2", "bitmaprenderer"

And investigating further, it's this line in WebGLRenderer.js inside of THREE that is returning null and shouldn't be:

const context = canvas.getContext( contextName, contextAttributes );

Apparently, getContext will return null if the canvas already has a context, but in this case it shouldn't and as far as I know, there's no way to check if it has one or not.

I can't share the project unfortunately, but this ThreeJS demo exhibits identical behaviour:

https://threejs.org/examples/webgl_worker_offscreencanvas.html

Can anyone help at all please?

  • Are you sure the error message really says "2s"? – Kaiido Jun 01 '23 at 09:08
  • Haha, no, it says "2d". I'll edit my post! – GeorgePorge Jun 01 '23 at 09:27
  • The demo I linked works for you!? Wow. – GeorgePorge Jun 01 '23 at 09:28
  • I'm on Safari 16.4 (18615.1.26.110.1) too, and the linked demo definitely doesn't work. – GeorgePorge Jun 01 '23 at 09:36
  • Nope. And 2 developers on my team are seeing the same error. – GeorgePorge Jun 05 '23 at 08:27
  • Oh, my bad. I misinterpreted that page as it also does not work on stable FF which doesn't support module workers yet. So indeed, the right canvas does not show anything and I have the same error message in the console. This error is caused by the last option they try `"experimental-webgl"` which is indeed invalid here. But that's surprising they don't support any of "webgl2" or "webgl"... I also thought they did. – Kaiido Jun 05 '23 at 09:09
  • ... https://trac.webkit.org/changeset/266275/webkit/ – Kaiido Jun 05 '23 at 09:13
  • Thanks for taking a look, anyway @Kaiido. Annoyingly, "webgl" and "webgl2" are supported in the latest technology preview: https://developer.apple.com/documentation/safari-technology-preview-release-notes/stp-release-114#WebGL – GeorgePorge Jun 05 '23 at 14:14
  • This is a 2020 release note. This has long been part of stable too. My TP (Release 170, which might be a bit outdated, but by no more than a week) doesn't support it either. Something has regressed that. Unfortunately I haven't yet had the time to check what though. But that might be worth a bug report. – Kaiido Jun 05 '23 at 14:33
  • So they actually already know about this: https://bugs.webkit.org/show_bug.cgi?id=254071 That's a weird situation. – Kaiido Jun 16 '23 at 07:56

0 Answers0