跳到正文
DEV Community· Yooo, Games!·· 4 天前AI 評分57

yooo.games 如何讓 3D 保齡球在 2019 年的 Chromium 79 電視瀏覽器上運行,並以手機作控制器

Making 3D bowling run on a 2019 TV browser (Chromium 79, phones as controllers)

AI 導讀

作者分享了 yooo.games 在採用 Chromium 79 的 LG 電視瀏覽器上運行 3D 保齡球,並以手機作控制器的實作經驗。他發現該電視的 Canvas 2D 由 CPU 繪製,並透過每幀只上傳一次頂點資料,將射擊遊戲由 6–10 fps 提升至 58 fps。文章亦整理了預先編譯 shader、減少 DOM 更新,以及用腳本檢查 Chromium 79 相容性的做法。

正文

Friends are over, dinner's done, and someone says "let's play something." Half the room doesn't know the board game. Nobody has a console. But everybody has a phone, and there's a TV on the wall.

So I built yooo.games: you open the site in the TV's browser, everyone scans a QR code, and their phone becomes the controller. There's nothing to install, and you don't need an account. It now has 23 games. Some you talk and draw in (Find the Spy, Mafia with the screen as the host, Quick Pencil), and in others you swing the phone like a real object: a bowling ball, a revolver in a holster, a fishing rod.

How it works: open the site on a big screen, scan the QR code, play together

The games weren't the hard part. The TV was. This post is about that.

The reference device: an LG with Chromium 79

My test TV is an LG on webOS. Its browser is Chromium 79, from late 2019, on a weak ARM chip with a Mali GPU. So I set the build target to that device and kept it there:

// vite.config
build: { target: 'chrome79' }

The first versions of the 3D games stuttered and froze on it. The 2D games were worse. Here's what I found, in the order it hurt.

1. On this TV, Canvas 2D runs on the CPU and WebGL runs on the GPU

I measured every game on the TV. The three.js games (golf, bowling, curling) ran at 49-59 fps. The full-screen Canvas 2D games ran at 7-32 fps. The shooting gallery, a pretty simple game, managed 8 fps.

It's backwards from what you'd expect: the "simple" 2D games were the slow ones, because on this TV Canvas 2D is drawn by the CPU.

What fixed it: I stopped drawing with Canvas 2D every frame. Each sprite is drawn with Canvas 2D once into a "stamp", uploaded as a texture, and from then on a small WebGL layer only positions those textures. On a desktop the shooting gallery's frame went from 4.5 ms to 0.3 ms. On the TV it was still slow, though, for the reason in the next section. Six more games were moved over the same way.

There's a catch, and it took a while to learn: a stamp that gets redrawn costs a CPU paint plus a texture upload again. One game redrew its characters every 250 ms and their eyes every 120 ms, which came to 281 texture uploads in 15 seconds. Now anything that moves is a static stamp moved by a transform, and animations are a few pre-drawn frames, not redraws.

2. Many bufferSubData calls per frame stall the Mali tiler

After the move to WebGL, the shooting gallery still ran at 6-10 fps on the TV. JS timings, long tasks and even gl.finish() showed nothing wrong.

The cause: I rewrote one vertex buffer dozens of times per frame with bufferSubData. On a tiled GPU like Mali, the driver has to stop and copy the buffer when you overwrite data a pending draw still uses. None of that shows up in a JavaScript profiler.

What fixed it: I upload all vertices for the frame once with bufferData(..., STREAM_DRAW) and draw in pieces by offset. The shooting gallery went to 58 fps, with a 17 ms frame. Now there's a counter in every room's debug journal, "vertex uploads per frame", and 1-3 is normal.

![Shooting gallery on an LG TV: 8 fps with Canvas 2D, 6-10 fps with WebGL and many bufferSubData calls, 58 fps with one upload per frame]

3. Shaders compiled mid-round are a freeze

three.js compiles a shader the first time an object becomes visible. On a desktop you never notice. On the TV, the first bonus item in a round, the first new golf hole, or a laser sight that's hidden until you aim each caused a visible hitch, sometimes hundreds of milliseconds.

Things that quietly compiled new shaders:

  • transparent double-sided materials picked a shader again every frame (forceSinglePass fixed that: frame p95 went from 33 to 17 ms in my slowed-down test);
  • removing the last material of a kind throws away its program, so the jelly shader was rebuilt at the start of every round;
  • setting material.map from null to a texture is a different shader, so give materials an empty texture up front;
  • hidden objects aren't compiled until first shown, so call renderer.compile(scene, camera) (it walks hidden objects too) when the scene is built, and compileAsync for things built mid-match.

