add new render type

This commit is contained in:
Stewart Allen 2020-11-06 22:33:56 -05:00
commit cbc363d49f
5 changed files with 79 additions and 15 deletions

View file

@ -16,6 +16,9 @@
};
function allocFloat32Array(len) {
if (Array.isArray(len)) {
return new Float32Array(len);
}
return new Float32Array(len);
}
@ -121,7 +124,7 @@
data: Object.values(this.layers).map(layer => { return {
polys: encode(layer.polys, state),
lines: encodePointArray(layer.lines, state),
faces: encodePointArray(layer.faces, state),
faces: codec.allocFloat32Array(layer.faces),
color: layer.color
} })
};
@ -134,7 +137,7 @@
render.layers[v.layers[i]] = {
polys: decode(v.data[i].polys, state),
lines: decodePointArray(v.data[i].lines),
faces: decodePointArray(v.data[i].faces),
faces: v.data[i].faces,
color: v.data[i].color
};
}

View file

@ -3,18 +3,29 @@
"use strict";
(function() {
const KIRI = self.kiri, BASE = self.base, POLY = BASE.polygons;
class Render {
constructor() {
this.layers = {};
}
setLayer(layer, color) {
setLayer(layer, colors) {
let layers = this.layers;
if (typeof(colors) === 'number') {
colors = {
line: color,
face: color
};
}
this.current = layers[layer] = layers[layer] || {
lines: [],
polys: [],
faces: [],
color: color || 0
color: colors || {
line: 0,
face: 0
}
};
return this;
}
@ -44,6 +55,33 @@
}
return this;
}
addFlat(polys, options) {
let opts = options || {};
let offset = opts.offset || 1;
if (Array.isArray(polys)) {
polys = POLY.flatten(polys, [], true);
} else {
polys = POLY.flatten([polys], [], true);
}
if (!polys.length) {
return;
}
const z = polys[0].getZ(), faces = this.current.faces;
polys.forEach(poly => {
let exp = [];
POLY.offset([poly], offset/2, { z, outs: exp, flat: true });
POLY.offset([poly], -offset/2, { z, outs: exp, flat: true });
if (opts.outline) {
this.addPolys(exp.clone());
}
POLY.nest(exp).forEach((poly,i) => {
poly.earcut().forEach(ep => {
ep.forEachPoint(p => { faces.push(p.x, p.y, p.z) });
});
});
});
}
}
self.kiri.Render = Render;