From One Photo to a Rotatable 3D Model of Fudan's Guanghua Towers in the Browser

阅读中文版 →

A few days ago I wanted to do one small, concrete thing: turn Fudan’s Guanghua Towers (光华楼), a building I see every day, from a single photo into a 3D model you can drag, rotate and zoom in the browser.

It turned out more interesting than I expected. It was not something you can sum up as “have AI turn a picture into 3D”. It was more like a small piece of reverse engineering: first read the structure of the building, then write that structure as parametric geometry, and only then deal with WebGL rendering and putting it on a web page.

Try the model first. Drag with the mouse to rotate and scroll to zoom; on a phone, use one finger to rotate and two to zoom.

Photo-guided reconstruction · not a survey / BIM model Open full screen ↗

Step one is not modelling, but taking the building apart

A single photo cannot recover a building’s true 3D shape. What the back looks like, how deep the building really is, what focal length the photo was taken at: none of these has a unique answer. So this is not surveying. It is photo-guided reconstruction: keep the visible parts, the proportions and the overall silhouette as close to the reference photo as possible, and fill in what the photo cannot show with reasonable guesses.

I started by breaking the photo down into its most important masses: the low podium, the two towers on the left and right, the tall central hall, the connecting block halfway up, the lattice at the top of the towers, the entrance steps, and the trees and front plaza, which are only there to give a sense of scale.

This step matters far more than how much detail you add. If the width-to-height ratio of the towers, the gap between them, the podium height or the position of the central connector is wrong, no number of windows added later will make it look like Guanghua.

Step two: parametric geometry, not hand-pulled meshes

The main model is generated by a Python program. The basic building blocks are very plain: box(), beam(), cylinder() and a few irregular tree crowns. Most of the building’s parts — walls, stone columns, glass windows, window frames, floor beams — come down to boxes with a position and a size.

For example, the tower facade is not a “window texture” stuck onto a surface. The program actually loops over floors and bays to generate the glass, mullions and beams:

for row in range(n_floors):
    z = z0 + row * floor_height
    for col in range(n_bays):
        x = tower_center - opening / 2 + (col + 0.5) * bay_width
        box("tower / glazing", glass_material,
            (x, facade_y, z + floor_height * 0.57),
            (bay_width - 0.11, 0.095, floor_height - 0.60))

The advantage is that when the view swings from the front to an oblique angle, the window panes, the stone columns at the tower corners and the structure at the top are still real 3D geometry, rather than a flat texture that gives itself away.

The final version has about 115,832 triangles, 158,352 vertices and 121 named objects. For a small architectural piece on a web page, that is detailed enough, but not so heavy that it needs complex LOD.

Step three: export straight to GLB

At the end, the modelling script writes out .glb, the binary form of glTF 2.0. I convert the Z-up coordinates used during modelling into the Y-up coordinates glTF usually expects, then pack the meshes, materials, accessors and binary buffer into a single file.

This step matters because GLB is a very good intermediate format for 3D in the browser: Blender can import it, Three.js can read it directly, and it is easier to manage than formats like OBJ, where geometry and materials live in separate files.

The raw model is about 5.2 MB. My first version was a fully offline single-file HTML: the GLB, the reference photo and the rendering code were all converted to Base64 and stuffed into the HTML, so the file came to about 7.2 MB. The upside was that one download was enough to run it. The downside was just as obvious: far too heavy for a blog.

Step four: the blog version does not ship 5 MB of triangles

The raw GLB is about 5.2 MB. It hard-codes every final triangle, normal and index, which suits Blender and general 3D software, but is a bit wasteful for this model: the building was generated from a large number of repeated boxes, beams and cylinders in the first place.

So the blog version takes a different approach: instead of shipping the resulting mesh, ship the modelling instructions. I recorded the 5,206 primitive commands the generator executes, for example:

["b", material_id, center, size]       # box
["m", material_id, start, end, width] # beam
["c", material_id, start, end, radius]# cylinder

The instruction JSON is about 560 KB raw, only about 56 KB after gzip, and still only about 75 KB after Base64 encoding. When the HTML opens, it decompresses the JSON and rebuilds the geometry in the browser. The 4,550 boxes that make up the building are not 4,550 separate draw calls; they are merged by material into InstancedMesh objects.

The core logic looks roughly like this. The compressed data is split into several static chunks only so they can be deployed with the blog as ordinary text files; after loading, they are joined back in order into the same gzip + Base64 string:

const DATA_URLS = [
  '/interactive/guanghua-data/gh_model_part_00_0.txt',
  '/interactive/guanghua-data/gh_model_part_00_1.txt',
  // ...remaining data chunks
  '/interactive/guanghua-data/gh_model_part_03.txt',
];

const b64 = (await Promise.all(
  DATA_URLS.map(url => fetch(url).then(r => r.text()))
)).join('');

const raw = Uint8Array.from(atob(b64), c => c.charCodeAt(0));
const ds = new DecompressionStream('gzip');
const json = JSON.parse(new TextDecoder().decode(
  await new Response(new Blob([raw]).stream().pipeThrough(ds)).arrayBuffer()
));

const mesh = new THREE.InstancedMesh(unitCube, material, matrices.length);
matrices.forEach((matrix, i) => mesh.setMatrixAt(i, matrix));
scene.add(mesh);

As a result, the interactive viewer plus the compressed model description comes to only about 88 KB, and the building geometry still comes from the same parametric model definition. Non-essential scenery such as tree crowns is replaced on the web side with lighter instanced low-poly geometry, in exchange for fewer draw calls and steadier performance on phones. Three.js and OrbitControls are loaded as modules from a CDN.

After that it is standard Three.js: PerspectiveCamera for the perspective camera, OrbitControls for dragging and zooming, HemisphereLight + DirectionalLight for basic architectural lighting, plus three preset camera positions, auto-rotation and a night-mode toggle.

I deliberately kept the UI simple. On a blog, an interactive model should be part of the article first, not a standalone piece of “3D software”.

Step five: put just one iframe in the Astro post

Embedding it in the blog was actually the easiest step. The standalone viewer lives at public/interactive/guanghua-building.html, and the compressed modelling instructions are split into a few small static data chunks under public/interactive/guanghua-data/. That way the deployment does not need to carry the 5.2 MB GLB, and the article only contains:

<iframe
  src="/interactive/guanghua-building.html"
  title="Interactive 3D model of the Guanghua Towers, Fudan University"
  allow="fullscreen"
  style="width:100%;height:720px;border:0"
></iframe>

I prefer this structure to putting Three.js code directly into the article page, for a simple reason: the article and the 3D app are decoupled. The blog handles layout, SEO and text; the viewer handles WebGL, input events and full screen on its own. If I later want to switch renderers, swap the model or even move to WebGPU, I will not need to touch the article system.

What is the model still missing?

The biggest limit is not rendering but input. With only one front-facing photo, the back, the depth and the roof details can only be inferred. If I ever want to turn this into something closer to a digital twin, I would first add side and back photos, satellite images or floor-plan dimensions, and only then think about extras like PBR materials, realistic glass and environment maps.

But as a small experiment going from a photo to parametric geometry to real-time rendering in the browser, I am quite happy with where it is now: it is not a picture of the Guanghua Towers, but a spatial object that sits right in a web page and that readers can turn around and look at for themselves.