The rule now: anything that can appear in a round is prepared at game start. That covers textures (initTexture), shaders (compiled against a warm-up scene), and pooled meshes that get hidden and reused instead of being disposed and recreated.

4. The DOM over the canvas was most of the main thread

Name tags over eight players, with only transform changing, took a fifth of the main thread: style recalc, layers, compositing. On the TV the overlay was relaid 100-170 times a second. Changing a font size every frame was worse. One game went from 19 to 54 fps and from 714 to ~0 ms/s of long tasks after I stopped doing that.

What I do now:

  • anything that moves every frame lives in the scene: name tags are one canvas texture and one draw call, and 3D labels are THREE.Sprites with sizeAttenuation: false;
  • text is written only when it changed (setText checks first), and that includes writing the same className or hidden;
  • never read offsetWidth to restart a CSS animation, because that forces a full layout mid-frame;
  • never read clientWidth in the render loop; a ResizeObserver reports size instead;
  • new Intl.NumberFormat() on every call cost one game 5 ms/s, so formatters are cached.

A sneaky one: the results screen with faces and medals was rebuilt on every state update (a spectator joined, someone sent an emoji reaction). On the TV each rebuild was a 1-3 second task with zero lines of JS in the profile, just the browser decoding and rasterizing every image again.

5. Chromium 79 doesn't know half of modern CSS

The bundler lowers syntax, but CSS is on you. Chromium 79 has no :has(), no gap in flexbox (my drawing albums sat glued together on the TV), no aspect-ratio, :is(), @container, dvh or color-mix(). CSS built in JS strings isn't touched by the minifier at all, and one :focus-visible in a selector list drops the whole rule, :hover included.

There's now a guard in the build report that scans everything shipped to the screen for features Chromium 79 doesn't have, and it fails the build.

Testing a TV without the TV

Walking to the living room after every change doesn't scale to 23 games. So there's a tvcheck script: headless Chrome with Canvas 2D acceleration off, CPU throttled Г—20, the TV's exact window size, and the game playing itself with bots. It reports fps, frame p95, long tasks, and the same counters as the TV journal (draw calls, vertex and texture uploads per frame, new shaders, DOM rebuilds per second), each with the line of code that caused it.

Its numbers aren't the TV's numbers. Chrome doesn't slow down the GPU, and some TV hitches never show up on a desktop even with a software GPU. But anything that's expensive under Г—20 is expensive on the TV too. So I compare traces before and after a change rather than chasing fps.

And there's an even weaker tier. Some TVs in the wild have no WebGL 2, or run Chromium 38. Those get a "superlite" mode: only the games whose screen has no WebGL and draws only when something changes (quiz, spy, drawing games, Mafia). The rest are shown greyed out, with an honest "won't run on this TV".

The other half: phones that disagree about physics

Bowling reads devicemotion while you hold a button, and computes the throw the moment you let go.

!A throw in yooo.games bowling: the ball rolls down the lane, then a spare

Some things I didn't expect:

  • Gravity has the opposite sign on iOS and Android.
  • The rotationRate axes don't match their names: in practice alpha is rotation around x, beta around y, gamma around z.
  • "Which direction did I throw" can't be read as an angle to the screen, because the phone doesn't know where the TV is. Instead you calibrate a straight throw, and direction is the deviation from that swing axis.
  • Some phones have a gyro that's noisy or missing. After three suspicious throws in a row, the game switches to accelerometer-only mode, with a sideways jolt for direction and a late release for loft.

None of it was tuned by eye. There are 58 recorded real throws from Android phones and iPhones as test fixtures. The physics (Rapier, 120 Hz) runs on the server with no renderer, and a sweep script throws thousands of balls across a grid of speeds and spins. The screens only replay 30 snapshots a second, about 150 ms behind, so a weak TV never runs the physics, and a second screen in the room shows the same game.

What I'd tell someone starting this

  1. Measure on the real device first. Two of my early "TV fixes" were guesses, and they did nothing.
  2. Assume the CPU is your bottleneck and the GPU is your friend, then count uploads, not just draw calls.
  3. Nothing new in the middle of a round: no textures, shaders, materials or DOM rebuilds.
  4. Keep a written list of rules and check them with a script. I re-learned the same lesson for every new game until they were written down.

!Quick Pencil: three players drew "fish" on their phones, the TV shows all three drawings

You can try it on any TV, laptop or projector: yooo.games. Bowling works with one laptop and one phone. With friends, start with Find the Spy or Quick Pencil. I've only tested one TV model properly, so if you have a Samsung, an Android TV or anything else, I'd love to hear what breaks.

Part of the code, and the first draft of this post, were written with an AI assistant (Claude). The measurements, the TV and the living-room playtests are real.

來源:DEV Community · dev.to