Skip to content
bg2 engine

ObjParser

Parses .obj text files into PolyList[] arrays, extracting vertex positions (v), normals (vn), texture coordinates (vt), faces (f), groups (g) and material names (usemtl).

import ObjParser from 'bg2e-js/ts/db/ObjParser';
const parser = new ObjParser(objText);
console.log(parser.polyListArray); // Array of PolyList objects

See also: index, obj-loader-plugin

ObjParser(objText: string): Initializes the parser with OBJ text content. Tokenizes lines by first character to dispatch parsing (v → vertex, vn → normal, vt → texcoord, g → group name, usemtl → material name, s → smoothing groups), f → face/geometry). Supports line continuation with backslash (\). Ignores comment lines starting with #.

PropertyTypeDescription
polyListArrayPolyList[]Read-only accessor returning the parsed geometry arrays. Each PolyList corresponds to a group or mesh in the OBJ file.
  • Vertex positions, normals and texture coordinates are stored internally in separate arrays
  • Faces define geometry through vertex/normal/texcoord indexing with support for polygons via triangulation
  • Group names from g statements are stored in the PolyList’s name property
  • Material names from usemtl statements are stored temporarily and can become group/PolyList names if PolyList has no name yet
  • .mtl files are parsed but not loaded — ObjLoaderPlugin marks MTL loading as a TODO