This guide illustrates the process of developing a 3D object viewer tailored for WebAssembly. Notably, it offers the flexibility to select the rendering backend, either OpenGL or WebGPU, during runtime.
The additional steps required are:
- Update
CMakeLists.txtso that theVTK::RenderingWebGPUcomponent is requested when finding VTK. - Link your targets with
RenderingWebGPU. There is no problem linking to bothRenderingWebGPUandRenderingOpenGL2. - Pass "--vtk-factory-prefer RenderingBackend=WebGPU" as command line arguments when initializing the VTK WASM module to select the WebGPU backend at runtime.
- Call
vtkObjectFactory::InitializePreferencesFromCommandLineArgs(argc, argv)in your main function before creating any VTK object. This will ensure that the rendering backend preference is set based on the command line arguments.
emcmake cmake \
-G Ninja \
-S /path/to/vtk/Examples/Emscripten/Cxx/ConeMultiBackend \
-B out/build \
-DVTK_DIR=/path/to/where/vtk/wasm/was/built
cmake --build out/build
# Configure
docker run --rm -it \
-v"$PWD":/work kitware/vtk-wasm-sdk:latest \
emcmake cmake -GNinja -S /work -B /work/build -DVTK_DIR=/VTK-install/Release/wasm32/lib/cmake/vtk
# Build
docker run --rm -it \
-v"$PWD":/work kitware/vtk-wasm-sdk:latest \
cmake --build /work/builddocker run --rm -it `
-v"$PWD":/work kitware/vtk-wasm-sdk:latest `
emcmake cmake -GNinja -S /work -B /work/build -DVTK_DIR=/VTK-install/Release/wasm32/lib/cmake/vtk
# Build
docker run --rm -it `
-v"$PWD":/work kitware/vtk-wasm-sdk:latest `
cmake --build /work/buildcd out/build
python3 -m http.server 8000
Open your browser to http://localhost:8000