diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index b07f70da5e..0dba3767f9 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -226,9 +226,10 @@ namespace OpenRA.Graphics shader.SetVec("DepthTextureScale", 128 * depthScale / height); } - public void SetDepthPreviewEnabled(bool enabled) + public void SetDepthPreview(bool enabled, float contrast, float offset) { shader.SetBool("EnableDepthPreview", enabled); + shader.SetVec("DepthPreviewParams", contrast, offset); } public void SetAntialiasingPixelsPerTexel(float pxPerTx) diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 67227505cb..a0162f3199 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -45,8 +45,6 @@ namespace OpenRA.Graphics readonly List renderablesBuffer = new List(); - bool lastDepthPreviewEnabled; - internal WorldRenderer(ModData modData, World world) { World = world; @@ -256,11 +254,7 @@ namespace OpenRA.Graphics if (World.WorldActor.Disposed) return; - if (debugVis.Value != null && lastDepthPreviewEnabled != debugVis.Value.DepthBuffer) - { - lastDepthPreviewEnabled = debugVis.Value.DepthBuffer; - Game.Renderer.WorldSpriteRenderer.SetDepthPreviewEnabled(lastDepthPreviewEnabled); - } + debugVis.Value?.UpdateDepthBuffer(); var bounds = Viewport.GetScissorBounds(World.Type != WorldType.Editor); Game.Renderer.EnableScissor(bounds); diff --git a/OpenRA.Game/Traits/World/DebugVisualizations.cs b/OpenRA.Game/Traits/World/DebugVisualizations.cs index 899dfd673e..628c98bea2 100644 --- a/OpenRA.Game/Traits/World/DebugVisualizations.cs +++ b/OpenRA.Game/Traits/World/DebugVisualizations.cs @@ -20,7 +20,51 @@ namespace OpenRA.Traits public bool CombatGeometry; public bool RenderGeometry; public bool ScreenMap; - public bool DepthBuffer; public bool ActorTags; + + // The depth buffer may have been left enabled by the previous world + // Initializing this as dirty forces us to reset the default rendering before the first render + bool depthBufferDirty = true; + bool depthBuffer; + public bool DepthBuffer + { + get => depthBuffer; + set + { + depthBuffer = value; + depthBufferDirty = true; + } + } + + float depthBufferContrast = 1f; + public float DepthBufferContrast + { + get => depthBufferContrast; + set + { + depthBufferContrast = value; + depthBufferDirty = true; + } + } + + float depthBufferOffset; + public float DepthBufferOffset + { + get => depthBufferOffset; + set + { + depthBufferOffset = value; + depthBufferDirty = true; + } + } + + public void UpdateDepthBuffer() + { + if (depthBufferDirty) + { + Game.Renderer.WorldSpriteRenderer.SetDepthPreview(DepthBuffer, DepthBufferContrast, DepthBufferOffset); + depthBufferDirty = false; + } + } } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/DepthPreviewHotkeysLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/DepthPreviewHotkeysLogic.cs new file mode 100644 index 0000000000..40bb6553e9 --- /dev/null +++ b/OpenRA.Mods.Common/Widgets/Logic/DepthPreviewHotkeysLogic.cs @@ -0,0 +1,92 @@ +#region Copyright & License Information +/* + * Copyright 2007-2021 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; +using OpenRA.Mods.Common.Lint; +using OpenRA.Traits; +using OpenRA.Widgets; + +namespace OpenRA.Mods.Common.Widgets.Logic +{ + [ChromeLogicArgsHotkeys("ToggleDepthPreviewKey", + "IncreaseDepthPreviewContrastKey", "DecreaseDepthPreviewContrastKey", + "IncreaseDepthPreviewOffsetKey", "DecreaseDepthPreviewOffsetKey")] + public class DepthPreviewHotkeysLogic : ChromeLogic + { + [ObjectCreator.UseCtor] + public DepthPreviewHotkeysLogic(Widget widget, World world, ModData modData, Dictionary logicArgs) + { + var debugVis = world.WorldActor.TraitOrDefault(); + + var toggleKey = new HotkeyReference(); + if (logicArgs.TryGetValue("ToggleDepthPreviewKey", out var yaml)) + toggleKey = modData.Hotkeys[yaml.Value]; + + var increaseContrastKey = new HotkeyReference(); + if (logicArgs.TryGetValue("IncreaseDepthPreviewContrastKey", out yaml)) + increaseContrastKey = modData.Hotkeys[yaml.Value]; + + var decreaseContrastKey = new HotkeyReference(); + if (logicArgs.TryGetValue("DecreaseDepthPreviewContrastKey", out yaml)) + decreaseContrastKey = modData.Hotkeys[yaml.Value]; + + var increaseOffsetKey = new HotkeyReference(); + if (logicArgs.TryGetValue("IncreaseDepthPreviewOffsetKey", out yaml)) + increaseOffsetKey = modData.Hotkeys[yaml.Value]; + + var decreaseOffsetKey = new HotkeyReference(); + if (logicArgs.TryGetValue("DecreaseDepthPreviewOffsetKey", out yaml)) + decreaseOffsetKey = modData.Hotkeys[yaml.Value]; + + var keyhandler = widget.Get("DEPTHPREVIEW_KEYHANDLER"); + keyhandler.AddHandler(e => + { + if (e.Event != KeyInputEvent.Down) + return false; + + if (toggleKey.IsActivatedBy(e)) + { + debugVis.DepthBuffer ^= true; + return true; + } + + if (!debugVis.DepthBuffer) + return false; + + if (decreaseOffsetKey.IsActivatedBy(e)) + { + debugVis.DepthBufferOffset -= 0.05f; + return true; + } + + if (increaseOffsetKey.IsActivatedBy(e)) + { + debugVis.DepthBufferOffset += 0.05f; + return true; + } + + if (increaseContrastKey.IsActivatedBy(e)) + { + debugVis.DepthBufferContrast += 0.1f; + return true; + } + + if (decreaseContrastKey.IsActivatedBy(e)) + { + debugVis.DepthBufferContrast -= 0.1f; + return true; + } + + return false; + }); + } + } +} diff --git a/glsl/combined.frag b/glsl/combined.frag index d8efc2bfdc..1d1f89f1ea 100644 --- a/glsl/combined.frag +++ b/glsl/combined.frag @@ -15,6 +15,7 @@ uniform sampler2D Palette; uniform sampler2D ColorShifts; uniform bool EnableDepthPreview; +uniform vec2 DepthPreviewParams; uniform float DepthTextureScale; uniform float AntialiasPixelsPerTexel; @@ -55,21 +56,6 @@ in vec4 vTint; out vec4 fragColor; #endif -float jet_r(float x) -{ - return x < 0.7 ? 4.0 * x - 1.5 : -4.0 * x + 4.5; -} - -float jet_g(float x) -{ - return x < 0.5 ? 4.0 * x - 0.5 : -4.0 * x + 3.5; -} - -float jet_b(float x) -{ - return x < 0.3 ? 4.0 * x + 0.5 : -4.0 * x + 2.5; -} - vec3 rgb2hsv(vec3 c) { // From http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl @@ -289,14 +275,12 @@ void main() if (EnableDepthPreview) { - float x = 1.0 - gl_FragDepth; - float r = clamp(jet_r(x), 0.0, 1.0); - float g = clamp(jet_g(x), 0.0, 1.0); - float b = clamp(jet_b(x), 0.0, 1.0); + float intensity = 1.0 - clamp(DepthPreviewParams.x * depth - 0.5 * DepthPreviewParams.x - DepthPreviewParams.y + 0.5, 0.0, 1.0); + #if __VERSION__ == 120 - gl_FragColor = vec4(r, g, b, 1.0); + gl_FragColor = vec4(vec3(intensity), 1.0); #else - fragColor = vec4(r, g, b, 1.0); + fragColor = vec4(vec3(intensity), 1.0); #endif } else diff --git a/mods/ts/chrome/ingame-player.yaml b/mods/ts/chrome/ingame-player.yaml index 6fa25c2264..0e99d17b63 100644 --- a/mods/ts/chrome/ingame-player.yaml +++ b/mods/ts/chrome/ingame-player.yaml @@ -2,6 +2,13 @@ Container@PLAYER_WIDGETS: Logic: LoadIngameChatLogic Children: Container@CHAT_ROOT: + LogicKeyListener@DEPTHPREVIEW_KEYHANDLER: + Logic: DepthPreviewHotkeysLogic + ToggleDepthPreviewKey: ToggleDepthPreview + IncreaseDepthPreviewContrastKey: IncreaseDepthPreviewContrast + DecreaseDepthPreviewContrastKey: DecreaseDepthPreviewContrast + IncreaseDepthPreviewOffsetKey: IncreaseDepthPreviewOffset + DecreaseDepthPreviewOffsetKey: DecreaseDepthPreviewOffset LogicKeyListener@CONTROLGROUP_KEYHANDLER: Logic: ControlGroupLogic LogicTicker@SIDEBAR_TICKER: diff --git a/mods/ts/chrome/settings-hotkeys.yaml b/mods/ts/chrome/settings-hotkeys.yaml new file mode 100644 index 0000000000..501571e52e --- /dev/null +++ b/mods/ts/chrome/settings-hotkeys.yaml @@ -0,0 +1,169 @@ +Container@HOTKEYS_PANEL: + Logic: HotkeysSettingsLogic + HotkeyGroups: + Game Commands: + Template: TWO_COLUMN + Types: OrderGenerator, World, Menu + Viewport Commands: + Template: TWO_COLUMN + Types: Viewport + Observer / Replay Commands: + Template: TWO_COLUMN + Types: Observer, Replay + Unit Commands: + Template: THREE_COLUMN + Types: Unit + Unit Stance Commands: + Template: TWO_COLUMN + Types: Stance + Production Commands: + Template: THREE_COLUMN + Types: Production, ProductionSlot + Support Power Commands: + Template: THREE_COLUMN + Types: SupportPower + Music Commands: + Template: TWO_COLUMN + Types: Music + Chat Commands: + Template: TWO_COLUMN + Types: Chat + Depth Preview Debug: + Template: TWO_COLUMN + Types: DepthDebug + Width: PARENT_RIGHT - 10 + Height: PARENT_BOTTOM + Children: + ScrollPanel@HOTKEY_LIST: + X: 15 + Y: 40 + Width: PARENT_RIGHT - 30 + TopBottomSpacing: 4 + ItemSpacing: 4 + Height: 190 + Children: + ScrollItem@HEADER: + BaseName: scrollheader + Width: 528 + Height: 13 + Visible: false + Children: + Label@LABEL: + Font: TinyBold + Width: PARENT_RIGHT + Height: 10 + Align: Center + ScrollItem@HEADER: + Width: 528 + Height: 13 + Visible: false + Children: + Label@LABEL: + Font: TinyBold + Width: PARENT_RIGHT + Height: 10 + Align: Center + Container@TEMPLATES: + Children: + Container@TWO_COLUMN: + Width: 262 + Height: 25 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 85 + Height: 25 + Align: Right + Button@HOTKEY: + X: PARENT_RIGHT - WIDTH + Width: 80 + Height: 25 + Align: Left + TooltipContainer: SETTINGS_TOOLTIP_CONTAINER + Container@THREE_COLUMN: + Width: 173 + Height: 25 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 84 + Height: 25 + Align: Right + Button@HOTKEY: + X: PARENT_RIGHT - WIDTH + 1 + Width: 80 + Height: 25 + Align: Left + TooltipContainer: SETTINGS_TOOLTIP_CONTAINER + Background@HOTKEY_DIALOG_ROOT: + X: 15 + Y: 230 + Width: PARENT_RIGHT - 30 + Height: 65 + Background: dialog3 + Children: + Label@HOTKEY_LABEL: + X: 15 + Y: 19 + Width: 219 - 15 - 10 + Height: 25 + Font: Bold + Align: Right + HotkeyEntry@HOTKEY_ENTRY: + X: 219 + Y: 20 + Width: 170 + Height: 25 + Container@NOTICES: + X: 219 + Y: 42 + Width: 170 + Height: 25 + Children: + Label@DEFAULT_NOTICE: + Width: PARENT_RIGHT + Height: PARENT_BOTTOM + Font: Tiny + Align: Left + Text: This is the default + Label@ORIGINAL_NOTICE: + Width: PARENT_RIGHT + Height: PARENT_BOTTOM + Font: Tiny + Align: Left + Text: The default is "{0}" + Label@DUPLICATE_NOTICE: + Width: PARENT_RIGHT + Height: PARENT_BOTTOM + Font: Tiny + Align: Left + Text: This is already used for "{0}" + Button@OVERRIDE_HOTKEY_BUTTON: + X: PARENT_RIGHT - 3 * WIDTH - 30 + Y: 20 + Width: 70 + Height: 25 + Text: Override + Font: Bold + Button@CLEAR_HOTKEY_BUTTON: + X: PARENT_RIGHT - 2 * WIDTH - 30 + Y: 20 + Width: 65 + Height: 25 + Text: Clear + Font: Bold + TooltipText: Unbind the hotkey + TooltipContainer: SETTINGS_TOOLTIP_CONTAINER + TooltipTemplate: SIMPLE_TOOLTIP + Button@RESET_HOTKEY_BUTTON: + X: PARENT_RIGHT - WIDTH - 20 + Y: 20 + Width: 65 + Height: 25 + Text: Reset + Font: Bold + TooltipText: Reset to default + TooltipContainer: SETTINGS_TOOLTIP_CONTAINER + TooltipTemplate: SIMPLE_TOOLTIP diff --git a/mods/ts/hotkeys.yaml b/mods/ts/hotkeys.yaml index c3b8928368..3ebba61841 100644 --- a/mods/ts/hotkeys.yaml +++ b/mods/ts/hotkeys.yaml @@ -21,3 +21,23 @@ ProductionTypeAircraft: U PowerDown: X Description: Power-down mode Types: OrderGenerator, Player + +DecreaseDepthPreviewContrast: LEFTBRACKET SHIFT + Description: Decrease Contrast + Types: DepthDebug + +IncreaseDepthPreviewContrast: RIGHTBRACKET SHIFT + Description: Increase Contrast + Types: DepthDebug + +DecreaseDepthPreviewOffset: SEMICOLON SHIFT + Description: Decrease Offset + Types: DepthDebug + +IncreaseDepthPreviewOffset: QUOTE SHIFT + Description: Increase Offset + Types: DepthDebug + +ToggleDepthPreview: BACKSLASH SHIFT + Description: Toggle Preview + Types: DepthDebug diff --git a/mods/ts/mod.yaml b/mods/ts/mod.yaml index ded61ad454..973f8dcdb0 100644 --- a/mods/ts/mod.yaml +++ b/mods/ts/mod.yaml @@ -150,7 +150,7 @@ ChromeLayout: common|chrome/settings-display.yaml common|chrome/settings-audio.yaml common|chrome/settings-input.yaml - common|chrome/settings-hotkeys.yaml + ts|chrome/settings-hotkeys.yaml common|chrome/settings-advanced.yaml common|chrome/credits.yaml common|chrome/lobby.yaml