-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjify.js
40 lines (30 loc) · 939 Bytes
/
objify.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
var encode = require('tab64').encode
var unindex = require('unindex-mesh')
var through = require('through')
var obj = require('dot-obj')
var deobj = require.resolve('./deobj')
module.exports = objify
function objify(file, opts) {
if (!/\.obj$/g.test(file)) return through()
var stream = through(write, flush)
var buffer = []
return stream
function write(data) {
buffer.push(data)
}
function flush() {
stream.queue('module.exports = function(separate, indices) {return ')
stream.queue('require("'+deobj+'")([')
var models = obj(buffer.join(''))
for (var i = 0; i < models.length; i++) {
var model = models[i]
var positions = unindex(model.positions, model.cells)
if (i) stream.queue(',')
stream.queue('"')
stream.queue(encode(positions).replace(/\s/g, ''))
stream.queue('"')
}
stream.queue('], separate, indices)}')
stream.queue(null)
}
}