-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.js
More file actions
50 lines (47 loc) · 1.21 KB
/
fs.js
File metadata and controls
50 lines (47 loc) · 1.21 KB
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
const fs = require('fs');
const readline = require('readline');
var filename = 'mountains';
function triangle(a, b, c, d, e, f, g, h, i) {
this.p = [new vec3d(a, b, c), new vec3d(d, e, f), new vec3d(g, h, i)];
}
var triangles = [];
var vertices = [];
function vec3d(x = 0, y = 0, z = 0, w = 1) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
const readInterface = readline.createInterface({
input: fs.createReadStream('./' + filename + '.obj'),
output: false,
console: false,
});
readInterface.on('line', function (line) {
var arr = line.split(' ');
if (arr[0] == 'v') {
vertices.push(new vec3d(Number(arr[1]), Number(arr[2]), Number(arr[3])));
} else if (arr[0] == 'f') {
one = Number(arr[1]) - 1;
two = Number(arr[2]) - 1;
three = Number(arr[3]) - 1;
triangles.push(
new triangle(
vertices[one].x,
vertices[one].y,
vertices[one].z,
vertices[two].x,
vertices[two].y,
vertices[two].z,
vertices[three].x,
vertices[three].y,
vertices[three].z
)
);
}
});
readInterface.on('close', () => {
fs.writeFile('./' + filename + '.js', JSON.stringify(triangles), (err) => {
console.error(err);
});
});