Need a Rhino3dm package that can be used directly in Unity WASM

The following content comes from Google Translate:

We are the genuine users of Rhino (non-educational version), and now we want to make architectural design software based on RhinoCommon or Rhino3dm.

We hope that users can use our software by directly opening the browser, so the options we can directly use are:

  1. Rhino3dm.js

But because if we use this, it means that many of the basic interface functions originally provided by Rhino require us to develop from scratch, which is a lot of work. We expect it will take at least half a year.

But right now we have another choice, that is Unity WASM. Unity’s rich ecology allows us not to spend time for too many interface details.

We tried to use the .Net version and Js version of Rhino3dm. We simply used Unity to call Rhino3dm and drew a line. The Unity compilation can pass, and the release to WASM can also pass, but it fails when running with a browser.

Then we tried to study the principle of Unity il2cpp, and studied the sample code provided by Unity for writing cross-platform native plug-ins:

According to the document description, this method should be possible.

Then I studied the source code of librhino3dm_native and tried to follow the method described in the above document, but found that there were too many dependencies and too many compilation options, and finally failed.

But because you provide the WASM version of the Rhino3dm library, I still think this is feasible, but I haven’t found the right way.

But I feel that through the sample code in the link above, you should be able to quickly implement the Rhino3dm package that Unity WASM can use.

Chinese:

我们是 Rhino 的正版用户(非教育版),现在希望基于 RhinoCommon 或 Rhino3dm 做建筑设计软件。

我们希望用户可以通过直接打开浏览器的方式来使用我们的软件,因此我们直接可以用的选项有:

  1. Rhino3dm.js

但因为如果我们使用这个,就意味着很多本来由 Rhino 提供的基础界面基础功能,都需要我们从头开始开发,这是一个很大的工作量。我们预计至少需要半年时间。

但眼下我们还有另一个选择,那就是 Unity WASM,Unity 丰富的生态使得我们不用为了过多的界面细节而花费时间。

我们尝试了使用 Rhino3dm 的 .Net 版和 Js 版,简单使用 Unity 调用 Rhino3dm 画了条线,Unity编译可以通过,发布至 WASM 也可以通过,但用浏览器运行时失败。

接着我们尝试研究了 Unity il2cpp 的原理,并且研究了 Unity 提供的编写跨平台 native 插件的示例代码:

按照文档描述的,这个办法应该是可以的。

接着我又研究了 librhino3dm_native 的源码,尝试按照上面文档中描述的办法去做,但发现依赖项过多,编译选项太多,最终依然失败了。

但因为你们提供了 WASM 版的 Rhino3dm 库,所以我仍然认为这是可行的,只是我目前没找对方法。

但我感觉你们通过上面链接中的示例代码,应该可以很快实现 Unity WASM 可以使用的 Rhino3dm 包

Moved to Developer category

We don’t have a good answer for this yet, but are looking into it. We don’t know what the correct way to develop a Unity app with a wasm dependency and have started to look online to see how others are doing it.

Yes, there is no perfect solution, I mean there is no way to support all APIs, and APIs like ChangeBasis seem to be difficult to support. In my mind, because you already have the wasm version of the API, at least you should be able to provide a unity-ready API that is the same as the wasm version of the API, and I can indirectly support the API that cannot be supported in other ways.

Do you know of any resources that show how to use WASM libraries in Unity? My next step is to open a post on their forums.

edit: I posted it here: https://forum.unity.com/threads/developing-webgl-app-with-wasm-or-net-dependency.1240315/

Sorry I only saw it now.

The whole process looks complicated, but in fact each step is very simple, so simple that I feel that if you tell me how to compile your WASM package, I can compile a package that can be used by Unity.

I have implemented calling your WASM package in Unity.

The following figure is the directory structure after Unity is compiled into a WebGL package

The Hashcode method is implemented, and the purpose is to pass its return value as an ID to Unity. Unity does not save anything in essence, but only saves an ID, which is used as the basis for the communication between JS code and Unity code

The following figure is the JS interoperability mechanism provided by Unity, all I did is to convert the Rhino cross-platform method call into a call to the WASM package you provided.

The one that looks more complicated is HEAP32[from >> 2] , related resources are: WebGL:与浏览器脚本交互 - Unity 手册

The following figure is the code of the package that calls Rhino3dm in Unity

The following picture is more critical. I modified the source code of Rhino3dm appropriately so that it no longer calls the UnsafeNativeMethods.ON_LineCurve_New method, but instead calls the ON_LineCurve_New3 method, which will be translated by Unity into the JS interop method I just mentioned.

Without very complicated theoretical knowledge, the ultimate goal is to use Unity’s cross-platform mechanism to convert Rhino method calls into calls to the Rhino3dm WASM package, which is far less difficult than compiling the WASM package.

I understand that what you need to do is, according to the content of GitHub - Unity-Technologies/NativeRenderingPlugin: C++ Rendering Plugin example for Unity, compile your project source code compiled into a WASM package in the form of this link, you will get dll or lib file, this lib file can be used directly for Unity

1 Like

Thank you very much. We will take a look and try this out.