📄 gui_xray_shader.lua
字号:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------- file: gui_xray_shader.lua-- brief: xray shader-- author: Dave Rodgers---- Copyright (C) 2007.-- Licensed under the terms of the GNU GPL, v2 or later.------------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:GetInfo() return { name = "XrayShader", desc = "XrayShader", author = "trepan", date = "Jul 15, 2007", license = "GNU GPL, v2 or later", layer = 0, enabled = false -- loaded by default? }end------------------------------------------------------------------------------------------------------------------------------------------------------------------ Automatically generated local definitionslocal GL_ONE = GL.ONElocal GL_ONE_MINUS_SRC_ALPHA = GL.ONE_MINUS_SRC_ALPHAlocal GL_SRC_ALPHA = GL.SRC_ALPHAlocal glBlending = gl.Blendinglocal glColor = gl.Colorlocal glCreateShader = gl.CreateShaderlocal glDeleteShader = gl.DeleteShaderlocal glDepthTest = gl.DepthTestlocal glFeature = gl.Featurelocal glGetShaderLog = gl.GetShaderLoglocal glPolygonOffset = gl.PolygonOffsetlocal glSmoothing = gl.Smoothinglocal glUnit = gl.Unitlocal glUseShader = gl.UseShaderlocal spEcho = Spring.Echolocal spGetAllFeatures = Spring.GetAllFeatureslocal spGetTeamColor = Spring.GetTeamColorlocal spGetTeamList = Spring.GetTeamListlocal spGetTeamUnits = Spring.GetTeamUnitslocal spIsUnitVisible = Spring.IsUnitVisible----------------------------------------------------------------------------------------------------------------------------------------------------------------if (not glCreateShader) then spEcho("Hardware is incompatible with Xray shader requirements") return falseend-------------------------------------------------------------------------------------------------------------------------------------------------------------------- simple configuration parameters--local edgeExponent = 2.5local doFeatures = falselocal featureColor = { 1, 0, 1 }-- looks a lot nicer, esp. without FSAA (but eats into the FPS too much)local smoothPolys = glSmoothing and true----------------------------------------------------------------------------------------------------------------------------------------------------------------local shaderfunction widget:Shutdown() glDeleteShader(shader)endfunction widget:Initialize() shader = glCreateShader({ uniform = { edgeExponent = edgeExponent, }, vertex = [[ // Application to vertex shader varying vec3 normal; varying vec3 eyeVec; varying vec3 color; uniform mat4 camera; uniform mat4 caminv; void main() { vec4 P = gl_ModelViewMatrix * gl_Vertex; eyeVec = P.xyz; normal = gl_NormalMatrix * gl_Normal; color = gl_Color.rgb; gl_Position = gl_ProjectionMatrix * P; } ]], fragment = [[ varying vec3 normal; varying vec3 eyeVec; varying vec3 color; uniform float edgeExponent; void main() { float opac = dot(normalize(normal), normalize(eyeVec)); opac = 1.0 - abs(opac); opac = pow(opac, edgeExponent); gl_FragColor.rgb = color; gl_FragColor.a = opac; } ]], }) if (shader == nil) then spEcho(glGetShaderLog()) spEcho("Xray shader compilation failed.") widgetHandler:RemoveWidget() endend-------------------------------------------------------------------------------------------------------------------------------------------------------------------- utility routine--local teamColors = {}local function SetTeamColor(teamID) local color = teamColors[teamID] if (color) then glColor(color) return end local r,g,b = spGetTeamColor(teamID) if (r and g and b) then color = { r, g, b } teamColors[teamID] = color glColor(color) return endend----------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:DrawWorld() if (smoothPolys) then glSmoothing(nil, nil, true) end glColor(1, 1, 1, 1) glUseShader(shader) glDepthTest(true) glBlending(GL_SRC_ALPHA, GL_ONE) glPolygonOffset(-2, -2) for _, teamID in ipairs(spGetTeamList()) do SetTeamColor(teamID) for _, unitID in ipairs(spGetTeamUnits(teamID)) do if (spIsUnitVisible(unitID)) then glUnit(unitID, true) end end end if (doFeatures) then glColor(featureColor) for _, featureID in ipairs(spGetAllFeatures()) do glFeature(featureID, true) end end glPolygonOffset(false) glBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glDepthTest(false) glUseShader(0) glColor(1, 1, 1, 1) if (smoothPolys) then glSmoothing(nil, nil, false) endend ----------------------------------------------------------------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -