BGE/Parsers
Alias: BGE.Parsers
Static Methods
parseOBJFile(filePath: string): BGE.Model3d
Loads and parses a Wavefront .obj file's geometry, plus its texture if the file references one via mtllib/map_Kd (see parseOBJContent's doc comment).
Parameters
filePath(string)
Returns
parseOBJContent(content: string, baseDir?: string): BGE.Model3d
Parses the geometry of a Wavefront .obj file's content: v vertices, vt texture coordinates, and f faces, fan-triangulating any face with more than 3 vertices (a quad or n-gon). vn indices in a face line are read only far enough to skip over them correctly - per-vertex normals aren't retained, since Model3dFace only supports one flat normal per face; every face's normal is instead computed from its own vertex geometry (Model3dFaceOps.create()'s default when no normal is given), the same as an STL face with a missing/zero normal.
vt UV coordinates ARE retained now (issue #89): a face where every corner has a vt index (v/vt or v/vt/vn) gets a Model3dFace.Texture with normalized (0..1) UV points; a face where any corner lacks one (v//vn or bare v) gets no Texture at all, falling back to flat-color rendering for that face only.
mtllib <name>.mtl (first occurrence only, matching this parser's o/g handling) is resolved relative to baseDir and read from disk to find the first map_Kd line, which becomes the returned model's texturePath (also resolved relative to baseDir) - only when baseDir is given (parseOBJFile always supplies it; a bare parseOBJContent(content) call, e.g. from a unit test with no real file on disk, skips mtllib/map_Kd resolution entirely and just parses geometry). usemtl is still ignored - a model has at most one texture in this engine's current scope.
Parameters
content(string) — the raw text content of a .obj filebaseDir(string, optional, default: "\"\"") — the directory the .obj file lives in, for resolving mtllib/map_Kd paths; "" skips that resolution entirely
Returns
resolveOBJFaceVertices(
faceTokens: Array.<string>,
vertices: Array.<BGE.Math.Vector>,
): dynamic
Resolves a face line's v/v/vt/v//vn/v/vt/vn tokens (tokens[1..]) to the actual vertex positions they reference.
Parameters
faceTokens(Array.<string>) — the full tokenized face line, including the leading "f"vertices(Array.<BGE.Math.Vector>) — every vertex parsed so far
Returns
dynamic
resolveOBJFaceTexCoords(
faceTokens: Array.<string>,
texCoords: Array.<BGE.Math.Vector>,
): dynamic
Resolves a face line's per-corner vt index (the 2nd /-separated part of each v/vt/v/vt/vn token) to the actual UV points they reference. Returns an EMPTY array the moment any corner is missing a vt index or an already-parsed texCoord
- callers compare the returned array's length against the vertex count to know whether every corner resolved.
Parameters
faceTokens(Array.<string>) — the full tokenized face line, including the leading "f"texCoords(Array.<BGE.Math.Vector>) — every vt UV point parsed so far
Returns
dynamic
appendOBJFace(
faces: Array.<BGE.Model3dFace>,
faceVertices: Array.<BGE.Math.Vector>,
faceTexCoords: Array.<BGE.Math.Vector>,
): void
Fan-triangulates a face's already-resolved vertices (3 for a plain triangle, more for a quad/n-gon) and appends the resulting triangle(s) to faces. faceTexCoords is fanned the same way when it has one UV point per vertex (i.e. every corner resolved a vt - see resolveOBJFaceTexCoords) - otherwise no triangle from this face gets a Texture.
Parameters
faces(Array.<BGE.Model3dFace>)faceVertices(Array.<BGE.Math.Vector>)faceTexCoords(Array.<BGE.Math.Vector>)
Returns
void
resolveOBJIndex(indexStr: string, count: integer): integer
Resolves a single (1-based, or negative/relative per the .obj spec) OBJ index string to a 0-based index into an array of the given count.
Parameters
indexStr(string)count(integer) — the current length of the array this index is into
Returns
integer
resolveOBJTexturePath(
baseDir: string,
mtllibName: string,
): dynamic
Reads baseDir/mtllibName from disk and returns the resolved path to its first map_Kd texture, or invalid if the file can't be read or has no map_Kd line.
Parameters
baseDir(string) — the .obj file's own directorymtllibName(string) — the mtllib line's filename, as written in the .obj
Returns
dynamic
extractMapKdPath(mtlContent: string): string
Finds the first map_Kd <path> line in a .mtl file's raw content. Per the Wavefront MTL spec, map_Kd may carry option flags (e.g. -s 1 1 1, -o 0 0 0, -clamp on) before the filename, so the filename is taken as the LAST token on the line rather than assuming it's always immediately after map_Kd - a bare map_Kd diffuse.png still works the same way, since its last token is the filename too.
Parameters
mtlContent(string) — the raw text content of a .mtl file
Returns
string— the path, or "" if no map_Kd line was found
joinObjPath(baseDir: string, relativePath: string): string
Joins a base directory and a relative path, regardless of whether baseDir already ends with a "/".
Parameters
baseDir(string)relativePath(string)
Returns
string
parseSTLFile(filePath: string): BGE.Model3d
Parameters
filePath(string)
Returns
parseSTLAsciiFile(content: string): BGE.Model3d
Parameters
content(string)
Returns
parseSTLBinaryFile(fileBytes: roByteArray): BGE.Model3d
Parameters
fileBytes(roByteArray)
Returns
stlStringToFloat(input: string): float
Parameters
input(string)
Returns
float