Enabling Unity WebGL

@Shehzan_Mohammed As promised on our meeting, I will describe here our experience with trying to enable Cesium for WebGL.

We use a customized version of the Cesium 1.20 package to create the WebGL build on our CI server. We have applied that change to make it possible to build with WebGL target from our Linux server.

As a first step, we enabled C++ Multithreading support for WebGL in the Unity project, as the WebGL build fails otherwise.

Next, we configured our server to use the needed COEP / CORP / COOP headers to make C++ multithreading possible. This was not enough, as we were including some external javascript in our index page, and these were failing to load because of the COEP. This was solved by adding the crossorigin="anonymous" attribute within the script tag.

But our app still fails to load. More specifically, it tries to load the url https://ourserver.com/undefined, but fails. That is a resource that was not being fetched before these changes, and we are still trying to figure out why that happens. From that ‘undefined’, it seems that the url was formed from an undefined javascript variable.

I will update here further if I have more information.

More specifically, it tries to load the url https://ourserver.com/undefined, but fails.

I have found why that happens. That was actually the fetch of the worker javascript file.
We use a customized index.html. We had to add the following line in there:

      var config = {
        ...
#if USE_THREADS
        workerUrl: buildUrl + "/{{{ WORKER_FILENAME }}}" + cachehash,
#endif

That problem probably won’t occur for someone that uses Unity’s default index.html page.

Another problem was that a third party javascript library would fail loading: it tries to access the window object, which is undefined. It seems that the worker.js file loads that script too, where there is no window context. This was fixed by adding the following line to jslib before any window access occurs:

    if (!ENVIRONMENT_IS_WEB)
      return;

We are not done yet. This time our app loads further, but we get a stack overflow somewhere. To be investigated further.

Please note that all this happens before any Cesium is executed at all. These problems are a consequence of enabling multithreaded C++ support in our app.