Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create info metal view shader and widget #4060

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions luaui/Widgets/Shaders/info_metal.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#version 420
#extension GL_ARB_uniform_buffer_object : require
#extension GL_ARB_shading_language_420pack: require

// This shader is (c) Beherith ([email protected]), Licensed under the MIT License

uniform sampler2D metalTex;
uniform sampler2D metalExtractionTex;
uniform sampler2D mapDepths;

uniform float alpha = 1.0;
uniform float minimap = 0.0;
uniform float flipMiniMap = 0.0;

//__DEFINES__
//__ENGINEUNIFORMBUFFERDEFS__

in DataVS {
vec4 texCoord;
};

out vec4 fragColor;

#if 0
vec2 CubicSampler(vec2 uvsin, vec2 texdims){
vec2 r = uvsin * texdims - 0.5;
vec2 tf = fract(r);
vec2 ti = r - tf;
tf = tf * tf * (3.0 - 2.0 * tf);
return (tf + ti + 0.5)/texdims;
}
#endif

void main(void) {
vec2 infoUV = texCoord.xy;

if (minimap < 0.5) { // world space
float mapdepth = texture(mapDepths, infoUV.xy).x;
vec4 fragWorldPos = vec4( vec3(infoUV.xy * 2.0 - 1.0, mapdepth), 1.0);

// reconstruct view pos:
fragWorldPos = cameraViewProjInv * fragWorldPos;
fragWorldPos.xyz = fragWorldPos.xyz / fragWorldPos.w; // YAAAY this works!

//clamp fragWorldPos to the infolos tex bounds:
infoUV = clamp(fragWorldPos.xz / mapSize.xy, 0, 1);

}else{
//minimap space is flipped fuck if i know why
infoUV.y = 1.0 - infoUV.y;
if (flipMiniMap > 0.5){
infoUV = 1.0 - infoUV;
}
}

vec2 texDims = vec2(METALTEXX, METALTEXY);
vec2 centroids = infoUV.xy * texDims;

vec4 metalSample = texture2D(metalTex, infoUV).rgba;
//vec4 metalSample = texture2D(metalTex, CubicSampler(infoUV, texDims)).rgba;
vec4 metalExtractionSample = texture2D(metalExtractionTex, infoUV).rgba;

vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0);
outColor.rgb = mix(vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 1.0), metalSample.r); // metalness
outColor.rgb = mix(outColor.rgb, vec3(1.0, 0.0, 0.0), metalExtractionSample.r); // extractedness
if (minimap < 0.5) { // world

outColor.a = 1.0 * metalSample.r * alpha; // alpha
}else { // minimap

outColor.rgb = mix(vec3(0), outColor.rgb, metalSample.r);
outColor.a = 1.0 * clamp(metalSample.r, 0.75, 1.0); // alpha

}

fragColor = outColor;
}
19 changes: 19 additions & 0 deletions luaui/Widgets/Shaders/info_metal.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 430


// This shader is (c) Beherith ([email protected]), Licensed under the MIT License

//__DEFINES__

//__ENGINEUNIFORMBUFFERDEFS__

layout (location = 0) in vec4 position; // [-1,1], [0,1] , xyuv

out DataVS {
vec4 texCoord;
};

void main(void) {
texCoord = position.zwzw;
gl_Position = vec4(position.xy * 1.0, 0.00, 1);
}
25 changes: 0 additions & 25 deletions luaui/Widgets/cmd_area_mex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ end
VFS.Include("luarules/configs/customcmds.h.lua")

local spGetActiveCommand = Spring.GetActiveCommand
local spGetMapDrawMode = Spring.GetMapDrawMode
local spGetUnitPosition = Spring.GetUnitPosition
local spSendCommands = Spring.SendCommands
local taremove = table.remove
Expand Down Expand Up @@ -172,30 +171,6 @@ function widget:CommandNotify(id, params, options)
end


-- Adjust map view mode as needed
function widget:Update(dt)
local _, cmd, _ = spGetActiveCommand()
if cmd == CMD_AREA_MEX then
if spGetMapDrawMode() ~= 'metal' then
if Spring.GetMapDrawMode() == "los" then
retoggleLos = true
end
spSendCommands('ShowMetalMap')
toggledMetal = true
end
else
if toggledMetal then
spSendCommands('ShowStandard')
if retoggleLos then
Spring.SendCommands("togglelos")
retoggleLos = nil
end
toggledMetal = false
end
end
end


