Is writing an image buffer efficient for creating fog of war?


(Lyeb) #1

Hey hey people,

Basis: Map each tile in the grid to a pixel on a texture, slap it on a plane.
Manipulate texture memory to generate black, grey and white (transparent with my setup).
What for? Fog of war. Control which tiles are hiden, explored and visible.

Reference code that I’ve tested enough to know this is possible.

from bge import logic,texture
obj = logic.getCurrentController().owner

mat_id = texture.materialID(obj,texturename)
tex = texture.Texture(obj,mat_id)
tex.source = texture.ImageBuff()

size = 64*64*3 #texsize times three
buff = bytearray(size)

def main(cont):
    buff[0] = 0 #R
    buff[1] = 0 #G
    buff[2] = 0 #B
    # ^ color of first pixel

    tex.source.load(buff,64,64)
    tex.refresh(True)

The question is, how well could this work on a larger scale?
Am I good building a fog of war system around this or should I keep looking?
Any ideas?


(BluePrintRandom) #2

what about vertex alpha of the land ?

use it to set the material fog to fog material?

use a KDTree to get the points in radius of the player and paint the vertex color alpha to a point, then set in in a ‘explored’ list

Kdtree.find_range( radius, point)


(Lyeb) #3

You feel that’s a more viable solution? I thought about something in that vein initially, what bugs me is a larger map would equal a larger vertex count. Not the biggest problem as I’m working solely with sprites, but still, I’d like this project to run on toasters and whatnot.


(BluePrintRandom) #4

the greater the density the more dots will be in a radius however if you put them in a pop list you can use

def get_list(near, filled):
    return [entry for entry in near if entry[1] not in filled]

when a area has been explored (alpha is 1 in vertex color) add its index to filled list

near = kd.find_range(radius, point)
filled = own[‘filled’]
pointsToColor = get_list(near,filled)

this way you don’t try and color points that have already been visited.


(BluePrintRandom) #5

here you are :smiley:kd_fog.blend (2.9 MB)


(Lyeb) #6

Thanks a ton c: I’ll give it a spin.


(BluePrintRandom) #7

No problem :smiley:

Animated_fog.blend (3.0 MB)

threw a bunch more nodes together and animated the fog w/noise and some waves


(Lyeb) #8

Alright, BPR’s way seems to be the answer here.

I was still curious about the whole bytearray affaire though, so I gave it a shot.
This is what it looks like. Not the best I’ve seen, and performance is more or less the same.
It also does require more logic, I’m pulling a lot of tricks here to get it working.

But hey, only two 4-vert planes total for ground and fog :B all around, not so bad.