-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsvg-mesh.js
36 lines (29 loc) · 1.17 KB
/
svg-mesh.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
var parse = require('parse-svg-path')
var triangulate = require('triangulate-contours')
var normalize = require('normalize-path-scale')
var contours = require('svg-path-contours')
var bounds = require('getboundingbox')
var simplify = require('simplify-path')
var flatten = require('flatten')
//takes in string contents from SVG path, and exports a 3D triangulated
//and simplified mesh, normalized to [-1.0 - 1.0] range
module.exports = function(contents, opt) {
opt = opt||{}
var polylines = contours(parse(contents), 5)
//get all our lines as a single path and determine its bounding box
var flat = flatten(polylines, 1)
var box = bounds(flat)
//now simplify based on the width of the bounding box (for consistency across shapes)
var threshold = typeof opt.threshold === 'number' ? opt.threshold : 0.001
polylines = polylines.map(function(c) {
return simplify(c, (box.maxX-box.minX)*threshold)
})
//triangulate the polylines..
var c = triangulate(polylines)
//then normalize the positions in -1.0 to 1.0 space
c.positions = normalize(c.positions, box).map(to3d)
return c
}
function to3d(p) {
return [p[0], p[1], p[2]||0]
}