Files
armorpaint/Sources/arm/data/LayerSlot.hx
T

362 lines
10 KiB
Haxe
Raw Normal View History

2019-06-19 22:42:17 +02:00
package arm.data;
2018-10-29 15:30:46 +01:00
2019-05-09 22:38:50 +02:00
import kha.graphics4.TextureFormat;
2019-06-20 11:23:01 +02:00
import kha.Image;
2018-10-29 15:30:46 +01:00
import iron.RenderPath;
2019-05-10 19:28:59 +02:00
import arm.ui.UITrait;
2019-11-13 15:17:10 +01:00
import arm.node.MaterialParser;
2019-12-08 16:59:08 +01:00
import arm.Tool;
2018-10-29 15:30:46 +01:00
class LayerSlot {
public var id = 0;
public var visible = true;
2018-11-01 01:57:38 +01:00
public var ext = "";
2018-10-29 15:30:46 +01:00
2019-12-09 00:28:10 +01:00
public var name: String;
2019-03-27 14:26:51 +01:00
2019-12-09 00:28:10 +01:00
public var texpaint: Image;
public var texpaint_nor: Image;
public var texpaint_pack: Image;
2018-10-29 15:30:46 +01:00
2019-12-09 00:28:10 +01:00
public var texpaint_preview: Image; // Layer preview
2019-03-27 14:26:51 +01:00
2019-12-09 00:28:10 +01:00
public var texpaint_mask: Image = null; // Texture mask
public var texpaint_mask_preview: Image;
2019-04-16 22:34:47 +02:00
public var maskOpacity = 1.0; // Opacity mask
2019-12-09 00:28:10 +01:00
public var material_mask: MaterialSlot = null; // Fill layer
2019-03-27 18:26:37 +01:00
2019-12-08 16:59:08 +01:00
public var blending = BlendMix;
2019-03-25 21:56:39 +01:00
public var objectMask = 0;
2019-10-15 00:01:41 +02:00
public var uvScale = 1.0;
2019-10-15 11:49:23 +02:00
public var uvRot = 0.0;
2019-12-08 16:59:08 +01:00
public var uvType = UVMap;
2019-03-26 15:22:07 +01:00
public var paintBase = true;
public var paintOpac = true;
public var paintOcc = true;
public var paintRough = true;
public var paintMet = true;
public var paintNor = true;
2019-04-13 23:38:00 +02:00
public var paintHeight = true;
public var paintEmis = true;
public var paintSubs = true;
2019-03-25 21:56:39 +01:00
2019-12-09 00:28:10 +01:00
var createMaskColor: Int;
var createMaskImage: Image;
2019-04-07 13:54:44 +02:00
2018-10-29 15:30:46 +01:00
public function new(ext = "") {
if (ext == "") {
2019-06-24 22:35:17 +02:00
id = 0;
for (l in Project.layers) if (l.id >= id) id = l.id + 1;
2018-10-29 15:30:46 +01:00
ext = id + "";
}
2019-12-11 21:30:29 +01:00
2018-10-29 15:30:46 +01:00
this.ext = ext;
2019-03-27 14:26:51 +01:00
name = "Layer " + (id + 1);
2019-12-11 21:30:29 +01:00
var format = App.bitsHandle.position == Bits8 ? "RGBA32" :
App.bitsHandle.position == Bits16 ? "RGBA64" :
"RGBA128";
2018-10-29 15:30:46 +01:00
{
var t = new RenderTargetRaw();
t.name = "texpaint" + ext;
2019-02-02 18:49:19 +01:00
t.width = Config.getTextureRes();
t.height = Config.getTextureRes();
2019-06-18 08:36:19 +02:00
t.format = format;
2018-10-29 15:30:46 +01:00
texpaint = RenderPath.active.createRenderTarget(t).image;
}
{
var t = new RenderTargetRaw();
t.name = "texpaint_nor" + ext;
2019-02-02 18:49:19 +01:00
t.width = Config.getTextureRes();
t.height = Config.getTextureRes();
2019-06-18 08:36:19 +02:00
t.format = format;
2018-10-29 15:30:46 +01:00
texpaint_nor = RenderPath.active.createRenderTarget(t).image;
}
{
var t = new RenderTargetRaw();
t.name = "texpaint_pack" + ext;
2019-02-02 18:49:19 +01:00
t.width = Config.getTextureRes();
t.height = Config.getTextureRes();
2019-06-18 08:36:19 +02:00
t.format = format;
2018-10-29 15:30:46 +01:00
texpaint_pack = RenderPath.active.createRenderTarget(t).image;
}
2019-03-27 14:26:51 +01:00
2019-06-20 11:23:01 +02:00
texpaint_preview = Image.createRenderTarget(200, 200, TextureFormat.RGBA32);
2018-10-29 15:30:46 +01:00
}
2019-11-11 15:35:00 +01:00
public function delete() {
unload();
var lpos = Project.layers.indexOf(this);
Project.layers.remove(this);
// Undo can remove base layer and then restore it from undo layers
if (lpos > 0) Context.setLayer(Project.layers[lpos - 1]);
}
2018-10-29 15:30:46 +01:00
public function unload() {
texpaint.unload();
texpaint_nor.unload();
texpaint_pack.unload();
2018-10-29 17:43:40 +01:00
RenderPath.active.renderTargets.remove("texpaint" + ext);
RenderPath.active.renderTargets.remove("texpaint_nor" + ext);
RenderPath.active.renderTargets.remove("texpaint_pack" + ext);
2019-03-27 14:26:51 +01:00
texpaint_preview.unload();
2019-03-27 18:26:37 +01:00
deleteMask();
2018-10-29 15:30:46 +01:00
}
2019-12-09 00:28:10 +01:00
public function swap(other: LayerSlot) {
2018-10-29 15:30:46 +01:00
RenderPath.active.renderTargets.get("texpaint" + ext).image = other.texpaint;
RenderPath.active.renderTargets.get("texpaint_nor" + ext).image = other.texpaint_nor;
RenderPath.active.renderTargets.get("texpaint_pack" + ext).image = other.texpaint_pack;
2019-10-06 19:03:59 +02:00
2018-10-29 15:30:46 +01:00
RenderPath.active.renderTargets.get("texpaint" + other.ext).image = texpaint;
RenderPath.active.renderTargets.get("texpaint_nor" + other.ext).image = texpaint_nor;
RenderPath.active.renderTargets.get("texpaint_pack" + other.ext).image = texpaint_pack;
2019-12-09 00:28:10 +01:00
var _texpaint = texpaint;
var _texpaint_nor = texpaint_nor;
var _texpaint_pack = texpaint_pack;
2018-10-29 15:30:46 +01:00
texpaint = other.texpaint;
texpaint_nor = other.texpaint_nor;
texpaint_pack = other.texpaint_pack;
2019-12-09 00:28:10 +01:00
other.texpaint = _texpaint;
other.texpaint_nor = _texpaint_nor;
other.texpaint_pack = _texpaint_pack;
2018-10-29 15:30:46 +01:00
}
2019-03-27 18:26:37 +01:00
2019-12-09 00:28:10 +01:00
public function swapMask(other: LayerSlot) {
2019-05-07 16:57:14 +02:00
RenderPath.active.renderTargets.get("texpaint_mask" + ext).image = other.texpaint_mask;
RenderPath.active.renderTargets.get("texpaint_mask" + other.ext).image = texpaint_mask;
2019-12-09 00:28:10 +01:00
var _texpaint_mask = texpaint_mask;
2019-05-07 16:57:14 +02:00
texpaint_mask = other.texpaint_mask;
2019-12-09 00:28:10 +01:00
other.texpaint_mask = _texpaint_mask;
2019-05-07 16:57:14 +02:00
}
2019-12-09 00:28:10 +01:00
public function createMask(color: Int, clear = true, image: Image = null) {
2019-03-27 18:26:37 +01:00
if (texpaint_mask != null) return;
{
var t = new RenderTargetRaw();
t.name = "texpaint_mask" + ext;
t.width = Config.getTextureRes();
t.height = Config.getTextureRes();
2019-12-09 00:28:10 +01:00
t.format = "R8";
2019-03-27 18:26:37 +01:00
texpaint_mask = RenderPath.active.createRenderTarget(t).image;
}
2019-06-20 11:23:01 +02:00
texpaint_mask_preview = Image.createRenderTarget(200, 200, TextureFormat.L8);
2019-03-27 18:26:37 +01:00
2019-04-17 10:13:29 +02:00
if (clear) {
createMaskColor = color;
2019-06-03 17:12:06 +02:00
createMaskImage = image;
2019-04-17 10:13:29 +02:00
iron.App.notifyOnRender(clearMask);
}
2019-03-27 18:26:37 +01:00
}
2019-12-09 00:28:10 +01:00
function clearMask(g: kha.graphics4.Graphics) {
2019-03-27 18:26:37 +01:00
g.end();
2019-06-03 17:12:06 +02:00
texpaint_mask.g2.begin();
2019-10-06 19:03:59 +02:00
2019-06-03 17:12:06 +02:00
if (createMaskImage != null) texpaint_mask.g2.drawScaledImage(createMaskImage, 0, 0, texpaint_mask.width, texpaint_mask.height);
else texpaint_mask.g2.clear(createMaskColor);
texpaint_mask.g2.end();
2019-03-27 18:26:37 +01:00
g.begin();
iron.App.removeRender(clearMask);
2019-06-03 17:12:06 +02:00
2019-06-23 14:50:41 +02:00
Context.layerPreviewDirty = true;
2019-06-03 17:12:06 +02:00
createMaskColor = 0;
createMaskImage = null;
2019-03-27 18:26:37 +01:00
}
public function deleteMask() {
if (texpaint_mask == null) return;
texpaint_mask.unload();
RenderPath.active.renderTargets.remove("texpaint_mask" + ext);
2019-03-27 21:23:50 +01:00
texpaint_mask = null;
2019-03-27 18:26:37 +01:00
2019-03-27 21:23:50 +01:00
texpaint_mask_preview.unload();
2019-03-27 18:26:37 +01:00
}
2019-04-16 00:04:28 +02:00
public function applyMask() {
if (texpaint_mask == null) return;
2019-12-12 14:34:11 +01:00
if (Layers.pipeMerge == null) Layers.makePipe();
2019-04-16 00:04:28 +02:00
Layers.makeTempImg();
// Copy layer to temp
Layers.imga.g2.begin(false);
Layers.imga.g2.pipeline = Layers.pipeCopy;
Layers.imga.g2.drawImage(texpaint, 0, 0);
Layers.imga.g2.end();
// Merge mask
if (iron.data.ConstData.screenAlignedVB == null) iron.data.ConstData.createScreenAlignedData();
texpaint.g4.begin();
texpaint.g4.setPipeline(Layers.pipeMask);
texpaint.g4.setTexture(Layers.tex0Mask, Layers.imga);
texpaint.g4.setTexture(Layers.texaMask, texpaint_mask);
texpaint.g4.setVertexBuffer(iron.data.ConstData.screenAlignedVB);
texpaint.g4.setIndexBuffer(iron.data.ConstData.screenAlignedIB);
texpaint.g4.drawIndexedVertices();
texpaint.g4.end();
deleteMask();
}
2019-04-16 22:34:47 +02:00
2019-12-09 00:28:10 +01:00
public function duplicate(): LayerSlot {
2019-06-23 14:50:41 +02:00
var layers = Project.layers;
2019-04-16 22:34:47 +02:00
var i = 0;
while (i++ < layers.length) if (layers[i] == this) break;
i++;
var l = new LayerSlot();
layers.insert(i, l);
2019-12-12 14:34:11 +01:00
if (Layers.pipeMerge == null) Layers.makePipe();
2019-04-16 22:34:47 +02:00
l.texpaint.g2.begin(false);
l.texpaint.g2.pipeline = Layers.pipeCopy;
l.texpaint.g2.drawImage(texpaint, 0, 0);
l.texpaint.g2.end();
l.texpaint_nor.g2.begin(false);
l.texpaint_nor.g2.pipeline = Layers.pipeCopy;
l.texpaint_nor.g2.drawImage(texpaint_nor, 0, 0);
l.texpaint_nor.g2.end();
l.texpaint_pack.g2.begin(false);
l.texpaint_pack.g2.pipeline = Layers.pipeCopy;
l.texpaint_pack.g2.drawImage(texpaint_pack, 0, 0);
l.texpaint_pack.g2.end();
2019-10-06 19:03:59 +02:00
2019-05-15 19:15:42 +02:00
l.texpaint_preview.g2.begin(true, 0xff000000);
l.texpaint_preview.g2.drawScaledImage(texpaint_preview, 0, 0, texpaint_preview.width, texpaint_preview.height);
l.texpaint_preview.g2.end();
if (texpaint_mask != null) {
l.createMask(0, false);
l.texpaint_mask.g2.begin(false);
l.texpaint_mask.g2.pipeline = Layers.pipeCopy;
l.texpaint_mask.g2.drawImage(texpaint_mask, 0, 0);
l.texpaint_mask.g2.end();
l.texpaint_mask_preview.g2.begin(true, 0xff000000);
l.texpaint_mask_preview.g2.drawScaledImage(texpaint_mask_preview, 0, 0, texpaint_mask_preview.width, texpaint_mask_preview.height);
l.texpaint_mask_preview.g2.end();
}
l.visible = visible;
l.maskOpacity = maskOpacity;
l.material_mask = material_mask;
l.objectMask = objectMask;
2019-10-22 12:02:25 +02:00
l.blending = blending;
2019-05-15 19:15:42 +02:00
l.paintBase = paintBase;
l.paintOpac = paintOpac;
l.paintOcc = paintOcc;
l.paintRough = paintRough;
l.paintMet = paintMet;
l.paintNor = paintNor;
l.paintHeight = paintHeight;
l.paintEmis = paintEmis;
l.paintSubs = paintSubs;
2019-10-06 19:03:59 +02:00
2019-04-16 22:34:47 +02:00
return l;
}
2019-05-09 22:38:50 +02:00
2019-06-19 09:40:14 +02:00
public function resizeAndSetBits() {
2019-12-11 21:30:29 +01:00
var format = App.bitsHandle.position == Bits8 ? TextureFormat.RGBA32 :
App.bitsHandle.position == Bits16 ? TextureFormat.RGBA64 :
TextureFormat.RGBA128;
2019-10-06 19:03:59 +02:00
2019-05-09 22:38:50 +02:00
var res = Config.getTextureRes();
var rts = RenderPath.active.renderTargets;
var texpaint = this.texpaint;
var texpaint_nor = this.texpaint_nor;
var texpaint_pack = this.texpaint_pack;
2019-06-20 11:23:01 +02:00
this.texpaint = Image.createRenderTarget(res, res, format);
this.texpaint_nor = Image.createRenderTarget(res, res, format);
this.texpaint_pack = Image.createRenderTarget(res, res, format);
2019-05-09 22:38:50 +02:00
this.texpaint.g2.begin(false);
this.texpaint.g2.drawScaledImage(texpaint, 0, 0, res, res);
this.texpaint.g2.end();
this.texpaint_nor.g2.begin(false);
this.texpaint_nor.g2.drawScaledImage(texpaint_nor, 0, 0, res, res);
this.texpaint_nor.g2.end();
this.texpaint_pack.g2.begin(false);
this.texpaint_pack.g2.drawScaledImage(texpaint_pack, 0, 0, res, res);
this.texpaint_pack.g2.end();
2019-11-26 12:49:04 +01:00
iron.App.notifyOnInit(function() { // Out of command list execution
texpaint.unload();
texpaint_nor.unload();
texpaint_pack.unload();
});
2019-05-09 22:38:50 +02:00
rts.get("texpaint" + this.ext).image = this.texpaint;
rts.get("texpaint_nor" + this.ext).image = this.texpaint_nor;
rts.get("texpaint_pack" + this.ext).image = this.texpaint_pack;
2019-06-19 09:40:14 +02:00
if (this.texpaint_mask != null && this.texpaint_mask.width != res) {
2019-05-09 22:38:50 +02:00
var texpaint_mask = this.texpaint_mask;
2019-06-20 11:23:01 +02:00
this.texpaint_mask = Image.createRenderTarget(res, res, TextureFormat.L8);
2019-05-09 22:38:50 +02:00
this.texpaint_mask.g2.begin(false);
this.texpaint_mask.g2.drawScaledImage(texpaint_mask, 0, 0, res, res);
this.texpaint_mask.g2.end();
2019-11-26 12:49:04 +01:00
iron.App.notifyOnInit(function() { // Out of command list execution
texpaint_mask.unload();
});
2019-05-09 22:38:50 +02:00
rts.get("texpaint_mask" + this.ext).image = this.texpaint_mask;
}
}
2019-11-11 15:35:00 +01:00
2019-12-09 00:28:10 +01:00
public function clear(g: kha.graphics4.Graphics) {
2019-11-11 15:35:00 +01:00
g.end();
texpaint.g4.begin();
texpaint.g4.clear(kha.Color.fromFloats(0.0, 0.0, 0.0, 0.0)); // Base
texpaint.g4.end();
texpaint_nor.g4.begin();
texpaint_nor.g4.clear(kha.Color.fromFloats(0.5, 0.5, 1.0, 0.0)); // Nor
texpaint_nor.g4.end();
texpaint_pack.g4.begin();
texpaint_pack.g4.clear(kha.Color.fromFloats(1.0, 0.0, 0.0, 0.0)); // Occ, rough, met
texpaint_pack.g4.end();
g.begin();
iron.App.removeRender(clear);
#if krom_linux
Context.layerPreviewDirty = true;
#end
}
public function toFillLayer() {
Context.setLayer(this);
material_mask = Context.material;
Layers.updateFillLayers(4);
MaterialParser.parsePaintMaterial();
Context.layerPreviewDirty = true;
UITrait.inst.hwnd.redraws = 2;
}
public function toPaintLayer() {
Context.setLayer(this);
material_mask = null;
MaterialParser.parsePaintMaterial();
Context.layerPreviewDirty = true;
UITrait.inst.hwnd.redraws = 2;
}
2018-10-29 15:30:46 +01:00
}