Devlog

Fixing Imported 3D Models in Ambient 3D: Colours, Draw Calls and Memory

Ambient 3D renders a 3D model as your desktop wallpaper, behind the icons. The built-in models were fine. Imported ones were a mess: car models came out in a single flat colour, the settings window was hard to use, and a big model made the whole thing heavy. This is a write-up of what was wrong and how we fixed it in the latest rework, with the real numbers we measured along the way.

Problem 1: imported models lost their colours

The engine is built on three.js running inside a Qt WebEngine view. When a model was imported, the old code ran every mesh through a "smart material" function that guessed what each part was from its name (glass, tyre, chrome, paint) and replaced its material accordingly. The model's own materials were thrown away. Then the colour picker in the settings repainted every mesh it could find. So whatever colours the file had, you never saw them.

For OBJ files there was a second problem underneath. OBJ stores geometry, but the colours and textures live in a separate .mtl file that the OBJ references with an mtllib line. The app copied only the .obj when importing and never loaded the .mtl at all, so there was no colour information to keep in the first place.

The fix had three parts:

  • Import the sidecar files. When you import an OBJ, the app now reads its mtllib lines, copies the material libraries next to the model, and copies every texture they reference. Exporters love writing absolute paths from the author's own PC (C:\Users\someone\Desktop\tex\paint red.png), so the importer tries the path as written, then shorter tails of it, then just the file name, and rewrites the copied .mtl to use plain relative paths.
  • Load materials before parsing. The engine fetches the OBJ text once, finds the mtllib line, loads the material library with three.js's MTLLoader, and only then parses the geometry with those materials. File names with spaces needed a URL modifier on the loading manager, because the loader doesn't encode them.
  • Keep all three looks. Each mesh now holds three material sets: the file's original materials, the old automatic "smart" materials, and a single-colour material. The settings panel has a switch between Original, Auto and One color, and switching is instant because nothing has to be reloaded.

One bug from this round is worth admitting. OBJ objects that use several materials get an array of materials rather than one, and the first version of the new code tried to dispose that array as if it were a single material. It threw m.dispose is not a function, and most car models silently failed to load. We caught it while measuring memory with a real 64 MB car model, which is a good argument for testing with real files instead of tidy test cubes.

Problem 2: hundreds of tiny meshes

The test model was a Lamborghini Revuelto OBJ: 64 MB on disk, about 445,000 vertices and 548,000 triangles. That's not extreme for a GPU. The problem was structure: the file is split into 1,063 object, group and material sections, and three.js turns each object into its own mesh. Every mesh costs at least one draw call per frame, with its own state changes. For a wallpaper that spins slowly at 60 FPS all day, issuing that many small draw calls is a lot of wasted CPU work.

Most of those objects shared a handful of materials. So at import, after materials are assigned, the engine now groups meshes that can safely be combined: same original material, same automatic material category, the same vertex attributes, no index buffer, no transform of their own and no material groups. Each group's vertex arrays are concatenated into one geometry, and the duplicate materials are disposed.

Result on the Lamborghini: the whole car now renders as 13 meshes in 15 draw calls per frame (the extra two are the floor shadow and the particles), with all 548,000 triangles intact. The whole import, including parsing a 64 MB text file and merging, took about 2.1 seconds. Wireframe mode and material switching still work on the merged meshes.

Problem 3: memory

Once geometry is uploaded to the GPU, three.js keeps the JavaScript copy of every vertex array by default. For a big model that's a lot of memory doing nothing. The engine now drops those CPU-side arrays right after upload, keeping only what's still needed: index data for wireframe, and the vertex count for non-indexed geometry (stored as a length-only placeholder, because three.js builds wireframe indices from the count). Bounding boxes are computed before the drop so culling still works.

Parsing a large OBJ also creates a lot of temporary garbage. The renderer now starts with V8's --expose-gc flag so the engine can ask for a collection right after a model loads, and the Python side trims the working set of the WebEngine helper processes a few seconds later.

With the Lamborghini loaded, the page process went from about 880 MB in the old build to around 540 MB. That's a real improvement but not the whole story. Qt WebEngine runs its GPU work inside the main process, and that process still used a few hundred megabytes more with the car loaded than without. Some of that is driver-side buffer memory we can't control from JavaScript. It's the honest number, so it's the one we're reporting.

Problem 4: settings that seemed to do nothing

This one was partly a design problem. The wallpaper pauses to save GPU when a maximised or fullscreen window covers the desktop, which is exactly what you want while working. But it also meant that if you opened the settings while your browser was maximised, every slider appeared to do nothing, because the wallpaper wasn't rendering and you couldn't see it anyway.

The settings window now has a live preview running the same engine, so every change shows up immediately whether the desktop is visible or not. One real bug hid behind this too: the fog slider genuinely did nothing on the model, because an old fix had turned fog off on every material to stop it "discolouring" models. Fog now applies to the model as it should.

What's left

Mesh merging only applies to simple, untransformed meshes, which covers most OBJ exports but not complex glTF scenes with hierarchies. A future version could merge those after baking transforms. And the GPU-side memory inside Qt WebEngine is worth a deeper look; it's the largest remaining cost, and it's mostly outside the engine's control.

If you want to try it, Ambient 3D is free. Import an OBJ with its .mtl and textures in the same folder and it should now look the way its author intended.