Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

troika-three-text: possible to use in a webworker? #323

Open
LukeWood opened this issue Jul 17, 2024 · 6 comments
Open

troika-three-text: possible to use in a webworker? #323

LukeWood opened this issue Jul 17, 2024 · 6 comments

Comments

@LukeWood
Copy link

When attempting to get troika text to run in a webworker I get the following error:

ReferenceError: document is not defined
    at getTextRenderInfo (troika-three-text.js?v=a6a14d45:4775:20)
    at Text.sync (troika-three-text.js?v=a6a14d45:5511:9)

Any interest in fixing this?

@lojjic
Copy link
Collaborator

lojjic commented Jul 17, 2024

How are you trying to use it in a worker? Rendering to an OffscreenCanvas? Gimme some context.

@LukeWood
Copy link
Author

Of course - sorry about that!

Yes, I'm rendering to an OffscreenCanvas with a WebGLRendererer ( NOT webGPU ).

@LukeWood
Copy link
Author

LukeWood commented Jul 17, 2024

It is something like...

  const canvas = document.getElementById('game-canvas') as any;
  const offscreen = canvas.transferControlToOffscreen();

  // const proxy = new ElementProxy(window as any, orbitalEventHandlers, orbitalExtraArgs, (data: any) => {
  //   gameHandle.handleProxiedEvent(data)
  // });

  gameHandle.setupGraphics(
    offscreen,
    window.innerWidth,
    window.innerHeight,
    window.devicePixelRatio
  );

Then in the worker I do:

function createText() {
    const text = new Text();
    text.text = this.parent.name.toUpperCase();
    text.fontSize = 1;
    text.rotateX(Math.PI / 2);
    text.scale.set(-1, 1, 1);
    text.color = 0xffffff;
    text.anchorX = 'center';
    text.anchorY = 'top';
    text.material.opacity = 0.5;
    text.sync();
    return text;
}

and it fails.

@LukeWood
Copy link
Author

It looks like in:

const canvas = document.createElement('canvas')

We can replace document.createElement('canvas') with new OffscreenCanvas perhaps. That is the only reference to document I see.

@lojjic
Copy link
Collaborator

lojjic commented Jul 17, 2024

Makes sense. If I recall, I originally tried using an OffscreenCanvas instead of (or optionally to) the document.createElement('canvas') but there was some issue with that where the SDFs rendered into it would render incorrectly -- maybe a colorspace difference so the color channel values were wrong?

That was a while ago, though, so if you try it out and it works then I'd definitely be open to merging it.

@LukeWood
Copy link
Author

Makes sense. If I recall, I originally tried using an OffscreenCanvas instead of (or optionally to) the document.createElement('canvas') but there was some issue with that where the SDFs rendered into it would render incorrectly -- maybe a colorspace difference so the color channel values were wrong?

Yeah unfortunately you have to set colorspaces a bit more consistently in the worker/offscreen world than in the main thread. It is a bit of a pain. I'll let you know if I figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants