Transport for NSW publishes realtime data for the whole NSW train network, so I wanted a map where you could watch every train move around in real time.

The project is split into two parts. The backend is a simple Express server in TypeScript that talks to the TfNSW API, caches everything, and hands the frontend clean JSON. The frontend is React with Vite, using MapLibre for the map and three.js for the 3D trains. The frontend is deployed to Cloudflare Pages and the backend to my own VPS.
The data
TfNSW gives you two kinds of data. The static timetable is a GTFS zip with every route, stop, trip and track shape. The realtime feeds are GTFS Realtime protobufs: vehicle positions, trip updates (delays, cancellations, skipped stops) and service alerts.
The static zip is big, so the backend downloads it once a day and only keeps trips running around the current service day. A train day doesn't end at midnight, a train leaving at 11:50pm is still running at 12:30am, so I refresh it at 3:30am Sydney time instead.
The realtime feeds get polled with a short cache, so no matter how many people have the map open, TfNSW only gets hit once every 15 seconds. If a fetch is already in flight, other requests just wait on that same promise rather than starting another one.
Making trains move smoothly
This was the hardest part of the project. A GPS position only arrives every 30 seconds or so, and if you just draw the train where the GPS says, it teleports around the map. Interpolating in a straight line between two GPS fixes isn't much better, because trains cut across corners and through buildings.
What I ended up doing is ignoring the GPS for movement and using the timetable instead (if the train has a matching timetable). Each stop has an arrival and departure time, and the trip updates feed says how late the train is at each stop in realtime. Stops without a realtime update borrow the delay from their nearest neighbour, so a train that's 3 minutes late at Strathfield is assumed to still be 3 minutes late further down the line.
Then each stop gets projected onto the track shape from the static timetable, turning "at Central at 8:04" into "2,340 metres along this track at 8:04". Once the trip is just a list of times and distances along a line, finding where the train is right now is a binary search and a lerp.
3D trains
Once trains were moving nicely as icons, I wanted to see the actual trains when zoomed in. MapLibre lets you add a custom layer that shares its WebGL context, so I render a three.js scene inside it, using the map's camera matrix so the models line up with the map.

Each car is placed by sampling the track motion a few metres ahead of or behind the centre of the train, so on a curve every carriage follows the rails rather than the whole train being one stiff block.
My first version added a full clone of the model for every car, which meant hundreds of draw calls with the whole network zoomed out a bit. I switched to InstancedMesh, so every car of the same model is drawn in one call per mesh part, and fleets sharing a model share a batch.
The only part that can't be instanced is the destination screen on each driving car, because its text is different for every train, so those stay as their own meshes with a canvas texture.
Creating the 3D train models
I am terrible at 3D modelling, and I needed multiple different trains created, so I installed the Blender MCP addon and let Claude do the modelling. The addon lets Claude run Python inside Blender and take viewport screenshots, so it can build a model step by step and actually look at what it has made. I originally tried some of the photo to 3d model models, but they are not quite there yet.
The process was as follows: for each train, Claude first researches it to get the real dimensions and a set of allowed reference photos from Wikimedia Commons. From that it writes a PROMPT.md for the modelling run, describing what the train looks like from front to back, the dimensions, and the conventions every model has to follow.
Every driving car also has a flat rectangle, which is the mesh the app finds and swaps for the canvas texture showing that train's destination.

After modelling and exporting as GLBs, they go up to my CDN, which the app loads from when needed.
3D terrain
A fair chunk of the NSW rail network is not flat. The South Coast line in particular squeezes between the escarpment and the ocean, and the Blue Mountains line climbs over a kilometre, so I added 3D terrain to enhance this app. MapLibre supports this. It just needs a raster DEM source, which is a set of image tiles where each pixel's colour encodes an elevation. I used the free AWS terrain tiles in Terrarium encoding.
Each base map gets its own sky and fog colours so the horizon blends in when the map is tilted.

The satellite imagery comes from Esri.
Wrapping up
There's a lot I didn't cover here, like following a selected train with the camera, colouring train icons by line and showing a dot when they're running late, 3D buildings, and the timeline panel that shows realtime stop times and cancelled stops.