Jump to content

Efficient fog of war


ooflorent
 Share

Recommended Posts

Basically this is what I do:

var viewport = new Rectangle(-camera.x, -camera.y, SCREEN_WIDTH, SCREEN_HEIGHT);var fogLayer = new PIXI.DisplayObjectContainer();var fog = [];function initializeFog() {  var fogSprite;  for (var y = WORLD_HEIGHT; y-- {    fog[y] = [];    for (var x = WORLD_WIDTH; x-- {      // `fogTexture` is a 16x16 RenderTexture      fogLayer.addChild(fogSprite = new PIXI.Sprite(fogTexture));      fog[y][x] = {        sprite: fogSprite,        bounds: new Rectangle(fogSprite.x = x * 16, fogSprite.y = y * 16, 16, 16)      };    }  }}// Called every framesfunction updateFog() {  // Culling  for (var y = WORLD_HEIGHT; y-- {    for (var x = WORLD_WIDTH; x-- {      var tile = this.f[y][x];      tile.sprite.visible = tile.bounds.overlaps(viewport);      // Update fog tile opacity      tile.sprite.alpha = Math.random(); // Dummy test    }  }}

Even without a large world, the above code has a big performance impact (FPS rate drops from 60 to 50, which is huge).

My game uses a CanvasRenderer. Not sure if I do it properly...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...