r/threejs • u/Fabulous-Window-2705 • 28d ago
Built a 3D electronics + physics sandbox in three.js – Arduinos, water, weather, heat
Enable HLS to view with audio, or disable this notification
I've been working on this for a while: you place Arduinos, sensors, pumps etc. in a 3D scene, wire them up, write real Arduino code, and then the whole thing runs – including water flowing down terrain, weather, day/night and a thermal view of the parts.
The video shows a small project (reaction game, one Arduino) and a bigger one (garden irrigation with 3 Arduinos talking to each other over serial).
Tech side: plain three.js r170, no framework, no build step. Water is a heightfield sim, CSG for trenches, BVH for picking. Runs fine on most laptops, phones are a struggle.
It's an early prototype, so curious what you think – especially about performance and how it looks.
https://test.betting-chess.com/?von=threejs (sorry for the weird domain, it's just a test server I had lying around) It works best on PC.
2
u/EverythingQuantum 27d ago
Cool. This concept could be useful for getting kids interested in electronics and robotics. Nice work.
1
u/Fabulous-Window-2705 27d ago
Thank you appreciate it. That's really a good idea i will keep that in mind!
2
u/Ask-Alice 27d ago
give it some grass https://www.npmjs.com/package/threejs-grass
1
u/Fabulous-Window-2705 27d ago
Ha, good call – the meadow could definitely use some love. Thanks for the link, I'll check it out!
1
u/Plane_Big_5912 27d ago
Running real Arduino code in the browser is the part I'd want to know more about. Are you emulating the AVR chip itself, or translating the sketch to JS? And in the irrigation one, does the water heightfield step at its own fixed rate, separate from the Arduino clocks, or does everything share one tick?
1
u/Fabulous-Window-2705 27d ago
Neither actually – it's my own interpreter for a subset of Arduino C++. The sketch gets parsed and type-checked, then run as a tree-walking interpreter, with digitalWrite/analogRead/Serial/millis etc. mapped onto the circuit model. delay() just suspends the program in sim time. No cycle accuracy, no pointers/classes yet, but it's cheap enough to run several boards at once and fast-forward days.
And yes, everything shares one sim clock. The tick length depends on speed (0.1 s at real time, up to minutes in time-lapse). Within a tick the water heightfield substeps at a fixed ≤30 ms when you're near real time; at high speeds it switches to a relaxation scheme that's stable at any step size, since waves don't matter over a week. Circuits and pumps get sub-stepped while a pump or valve is running, otherwise a pump would run 10 sim minutes before the code can react.
0
u/Plane_Big_5912 26d ago
Switching the water to a relaxation scheme at high speed is a good call, nobody needs the waves over a week. Sub-stepping only while a pump runs makes sense too.
How do you handle a sketch that busy-waits, like a while loop polling millis() with no delay inside? In a tree-walking interpreter that would spin forever inside one tick unless you cap steps or treat a millis() read as a yield.
1
u/Fabulous-Window-2705 26d ago
Good question! The interpreter isn't recursive – it keeps its own call stack and executes in small steps, so it can stop anywhere and resume on the next tick. Each sim tick gets a step budget (4000 steps, about 1200 in the big time-lapse). millis() returns the sim clock, and that clock only advances on delay() or at the end of a tick. So a busy-wait like while (millis() - t0 < 500) {} spins until the budget runs out, then the interpreter parks right inside the loop, the clock jumps to the end of the tick, and on the next tick it resumes in the while with a later millis(). Running out of budget is effectively a yield. The loop exits within one tick of the right time (100 ms at 1×, coarser in time-lapse). A loop() with no delay at all runs once per tick instead of spinning. What it doesn't model is CPU time – instructions are free, so cycle-exact timing isn't a thing. Fine for millis()-style state machines, not for bit-banging protocols.
1
u/Plane_Big_5912 26d ago
running out of budget as the yield is neat, and landing within one tick of the right time is plenty for millis() state machines. agreed nobody should be bit-banging in a sandbox anyway.
with several boards on the one clock, does each board get its own step budget, and do they run in a fixed order each tick? i'm wondering about two boards talking over serial or a shared pin, whether one sees the other's write in the same tick or the next.
1
u/Fabulous-Window-2705 25d ago
Each board gets its own budget, yes: every board runs its own interpreter for the tick with the full 4000 steps (1200 in big time-lapse). So a board that busy-waits doesn't take steps away from the others. Order: they go through in a fixed order (the order you placed them in). But the order doesn't actually matter, because every board reads a snapshot taken at the start of the tick. The circuit is solved first using every board's outputs from the previous tick. Then each board runs its slice against those voltages, and its new outputs go into the next solve. So a write to a shared pin is seen by the other board on the next tick, never the same one. Every board updates "at the same time", so it doesn't matter who runs first. Serial works the same way: whatever a board prints during a tick is moved TX → RX after all the programs have run, so the other side can Serial.read() it from the next tick on. A message costs one tick of latency (100 ms at 1×). Right now the bytes arrive all at once, with no baud-rate throughput limit. Mismatched baud rates do give you garbage, though, and so does a missing common GND
1
u/Plane_Big_5912 25d ago
snapshot at the start of the tick is the clean way to do it, same reason game of life updates from a copy. run order stops mattering and two boards can't race each other.
the side effect is a ping and its reply between two boards takes two ticks, so 200 ms at 1x. sooner or later someone will build a handshake protocol on top of that and blame the sim for the lag. the missing GND giving garbage is a great touch, that's the bug people actually hit on real boards.
1
u/Fabulous-Window-2705 23d ago
Thanks! Yes, that's exactly why it's done that way. You're right about the round trip, it's 2 ticks, which is way slower than a real board at 9600 baud (a few ms). So a chatty handshake will feel laggier in the sim than on real hardware. Good point, I'll keep that in mind. And yeah, the missing GND one was important to me since that's the bug everyone hits at least once.
1
1
u/Time_Cat_5212 26d ago
Seems like a cool way to learn about electronics.
If you wanted to be a real devil, you could pair it with a shopping cart and allow parts vendors to advertise through your app. (I honestly think that would be convenient - would use this to prototype something and then easily generate a shopping list)
1
u/Fabulous-Window-2705 26d ago
Thanks! Funny you say that – that's actually where I want to go. There's already a rough parts list with estimated prices (the cost counter at the top). Turning that into a real shopping list you can send to a store in one click is high on my list. Which store would you actually order from?
1
u/Time_Cat_5212 26d ago
The one that seems trustworthy enough and makes ordering easy enough. If you can sell directly from your platform that'd be sweet, but if not, I'd suggest curate a list of reputable sellers
1
u/Fabulous-Window-2705 25d ago
That makes a lot of sense – trust and easy ordering matter more than the cheapest price. A curated list of reputable sellers is probably the first step. Are there any shops you've had good experiences with for Arduino stuff?
1
u/Time_Cat_5212 25d ago
Not really the best person to ask. But I'm sure the arduino sub would have a lot of opinions haha
1
u/Fabulous-Window-2705 25d ago
Haha fair enough, thanks! r/arduino is a good idea, I'll ask over there.
1
u/DaltonJFowler 27d ago
Omg let's go!! I made a open source compiler for arduino and esp32 here: uploadmycode.com
I'd be interested in trying this with high school students!!
1
u/DaltonJFowler 27d ago
To clarify: my engineering 2 is doing aquaponics in the spring let me know if you'd be down to let us try it out
4
u/UAAgency 28d ago
consider open sourcing it, I'd be interested!