From 5bf3e5192f56e8852bf978f221f1eef18d6a4986 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Thu, 5 Jan 2023 15:34:08 +0100 Subject: [PATCH] Add 'to mask' button for color id tool --- Shaders/mask_colorid.frag.glsl | 15 +++++++++++++++ Sources/arm/Layers.hx | 13 +++++++++++++ Sources/arm/ui/UIHeader.hx | 27 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 Shaders/mask_colorid.frag.glsl diff --git a/Shaders/mask_colorid.frag.glsl b/Shaders/mask_colorid.frag.glsl new file mode 100644 index 00000000..f3b93962 --- /dev/null +++ b/Shaders/mask_colorid.frag.glsl @@ -0,0 +1,15 @@ +// Turn picked color id into mask + +#version 330 + +uniform sampler2D texpaint_colorid; // 1x1 picked color +uniform sampler2D texcolorid; +in vec2 texCoord; +out vec4 FragColor; + +void main() { + vec3 colorid_c1 = texelFetch(texpaint_colorid, ivec2(0, 0), 0).rgb; + vec3 colorid_c2 = textureLod(texcolorid, texCoord, 0).rgb; + if (colorid_c1 != colorid_c2) discard; + FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/Sources/arm/Layers.hx b/Sources/arm/Layers.hx index 9e9b9b69..232a2128 100644 --- a/Sources/arm/Layers.hx +++ b/Sources/arm/Layers.hx @@ -35,6 +35,7 @@ class Layers { public static var pipeInvert8: PipelineState; public static var pipeApplyMask: PipelineState; public static var pipeMergeMask: PipelineState; + public static var pipeColorIdToMask: PipelineState; public static var tex0: TextureUnit; public static var tex1: TextureUnit; public static var texmask: TextureUnit; @@ -45,6 +46,8 @@ class Layers { public static var texaMask: TextureUnit; public static var tex0MergeMask: TextureUnit; public static var texaMergeMask: TextureUnit; + public static var texColorId: TextureUnit; + public static var texpaintColorId: TextureUnit; public static var opacMergeMask: ConstantLocation; public static var blendingMergeMask: ConstantLocation; public static var tempImage: Image = null; @@ -240,6 +243,16 @@ class Layers { texaMergeMask = pipeMergeMask.getTextureUnit("texa"); opacMergeMask = pipeMergeMask.getConstantLocation("opac"); blendingMergeMask = pipeMergeMask.getConstantLocation("blending"); + + pipeColorIdToMask = new PipelineState(); + pipeColorIdToMask.vertexShader = kha.Shaders.getVertex("layer_merge.vert"); + pipeColorIdToMask.fragmentShader = kha.Shaders.getFragment("mask_colorid.frag"); + var vs = new VertexStructure(); + vs.add("pos", VertexData.Float2); + pipeColorIdToMask.inputLayout = [vs]; + pipeColorIdToMask.compile(); + texpaintColorId = pipeColorIdToMask.getTextureUnit("texpaint_colorid"); + texColorId = pipeColorIdToMask.getTextureUnit("texcolorid"); } public static function makePipeCopyRGB() { diff --git a/Sources/arm/ui/UIHeader.hx b/Sources/arm/ui/UIHeader.hx index c42ed237..6716262d 100644 --- a/Sources/arm/ui/UIHeader.hx +++ b/Sources/arm/ui/UIHeader.hx @@ -45,10 +45,12 @@ class UIHeader { if (Context.colorIdPicked) { ui.image(RenderPath.active.renderTargets.get("texpaint_colorid").image, 0xffffffff, 64); } + ui.enabled = Context.colorIdPicked; if (ui.button(tr("Clear"))) { Context.colorIdPicked = false; UIToolbar.inst.toolbarHandle.redraws = 1; } + ui.enabled = true; ui.text(tr("Color ID Map")); if (Project.assetNames.length > 0) { var cid = ui.combo(Context.colorIdHandle, App.enumTexts("TEX_IMAGE"), tr("Color ID")); @@ -58,6 +60,7 @@ class UIHeader { UIToolbar.inst.toolbarHandle.redraws = 1; } ui.image(Project.getImage(Project.assets[cid])); + if (ui.isHovered) ui.tooltipImage(Project.getImage(Project.assets[cid]), 256); } if (ui.button(tr("Import"))) { UIFiles.show(Path.textureFormats.join(","), false, true, function(path: String) { @@ -74,6 +77,30 @@ class UIHeader { UIStatus.inst.statusHandle.redraws = 2; }); } + ui.enabled = Context.colorIdPicked; + if (ui.button(tr("To Mask"))) { + if (Context.layer.isMask()) Context.setLayer(Context.layer.parent); + var m = Layers.newMask(false, Context.layer); + function _next() { + if (Layers.pipeMerge == null) Layers.makePipe(); + if (iron.data.ConstData.screenAlignedVB == null) iron.data.ConstData.createScreenAlignedData(); + m.texpaint.g4.begin(); + m.texpaint.g4.setPipeline(Layers.pipeColorIdToMask); + m.texpaint.g4.setTexture(Layers.texpaintColorId, RenderPath.active.renderTargets.get("texpaint_colorid").image); + m.texpaint.g4.setTexture(Layers.texColorId, Project.getImage(Project.assets[Context.colorIdHandle.position])); + m.texpaint.g4.setVertexBuffer(iron.data.ConstData.screenAlignedVB); + m.texpaint.g4.setIndexBuffer(iron.data.ConstData.screenAlignedIB); + m.texpaint.g4.drawIndexedVertices(); + m.texpaint.g4.end(); + Context.colorIdPicked = false; + UIToolbar.inst.toolbarHandle.redraws = 1; + UIHeader.inst.headerHandle.redraws = 1; + Context.layerPreviewDirty = true; + } + App.notifyOnNextFrame(_next); + History.newWhiteMask(); + } + ui.enabled = true; } else if (Context.tool == ToolPicker) { var baseRPicked = Math.round(Context.pickedColor.base.R * 10) / 10;