Skip to content

Fix camera.screenToWorld returning NaN when called in initialize#8950

Merged
mvaligursky merged 1 commit into
mainfrom
mv-fix-screentoworld-webgpu
Jun 23, 2026
Merged

Fix camera.screenToWorld returning NaN when called in initialize#8950
mvaligursky merged 1 commit into
mainfrom
mv-fix-screentoworld-webgpu

Conversation

@mvaligursky

Copy link
Copy Markdown
Contributor

Fixes #8932. CameraComponent#screenToWorld returned NaN when called from a script's initialize/postInitialize (or the app initialize event) on WebGPU, while working correctly on WebGL.

Cause:
screenToWorld divides by device.clientRect.width/height. clientRect initializes to {0, 0} and is only refreshed by device.update(), which runs in the frame loop after app.start() fires initialize/postInitialize. The WebGL device constructor happened to call updateClientRect(), so its rect was valid early; the WebGPU device constructor did not, leaving clientRect at {0, 0} until the first frame — causing a divide-by-zero and NaN results.

Changes:

  • Move the updateClientRect() call into the base GraphicsDevice constructor so clientRect is valid before the first frame on all backends (WebGL, WebGPU, and any future ones).
  • Remove the now-redundant call from the WebGL device constructor.

screenToWorld divides by device.clientRect, which initializes to {0,0}
and is only refreshed by device.update() in the frame loop, after start()
fires initialize/postInitialize. The WebGL device constructor called
updateClientRect() so its rect was valid early, but the WebGPU constructor
did not, leaving clientRect at {0,0} and producing NaN results.

Move the updateClientRect() call into the base GraphicsDevice constructor
so all backends have a valid clientRect before the first frame, and remove
the now-redundant WebGL-specific call.

Fixes #8932
@github-actions

Copy link
Copy Markdown

Build size report

This PR changes the size of the minified bundles.

Bundle Minified Gzip Brotli
playcanvas.min.js 2260.9 KB — 581.6 KB (+0.0 KB, +0.00%) 451.9 KB (−0.0 KB, −0.00%)
playcanvas.min.mjs 2258.3 KB — 580.7 KB (+0.0 KB, +0.00%) 451.7 KB (−0.1 KB, −0.03%)

@mvaligursky mvaligursky merged commit d8fbe90 into main Jun 23, 2026
10 checks passed
@mvaligursky mvaligursky deleted the mv-fix-screentoworld-webgpu branch June 23, 2026 10:20
willeastcott added a commit to playcanvas/developer-site that referenced this pull request Jul 1, 2026
Engine 2.20.0 (playcanvas/engine#8950) moved updateClientRect() into the
base GraphicsDevice constructor, which calls canvas.getBoundingClientRect().
@playcanvas/react constructs a NullGraphicsDevice with a bare mock canvas
object at module scope for SSR, so importing it during Docusaurus static
site generation now throws, failing all 639 doc pages. Pin the engine to
2.19.x (tilde range, since a caret would re-resolve to 2.20.x) until the
upstream fixes land.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: graphics Graphics related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

camera.screenToWorld returns NaN if called in initailize or postInitialize with WebGPU enabled

1 participant