Exploration on Unity WebGL

Reposted from: https://www.cnblogs.com/yaukey/p/unity_webgl_explore_1.html

After looking up the official information of Unity, if we need to use WebGL, we need to face the following challenges:

  1. Native Plugin: That is to say, various native plugins (local machine code libraries compiled by C/C++, etc.), our challenge is to use SLua.
  2. Multi-threading: The WebGL side cannot support any multi-threaded code, because JavaScript does not have a multi-threaded implementation, and libraries such as System.Threading used on the C# side will not be compiled into corresponding js codes in the end.
  3. Network module: traditional Socket cannot be used, WebSocket or xxx must be used, System.Net, especially UnityEngine.Net.Sockets are not implemented on the WebGL side, so they will not be compiled and converted correctly; WWW and UnityWebRequest can be used in Unity, or Use the new Unity Networking API that supports WebGL; or use WebSockets and WebRTC directly in JavaScript to implement network layer functions.
  4. Rendering: WebGL's graphics api is based on OpenGL ES 2.0; GI only supports Baked GI (we don't use it); Procedural Materials doesn't support it (we don't use it); Linear Rendering doesn't support it (we don't use it); MovieTextures doesn't support it (we don't use it ); WebGL Shader code restrictions: At present, it is understood that only constants, circular index values ​​or unions are supported in the shader code as the index for accessing arrays and matrices. The only exception is that any uniform can be used when accessing uniforms in Vertex Shader Expressions, in addition to the restrictions on loops, cannot use methods other than "counting loops - assign a constant value when initializing a variable, increase or decrease a constant value each time looping", and do not support while loops. (At present, it seems that we have not used the subscript expressions of arrays or matrices, nor used complex loops, and we may need to check carefully later).
  5. Almost more than half of the APIs in Audio do not support it, and compatibility modifications are required later, which should be a lot of trouble.
  6. Others are not considered for the time being. The above items directly determine whether we can port out first, and the efficiency issue is not considered first.
  7. Start with Native Plugin, Lua is the first hurdle that needs to be passed. The official has given two good documents: WebGL: Interacting with browser scripting  and  the underlying plug-in in Unity WebGL . WebGL converts all C# code into C++ through IL2CPP, so that you can use the LLVM-based Emscripten toolchain to convert all The C++ code compiled into asm.js-based JavaScript code, so that it can run on browsers that support Html5.

      We use the SLua plugin, so I now need to use the Lua source code to participate in the compilation and packaging process. I am very glad that our project decided to upgrade from the original LuaJit to Lua 5.3 after everyone met and discussed this year. If the compilation of the LuaJit project itself generates a large amount of assembly code for the target platform, it is extremely platform-specific, so Even if you use the source code of LuaJit, you cannot use WebGL. You still need to use the source code of Lua 5.1 or 5.3 directly.

      Create a new empty Unity project, import the SLua plug-in, switch to the WebGL platform, create a new folder WebGL in Plugins, create a new C code file such as: lua.dll.c, and then decompress the latest version of Lua5.3 source code to a local Directory: (LuaSrcDir), all codes are in (LuaSrcDir), all codes are in (LuaSrcDir)/src, copy slua.c, but exclude lua.c and luac.c.

      Add the following to lua.dll.c:

    copy code

    #define LUA_COMPAT_5_1
    #define LUA_COMPAT_5_2
    
    // Lua source code only, relative .
    #include “$(LuaSrcRelativePath)/src/lapi.c”
    #include “$(LuaSrcRelativePath)/src/lauxlib”
    #include “$(LuaSrcRelativePath)/src/lbaselib.c”
    // Add all lua source file *.c, exclude lua.c, luac.c.
    //#include "$(LuaSrcRelativePath)/src/lua.c"
    //#include "$(LuaSrcRelativePath)/src/luac.c"
    
    #include “$(LuaSrcRelativePath)/src/slua.c”

    copy code

      Note: All the above content is to add Lua source files, excluding header files, which precompiled macros to use at the beginning depends on your project.

    In addition, due to the backward compatibility of Lua 5.3, if LUA_COMPAT_5_1 is defined, LUA_COMPAT_MODULE will be defined, then the compatible implementation will be compiled: luaL_findtable, and SLua has written an extra copy for compatibility, so you can delete slua.c at this time implementation in , otherwise a redefinition error will occur when compiling.

      Next, add the precompiled macro LUA_5_3 in the Unity Player Setting to switch SLua to the 5.3 implementation version, and then directly add a build list to a sample scene, BuildAndRun, and you can see that the Demo scene of SLua runs correctly in the browser up.

      So far, the Native Plugin part of Lua has been completed, and it can go down.

Guess you like

Origin blog.csdn.net/u014805066/article/details/103785891