Skip to content
bg2 engine

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({ bg2ioPath, preferedDrawableFormat, materialImportCallback })
ParameterTypeDefaultDescription
bg2ioPathstring | 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 | undefinedCallback 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 bg2ioPath should point to the directory where the bg2io.js and bg2io.wasm files are located in your final distribution. If you are using Vite, you can use the copyBg2eAssets plugin to copy these files to a specific directory in your dist folder, and then set bg2ioPath to that directory name (e.g., "bg2io/" if you copy them to dist/bg2io/). If you use the default value of bg2ioPath in the copyBg2eAssets plugin, which is bg2io/, then you can omit the bg2ioPath parameter since it defaults to "bg2io/".

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;
}
}));
  • Bg2LoaderPlugin lazy-loads the bg2io WASM wrapper and caches it globally
  • For file version 1.4 backward compatibility, materials with a class property are automatically converted to type
  • When loading PolyList resources, all polyLists from the file are returned as a single array