function widget:SelectionChanged(sel)
selectedUnits = sel
end
Expand Down
1 change: 1 addition & 0 deletions luaui/Widgets/gui_chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ local autocompleteCommands = {
'shadows',
'sharedialog',
'showelevation',
'showinfometal',
'showmetalmap',
'showpathcost',
'showpathflow',
Expand Down
4 changes: 1 addition & 3 deletions luaui/Widgets/gui_geothermalspots.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ local spIsGUIHidden = Spring.IsGUIHidden
local spGetUnitsInSphere = Spring.GetUnitsInSphere
local spGetUnitDefID = Spring.GetUnitDefID
local spGetGroundHeight = Spring.GetGroundHeight
local spGetMapDrawMode = Spring.GetMapDrawMode

local spots = {}
local previousOsClock = os.clock()
Expand Down Expand Up @@ -376,8 +375,7 @@ end


function widget:DrawWorldPreUnit()
local mapDrawMode = spGetMapDrawMode()
if metalViewOnly and mapDrawMode ~= 'metal' then return end
if metalViewOnly and not WG.metalView then return end
if chobbyInterface then return end
if spIsGUIHidden() then return end

Expand Down
136 changes: 136 additions & 0 deletions luaui/Widgets/gui_info_metal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
function widget:GetInfo()
return {
name = "Info Metal View",
version = 3,
desc = "Draws Metal Amount and Metal Extraction maps",
author = "Beherith",
date = "2024.12.22",
license = "GPL V2",
layer = 10001,
enabled = true,
depends = {"shaders"},
}
end

-- Author: Beherith ([email protected])
-- TODO:
-- [x] USE BICUBIC SAMPLER!
-- nope, tried, suxx
-- [x] Flip minimap?
-- [x] calc tex sizes from map size
-- [ ] override F4 keysym
-- [ ] fade alpha in

local autoreload = true
local alpha = 0.75

local luaShaderDir = "LuaUI/Widgets/Include/"
local LuaShader = VFS.Include(luaShaderDir.."LuaShader.lua")
VFS.Include(luaShaderDir.."instancevbotable.lua")

local getMiniMapFlipped = VFS.Include("luaui/Widgets/Include/minimap_utils.lua").getMiniMapFlipped

local fullScreenQuadVAO
local infoMetalShader

local shaderSourceCache = {
vssrcpath = "LuaUI/Widgets/Shaders/info_metal.vert.glsl",
fssrcpath = "LuaUI/Widgets/Shaders/info_metal.frag.glsl",
uniformInt = {
metalTex = 0,
metalExtractionTex = 1,
mapDepths = 2,
},
uniformFloat = {
alpha = 0.5,
minimap = 0,
flipMiniMap = getMiniMapFlipped() and 1 or 0,
},
shaderName = "Info Metal View GL4",
shaderConfig = {
METALTEXX = Game.mapSizeX/16,
METALTEXY = Game.mapSizeZ/16,
},
}

local callins = {'DrawWorldPreUnit', 'DrawInMiniMap'}
if autoreload then
callins[#callins+1] = 'DrawScreen'
end

local viewEnabled = true -- will disable at Initialize

local function ShowInfoMetal(cmd, line, words, playerID)
if #words > 0 then
local enabled = (words[1] == '1')
if enabled == viewEnabled then return end

viewEnabled = enabled
else
viewEnabled = not viewEnabled
end
for _, callinName in pairs(callins) do
if viewEnabled then
widgetHandler:UpdateCallIn(callinName)
else
widgetHandler:RemoveCallIn(callinName)
end
end
WG.metalView = viewEnabled
end

function widget:Initialize()
WG.metalView = false
infoMetalShader = LuaShader.CheckShaderUpdates(shaderSourceCache)
shaderCompiled = infoMetalShader:Initialize()
if not shaderCompiled then Spring.Echo("Failed to compile Info Metal View GL4") end

fullScreenQuadVAO = MakeTexRectVAO()-- -1, -1, 1, 0, 0,0,1, 0.5

widgetHandler:AddAction("showinfometal", ShowInfoMetal, nil, "t") -- 'p' is coming from somewhere else
ShowInfoMetal(nil, nil, {"0"})
end

function widget:Shutdown()
widgetHandler:RemoveAction("showinfometal")
end

function DrawInfoMetal(inminimap)
gl.Texture(0, "$info:metal")
gl.Texture(1, "$info:metalextraction")
if not inminimap then
gl.Texture(2, "$map_gbuffer_zvaltex")
end

gl.Blending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
gl.Culling(false) -- ffs
gl.DepthTest(false)
gl.DepthMask(false) --"BK OpenGL state resets", default is already false, could remove
infoMetalShader:Activate()
infoMetalShader:SetUniformFloat("alpha", alpha)
infoMetalShader:SetUniformFloat("minimap", inminimap and 1 or 0)
infoMetalShader:SetUniformFloat("flipMiniMap", getMiniMapFlipped() and 1 or 0)
fullScreenQuadVAO:DrawArrays(GL.TRIANGLES)
infoMetalShader:Deactivate()
gl.DepthTest(true)
gl.Texture(0, false)
gl.Texture(1, false)
if not inminimap then
gl.Texture(2, false)
end
end

function widget:DrawWorldPreUnit()
if autoreload then
infoMetalShader = LuaShader.CheckShaderUpdates(shaderSourceCache) or infoMetalShader
end
DrawInfoMetal(false)
end

function widget:DrawInMiniMap()
DrawInfoMetal(true)
end

function widget:DrawScreen()
if infoMetalShader.DrawPrintf then infoMetalShader.DrawPrintf() end
end
2 changes: 1 addition & 1 deletion luaui/Widgets/gui_keybind_info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ local function refreshText()
{ type = lineType.key, key = getActionHotkey('showelevation'), text = Spring.I18N('ui.keybinds.cameraModes.heightmap') },
{ type = lineType.key, key = getActionHotkey('showpathtraversability'), text = Spring.I18N('ui.keybinds.cameraModes.traversability')},
{ type = lineType.key, key = getActionHotkey('lastmsgpos'), text = Spring.I18N('ui.keybinds.cameraModes.mapmarks') },
{ type = lineType.key, key = getActionHotkey('showmetalmap'), text = Spring.I18N('ui.keybinds.cameraModes.resourceSpots') },
{ type = lineType.key, key = getActionHotkey('showinfometal'), text = Spring.I18N('ui.keybinds.cameraModes.resourceSpots') },
{ type = lineType.key, key = getActionHotkey('hideinterface'), text = Spring.I18N('ui.keybinds.cameraModes.interface') },
{ type = lineType.blank },
{ type = lineType.title, text = Spring.I18N('ui.keybinds.sound.title') },
Expand Down
42 changes: 42 additions & 0 deletions luaui/Widgets/gui_metalmode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function widget:GetInfo()
return {
name = "Toggle metal view",
desc = "Toggles metal view when active command changes",
license = "GNU GPL, v2 or later",
layer = -50,
enabled = true
}
end

VFS.Include("luarules/configs/customcmds.h.lua")

local isMex = {}

local function initMexes()
for uDefID, uDef in pairs(UnitDefs) do
if uDef.extractsMetal > 0 then
isMex[uDefID] = true
end
end
end

function widget:Initialize()
if Spring.SetAutoShowMetal then
Spring.SetAutoShowMetal(false)
end

local success, mapinfo = pcall(VFS.Include, "mapinfo.lua")

if mapinfo and mapinfo["autoshowmetal"] then
initMexes()
else
widgetHandler:RemoveWidget()
end
end

function widget:ActiveCommandChanged(cmdID)
local wantsMetal = (cmdID == CMD_AREA_MEX) or (cmdID and isMex[-cmdID]) or false
local onoff = wantsMetal and "1" or "0"

Spring.SendCommands("showinfometal " .. onoff)
end
4 changes: 1 addition & 3 deletions luaui/Widgets/gui_metalspots.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ local spIsSphereInView = Spring.IsSphereInView
local spGetUnitsInSphere = Spring.GetUnitsInSphere
local spGetUnitDefID = Spring.GetUnitDefID
local spGetGroundHeight = Spring.GetGroundHeight
local spGetMapDrawMode = Spring.GetMapDrawMode
local spIsUnitAllied = Spring.IsUnitAllied

local mySpots = {} -- {spotKey = {x = spot.x, y= spGetGroundHeight(spot.x, spot.z), z = spot.z, value = value, scale = scale, occupied = occupied, t = currentClock, ally = false, enemy = false, instanceID = "1024_1023"}}
Expand Down Expand Up @@ -511,8 +510,7 @@ function widget:GameFrame(gf)
end

function widget:DrawWorldPreUnit()
local mapDrawMode = spGetMapDrawMode()
if metalViewOnly and mapDrawMode ~= 'metal' then return end
if metalViewOnly and not WG.metalView then return end
if chobbyInterface then return end
if Spring.IsGUIHidden() then return end

Expand Down
8 changes: 3 additions & 5 deletions luaui/Widgets/gui_pregame_build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,9 @@ local function setPreGamestartDefID(uDefID)
local isMex = UnitDefs[uDefID] and UnitDefs[uDefID].extractsMetal > 0

if isMex then
if Spring.GetMapDrawMode() ~= "metal" then
Spring.SendCommands("ShowMetalMap")
end
elseif Spring.GetMapDrawMode() == "metal" then
Spring.SendCommands("ShowStandard")
Spring.SendCommands("showinfometal 1")
else
Spring.SendCommands("showinfometal 0")
end
end

Expand Down
Loading