Any success with using Rust wasm + BJS?

Hi,

I would like to try something new with BJS, and instead of having multiple languages, the goal would be to code in Rust, and drive BJS as much as possible from wasm instead of using TS/JS stacks.

So, as of 2024, is it something that could be mainstream or is it complicated?

Again, the goal is to reduce the overall stack complexity and benefit from a more typed system.

I would like to lower the usage of JS/TS to a bare minimum.

Thanks guys,

Vite setup, maybe better setup if you want to avoid webpack.
.GitHub - jeremy-coleman/normal-rust: A small Rust / Webassembly project to calculate Normals
Has actual logic, as you desire
.GitHub - jeremy-coleman/babylon-wasmpack

There are a couple other examples i can link you if those dont work for you, just reply or @ me so i get notified. The webpack setup has the benefit that hot reloading works, but i had found some bugs with the webpack plugin that doesnt work with cargo workspaces. Parcel v1 worked really well too, but for whatever reason they screwed up v2. Ive been meaning to setup a repo with bevy ecs and a bjs renderer, maybe il do that sometime

2 Likes

Thanks Jeremy,
I read your repos, thanks for it, it puts me on track.

There is still a lot of TS/JS, as I expected… Will dig into it nonetheless.
A rust crate that would bind to BJS would serve its purpose…

Hi Jeremy, reading your answer again, I skipped an info : you mean you can drive BJS from bevy ?

you can use bevy_ecs yes. its just an ecs system like entt/flecs. you’d still need to sync the transforms to your scene using javascript or you can use wasm bindgen and register all the babylon classes so you can code against babylon in rust, but thats terrible dont do it.

@jeremy-coleman
Thanks,
Don’t you think it’s doubling the work and in the end reducing the app performance, battery consumption?

It’s appealing to use an ecs system but if the cost is “reduced everything else”, yeah… :thinking:

If your goal is reliable, clean code, I think bevy is the way to go.

But whether you really need a rust wasm depends on many more factors.


The first consideration is performance.

Making it all wasm can have a bad performance impact.
In particular, calling js types in rust involves a huge overhead.

This is why rendering engines built on javascript (babylon.js, three.js, Aframe, PlayCanvas) perform better in browsers than those built with native engines wasm (Unity, bevy).


The second is the size of the builds.

Unlike javascript, wasm does not allow you to split a build into multiple files, like javascript’s async imports.

This creates a bad user experience on initial page load, for example in games that use Unity WebGL builds.


Among javascript-based 3D rendering engines, babylon.js is almost the only one based on typescript, with many bugs being caught at compile time.
And bugs reported in the forums are fixed quickly, within a week at the earliest.

For these reasons, I recommend using babylon.js for projects that use typescript to create reliable 3D web applications with a typed system.

On the other hand, WASM can solve a number of performance issues when used locally

If a task is very CPU intensive and the required input is not complex in shape, accelerating with wasm can provide an improvement of several orders of magnitude.

This project is using the rust wasm to multithread certain logic. If performance considerations are part of your reason, you may want to consider developing something similar to this repository.

1 Like

Thanks,
both of you answered the question, it looks more of an hassle. Not prime time for this path yet.
I will keep the rendering in js but all my app logic in rust with bindgen because this is what matters the most for me right now.
This way, it avoids a lot of marshalling ↔ json, better confidence in the stack and still good performance on mobile + web.

Thanks a lot guys, really helpful!

I’ve seen them fixed in an hour! Not necessarily on NPM that fast.

I enjoy the cross language discussion. I’ve loaded javascript in Java and it was always a fragile experience. Of course generated javascript from more languages, but curious if somebody will get good performance on Rust + BabylonJS. Thanks for sharing.

1 Like

I just watched a video on Bevy ECS and I love the programming model. Being able to use Bevy ECS with BabylonJS for rendering would be very cool.