Skip to content

Commit cb4a32e

Browse files
committed
play with FoW
1 parent e9bca4c commit cb4a32e

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

scen_edit/view/map/terrain_settings_editor.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ function TerrainSettingsEditor:init()
194194
local shaderDef = VFS.Include(shaderFile, envTbl)
195195
Spring.SetMapShader(shaderDef.shader, shaderDef.shader)
196196
SB.DrawGroundPreForward = shaderDef.DrawGroundPreForward
197+
SB.DrawWorld = shaderDef.DrawWorld
197198
end)
198199
if not success then
199200
Log.Error(msg)

scen_edit/widget.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ function widget:DrawWorld()
223223
end
224224
SB.executeDelayed("DrawWorld")
225225
SB.displayUtil:Draw()
226+
-- HACK
227+
if SB.DrawWorld ~= nil then
228+
SB.DrawWorld()
229+
end
226230
end
227231

228232
function widget:DrawWorldPreUnit()
@@ -233,6 +237,7 @@ function widget:DrawWorldPreUnit()
233237
end
234238

235239
function widget:DrawGroundPreForward()
240+
-- HACK
236241
if SB.DrawGroundPreForward ~= nil then
237242
SB.DrawGroundPreForward()
238243
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#version 110
2+
3+
uniform sampler2D customSampler;
4+
uniform sampler2D texSampler;
5+
uniform vec2 customSamplerSize;
6+
7+
varying vec4 vertexWorldPos;
8+
varying vec2 texCoors;
9+
10+
void main(void) {
11+
float fow = texture2D(customSampler, vertexWorldPos.xz / customSamplerSize).r;
12+
gl_FragColor = texture2D(texSampler, texCoors);
13+
gl_FragColor.rgb = mix(vec3(0.0), gl_FragColor.rgb, fow);
14+
15+
// show height (since we don't have any lighting)
16+
float heightFactor = max(0.5, vertexWorldPos.y / 100.0);
17+
heightFactor = min(1.5, heightFactor);
18+
gl_FragColor.rgb *= heightFactor;
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local dir = Path.ExtractDir(__path__)
2+
local shader = Shaders.Compile({
3+
vertex = VFS.LoadFile(Path.Join(dir, "fow.vert")),
4+
fragment = VFS.LoadFile(Path.Join(dir, "fow.frag")),
5+
uniformInt = {
6+
texSampler = 0,
7+
customSampler = 1,
8+
},
9+
uniform = {
10+
customSamplerSize = { Game.mapSizeX, Game.mapSizeZ },
11+
},
12+
}, "Custom Map Shader")
13+
14+
15+
local texInfo = gl.TextureInfo("$info_los")
16+
local FOW_TEX = gl.CreateTexture(texInfo.xsize, texInfo.ysize, {
17+
fbo = true,
18+
min_filter = GL.LINEAR,
19+
mag_filter = GL.LINEAR,
20+
wrap_s = GL.CLAMP_TO_EDGE,
21+
wrap_t = GL.CLAMP_TO_EDGE,
22+
})
23+
24+
local function Blit(tex1, tex2)
25+
gl.Blending("add")
26+
gl.Texture(tex1)
27+
gl.RenderToTexture(tex2, function()
28+
gl.TexRect(-1,-1, 1, 1, 0, 0, 1, 1)
29+
end)
30+
gl.Texture(false)
31+
end
32+
33+
local function DrawWorld()
34+
Blit("$info_los", FOW_TEX)
35+
end
36+
37+
local function DrawGroundPreForward()
38+
gl.Texture(1, FOW_TEX)
39+
end
40+
41+
return {
42+
shader = shader,
43+
DrawGroundPreForward = DrawGroundPreForward,
44+
DrawWorld = DrawWorld,
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 110
2+
3+
#define SMF_TEXSQR_SIZE 1024.0
4+
5+
uniform ivec2 texSquare;
6+
7+
varying vec4 vertexWorldPos;
8+
varying vec2 texCoors;
9+
10+
void main(void) {
11+
vertexWorldPos = gl_Vertex;
12+
texCoors = (floor(gl_Vertex.xz) / SMF_TEXSQR_SIZE) - vec2(texSquare);
13+
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
14+
}

0 commit comments

Comments
 (0)