Files
armorpaint/Assets/plugins/texsynth.js
T

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-10-12 09:41:08 +02:00
// Example-based texture synthesis written in Rust
2020-10-11 21:22:33 +02:00
// https://github.com/EmbarkStudios/texture-synthesis
2020-10-12 09:41:08 +02:00
// texsynth library is linked with armorcore until multi-threaded execution is supported via webassembly
2020-10-11 21:22:33 +02:00
// https://github.com/armory3d/armorpaint_plugins/tree/master/plugin_texsynth
let plugin = new arm.Plugin();
let h1 = new zui.Handle();
plugin.drawUI = function(ui) {
if (ui.panel(h1, "Texture Synthesis")) {
if (ui.button("Inpaint")) {
texsynthInpaint(false);
}
if (ui.button("Tiling")) {
texsynthInpaint(true);
}
}
};
function texsynthInpaint(tiling) {
let w = arm.Config.getTextureResX();
let h = arm.Config.getTextureResY();
let l = arm.Context.layer;
2020-10-13 11:41:00 +02:00
if (l.texpaint_mask == null) {
return;
2020-10-12 09:41:08 +02:00
}
2020-10-13 11:41:00 +02:00
let bytes_img = l.texpaint.getPixels().b.bufferValue;
let bytes_mask = l.texpaint_mask.getPixels().b.bufferValue;
2020-10-12 09:41:08 +02:00
let bytes_out = new arm.Bytes(new ArrayBuffer(w * h * 4));
2020-10-13 11:41:00 +02:00
Krom.texsynthInpaint(w, h, bytes_out.b.bufferValue, bytes_img, bytes_mask, tiling);
2020-10-11 21:22:33 +02:00
let image = arm.Image.fromBytes(bytes_out, w, h);
2020-10-13 11:41:00 +02:00
function apply(g) {
g.end();
arm.Context.layerIsMask = false;
arm.History.applyFilter();
l.deleteMask();
l.texpaint.unload();
l.texpaint = arm.Image.createRenderTarget(w, h);
let g2 = l.texpaint.get_g2();
g2.begin(false);
g2.drawImage(image, 0, 0);
g2.end();
let rts = iron.RenderPath.active.renderTargets;
rts.h["texpaint" + l.ext].image = l.texpaint;
arm.MakeMaterial.parseMeshMaterial();
arm.App.redrawUI();
arm.Context.layerPreviewDirty = true;
g.begin();
iron.App.removeRender(apply);
}
iron.App.notifyOnRender(apply);
2020-10-11 21:22:33 +02:00
}