-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
64 lines (59 loc) · 1.54 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Vec3 } from './common.js';
export class Model {
/**
* @param {string} data
*/
constructor(data) {
/**
* @type {Vec3[]}
*/
this.verts = [];
/**
* @type {{ v0: number; v1: number; v2: number; vt0: number; vt1: number; vt2: number; vn0: number; vn1: number; vn2: number; }[]}
*/
this.faces = [];
/**
* @type {[number, number][]}
*/
this.texts = [];
/**
* @type {Vec3[]}
*/
this.norms = [];
const lines = data.split('\n');
for (const line of lines) {
const items = line.split(/\s+/);
switch (items[0]) {
case 'v':
this.verts.push(
new Vec3(Number(items[1]), Number(items[2]), Number(items[3]))
);
break;
case 'f':
const [v0, vt0, vn0] = items[1].split('/');
const [v1, vt1, vn1] = items[2].split('/');
const [v2, vt2, vn2] = items[3].split('/');
this.faces.push({
v0: Number(v0) - 1,
v1: Number(v1) - 1,
v2: Number(v2) - 1,
vt0: Number(vt0) - 1,
vt1: Number(vt1) - 1,
vt2: Number(vt2) - 1,
vn0: Number(vn0) - 1,
vn1: Number(vn1) - 1,
vn2: Number(vn2) - 1,
});
break;
case 'vt':
this.texts.push([Number(items[1]), Number(items[2])]);
break;
case 'vn':
this.norms.push(
new Vec3(Number(items[1]), Number(items[2]), Number(items[3]))
);
break;
}
}
}
}