Bg2LoaderPlugin
Loads .bg2 and .vwglb model files using the bg2io WASM library.
import Bg2LoaderPlugin from 'bg2e-js/ts/db/Bg2LoaderPlugin.ts';import Loader, { registerLoaderPlugin } from 'bg2e-js/ts/db/Loader.ts';
registerLoaderPlugin(new Bg2LoaderPlugin({ bg2ioPath: "bg2io/" }));
const loader = new Loader();const drawable = await loader.loadDrawable("../resources/cubes.bg2");Supported extensions: ["bg2", "vwglb"]
Supported resource types: PolyList, Drawable, Node
See also: index, loader-plugin
Constructor
Section titled “Constructor”constructor({ bg2ioPath, preferedDrawableFormat, materialImportCallback })| Parameter | Type | Default | Description |
|---|---|---|---|
bg2ioPath | string | null | "bg2io" | Path to the bg2io WASM files directory. Pass "dist/" or "./" when serving from Vite dev server output folders. Only needed if bg2io is not in the default /bg2io location. |
materialImportCallback | (matData: any) => any | undefined | — | Callback invoked on each material parsed from the file. Return a modified copy to transform materials at load time, e.g., replace legacy material properties or inject custom shaders. |
NOTE: The
bg2ioPathshould point to the directory where thebg2io.jsandbg2io.wasmfiles are located in your final distribution. If you are using Vite, you can use thecopyBg2eAssetsplugin to copy these files to a specific directory in yourdistfolder, and then setbg2ioPathto that directory name (e.g.,"bg2io/"if you copy them todist/bg2io/). If you use the default value ofbg2ioPathin thecopyBg2eAssetsplugin, which isbg2io/, then you can omit thebg2ioPathparameter since it defaults to"bg2io/".
Material Import Callback
Section titled “Material Import Callback”The materialImportCallback lets you transform material data before it is applied to models. It receives the raw parsed material JSON and returns the modified version:
import Bg2LoaderPlugin from 'bg2e-js/ts/db/Bg2LoaderPlugin.ts';
registerLoaderPlugin(new Bg2LoaderPlugin({ bg2ioPath: "dist/", materialImportCallback: (mat) => { // Replace class-based materials with type-based ones if (!mat.type && mat.class) { mat.type = mat.class; delete mat.class; } return mat; }}));Bg2LoaderPluginlazy-loads the bg2io WASM wrapper and caches it globally- For file version 1.4 backward compatibility, materials with a
classproperty are automatically converted totype - When loading PolyList resources, all polyLists from the file are returned as a single array