New type definitions for obj-file-parser package

This commit is contained in:
Ben Coleman
2018-06-24 18:35:06 +01:00
parent 8cfe3b2c5a
commit 4b6657c2f3
4 changed files with 110 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
// Type definitions for obj-file-parser 0.5
// Project: https://github.com/WesUnwin/obj-file-parser#readme
// Definitions by: Ben Coleman https://github.com/benc-uk
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export default class Parser {
constructor(fileContents: any, defaultModelName?: any);
parse(): ObjFile;
}
export interface ObjFile {
models: ObjModel[];
materialLibraries: any[];
}
export class ObjModel {
name: string;
vertices: {x: number, y: number, z: number}[];
textureCoords: {u: number, v: number, w: number}[];
vertexNormals: {x: number, y: number, z: number}[];
faces: Face[];
}
export class Face {
material: any;
group: string;
smoothingGroup: number;
vertices: FaceVertex[]
}
export class FaceVertex {
vertexIndex: number;
textureCoordsIndex: number;
vertexNormalIndex: number;
}
@@ -0,0 +1,51 @@
import ObjFileParser, { ObjModel, ObjFile } from 'obj-file-parser'
let testObjFile = `
# cube.obj
# Example object
g cube
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
g cube-faces
f 1//2 7//2 5//2
f 1//2 3//2 7//2
f 1//6 4//6 3//6
f 1//6 2//6 4//6
f 3//3 8//3 7//3
f 3//3 4//3 8//3
f 5//5 7//5 8//5
f 5//5 8//5 6//5
f 1//4 5//4 6//4
f 1//4 6//4 2//4
f 2//1 6//1 8//1
f 2//1 8//1 4//1`
// Create parser and load test OBJ
let parser = new ObjFileParser(testObjFile, 'test');
let file = parser.parse();
let model = file.models[0];
let name = model.name; // Prints "test"
let face = model.faces[0] // Prints faces object
// gets { vertexIndex: 1, textureCoordsIndex: 0, vertexNormalIndex: 2 }
let vert = face.vertices[0];
// gets { x: 0, y: 0, z: 1 }
let vertPoints = model.vertices[vert.vertexIndex];
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"obj-file-parser-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }