From 2ffb76499481305f2ebacdb397ee68130eab9499 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Thu, 15 Oct 2020 14:19:49 +0200 Subject: [PATCH] Make decal paint work --- Sources/arm/node/MakeBrush.hx | 20 ++++++++++----- Sources/arm/node/MakeTexcoord.hx | 5 ++-- Sources/arm/render/Uniforms.hx | 44 ++++++++++++++++++-------------- Sources/arm/ui/UISidebar.hx | 8 +++--- Sources/arm/util/RenderUtil.hx | 2 +- 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Sources/arm/node/MakeBrush.hx b/Sources/arm/node/MakeBrush.hx index b071231f..423f2e4f 100644 --- a/Sources/arm/node/MakeBrush.hx +++ b/Sources/arm/node/MakeBrush.hx @@ -9,10 +9,14 @@ class MakeBrush { public static function run(vert: NodeShader, frag: NodeShader) { - if (Context.tool == ToolDecal || Context.tool == ToolText || Context.tool == ToolParticle) { - frag.write('float dist = 0.0;'); - } - else if (Config.raw.brush_3d) { + frag.write('float dist = 0.0;'); + + if (Context.tool == ToolParticle) return; + + var decal = Context.tool == ToolDecal || Context.tool == ToolText; + if (decal) frag.write('if (decalPaint.z > 0) {'); + + if (Config.raw.brush_3d) { frag.write('vec4 inpLocal = inp;'); // TODO: spirv workaround frag.write('vec4 inplastLocal = inplast;'); // TODO: spirv workaround #if (kha_direct3d11 || kha_direct3d12 || kha_metal || kha_vulkan) @@ -67,12 +71,12 @@ class MakeBrush { if (Context.brushLazyRadius > 0 && Context.brushLazyStep > 0) { // Sphere - frag.write('float dist = distance(wposition, winp.xyz);'); + frag.write('dist = distance(wposition, winp.xyz);'); } else { // Capsule frag.write('float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);'); - frag.write('float dist = length(pa - ba * h);'); + frag.write('dist = length(pa - ba * h);'); } frag.write('if (dist > brushRadius) discard;'); @@ -89,9 +93,11 @@ class MakeBrush { frag.write('vec2 pa = bsp.xy - binp.xy;'); frag.write('vec2 ba = binplast.xy - binp.xy;'); frag.write('float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);'); - frag.write('float dist = length(pa - ba * h);'); + frag.write('dist = length(pa - ba * h);'); frag.write('if (dist > brushRadius) discard;'); } + + if (decal) frag.write('}'); } } diff --git a/Sources/arm/node/MakeTexcoord.hx b/Sources/arm/node/MakeTexcoord.hx index 85e30bae..d79746ae 100644 --- a/Sources/arm/node/MakeTexcoord.hx +++ b/Sources/arm/node/MakeTexcoord.hx @@ -23,9 +23,10 @@ class MakeTexcoord { //frag.write_attrib('if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;'); } else if (decal) { - frag.write_attrib('uvsp -= inp.xy;'); + frag.add_uniform('vec4 decalPaint', '_decalPaint'); + frag.write_attrib('uvsp -= decalPaint.xy;'); frag.write_attrib('uvsp.x *= aspectRatio;'); - frag.write_attrib('uvsp *= 0.21 / (brushRadius * 0.9);'); + frag.write_attrib('uvsp *= 0.21 / (decalPaint.w * 0.9);'); // Decal radius if (Context.brushDirectional) { frag.add_uniform('vec3 brushDirection', '_brushDirection'); diff --git a/Sources/arm/render/Uniforms.hx b/Sources/arm/render/Uniforms.hx index 7e678f28..4b9b3324 100644 --- a/Sources/arm/render/Uniforms.hx +++ b/Sources/arm/render/Uniforms.hx @@ -32,20 +32,9 @@ class Uniforms { switch (link) { #if arm_painter case "_brushRadius": { - var val = (Context.brushRadius * Context.brushNodesRadius) / 15.0; - var pen = Input.getPen(); - if (Config.raw.pressure_radius && pen.down()) { - val *= pen.pressure * Config.raw.pressure_sensitivity; - } - var scale2d = (900 / App.h()) * Config.raw.window_scale; - var decal = Context.tool == ToolDecal || Context.tool == ToolText; - if (Config.raw.brush_3d && !decal) { - val *= Context.paint2d ? 0.55 * scale2d : 2; - } - else { - val *= scale2d; // Projection ratio - } - return val; + var val = radiusUniform(); + var decalPaint = Operator.shortcut(Config.keymap.decal_paint + "+" + Config.keymap.action_paint, ShortcutDown); + return decalPaint ? val * 2 : val; } case "_brushScaleX": { return 1 / Context.brushScaleX; @@ -126,6 +115,23 @@ class Uniforms { return null; } + static function radiusUniform(): Float { + var val = (Context.brushRadius * Context.brushNodesRadius) / 15.0; + var pen = Input.getPen(); + if (Config.raw.pressure_radius && pen.down()) { + val *= pen.pressure * Config.raw.pressure_sensitivity; + } + var scale2d = (900 / App.h()) * Config.raw.window_scale; + var decal = Context.tool == ToolDecal || Context.tool == ToolText; + if (Config.raw.brush_3d && !decal) { + val *= Context.paint2d ? 0.55 * scale2d : 2; + } + else { + val *= scale2d; // Projection ratio + } + return val; + } + public static function linkVec2(object: Object, mat: MaterialData, link: String): iron.math.Vec4 { #if arm_painter switch (link) { @@ -312,11 +318,6 @@ class Uniforms { Context.prevPaintVecY = Context.lastPaintVecY; return v; } - case "_decalPaint": { - var decalPaint = Operator.shortcut(Config.keymap.decal_paint + "+" + Config.keymap.action_paint, ShortcutDown); - vec.set(Context.decalX, Context.decalY, decalPaint ? 1 : 0); - return vec; - } #end } @@ -359,6 +360,11 @@ class Uniforms { vec.set(Context.envmapAngle, Math.sin(-Context.envmapAngle), Math.cos(-Context.envmapAngle), Context.showEnvmap ? Scene.active.world.probe.raw.strength : 4.0); return vec; } + case "_decalPaint": { + var decalPaint = Operator.shortcut(Config.keymap.decal_paint + "+" + Config.keymap.action_paint, ShortcutDown); + vec.set(Context.decalX, Context.decalY, decalPaint ? 1 : 0, radiusUniform()); + return vec; + } } #end return null; diff --git a/Sources/arm/ui/UISidebar.hx b/Sources/arm/ui/UISidebar.hx index 090d41c1..d15f3029 100644 --- a/Sources/arm/ui/UISidebar.hx +++ b/Sources/arm/ui/UISidebar.hx @@ -743,11 +743,13 @@ class UISidebar { if (decal && !inNodes) { var psizex = Std.int(256 * ui.SCALE() * (Context.brushRadius * Context.brushNodesRadius * Context.brushScaleX)); var psizey = Std.int(256 * ui.SCALE() * (Context.brushRadius * Context.brushNodesRadius)); + var decalAlpha = 0.5; if (!Operator.shortcut(Config.keymap.decal_paint, ShortcutDown)) { Context.decalX = Context.paintVec.x; Context.decalY = Context.paintVec.y; + decalAlpha = Context.brushOpacity; } - var decalX = App.x() + Context.decalX * iron.App.w() - psizex / 2; + var decalX = App.x() + Context.decalX * App.w() - psizex / 2; var decalY = App.y() + Context.decalY * App.h() - psizey / 2; // Radius being scaled @@ -756,9 +758,9 @@ class UISidebar { decalY += Context.lockStartedY - System.windowHeight() / 2; } - g.color = kha.Color.fromFloats(1, 1, 1, Context.brushOpacity); + g.color = kha.Color.fromFloats(1, 1, 1, decalAlpha); var angle = (Context.brushAngle + Context.brushNodesAngle) * (Math.PI / 180); - g.pushRotation(-angle, mx, my); + g.pushRotation(-angle, decalX + psizex / 2, decalY + psizey / 2); #if (kha_direct3d11 || kha_direct3d12 || kha_metal || kha_vulkan) g.drawScaledImage(Context.decalImage, decalX, decalY, psizex, psizey); #else diff --git a/Sources/arm/util/RenderUtil.hx b/Sources/arm/util/RenderUtil.hx index 68142baf..3a530384 100644 --- a/Sources/arm/util/RenderUtil.hx +++ b/Sources/arm/util/RenderUtil.hx @@ -164,7 +164,7 @@ class RenderUtil { Scene.active.world.envmap = Context.showEnvmap ? Context.savedEnvmap : Context.emptyEnvmap; MakeMaterial.parseMeshMaterial(); - Context.ddirty = 0; + Context.ddirty = 1; // Refresh depth for decal paint if (current != null) current.begin(false); }