5
\$\begingroup\$

I'm trying to make a simple Fog of War effect for my 2d game, similar to these:

enter image description here

this.

I want to have a sprite layer of black and then have a sprite to cut out itself from the black layer, leaving a part of the black sprite transparent. And if I have two cut out sprites they will blend like the image I linked.

Anyone got any ideas?

\$\endgroup\$
1
\$\begingroup\$

You need to write a shader, in which you pass two textures:

  • camera rendertexture
  • grayscale mask of light and shadow

You can get the second one in rendertexture from the second camera that uses OnPreCull method to adjust some render settings and reverts all back on OnPostRender. Or using second camera's Culling Mask.

In the shader you blend the two textures pixel by pixel and render the result to screen.

You can also read my post about rendertextures and rendering from a shader to screen: Unity3D - override main camera rendering for compositing effect.

\$\endgroup\$
-2
\$\begingroup\$

There are two hacks to achieve this effect

1) Make a big black sprite with a hole cut out in the middle. Make this sprite be on top of everything.

2) Change the material of all sprites to Sprite-Diffuse. And add a light source as the child of the player.

\$\endgroup\$
  • 1
    \$\begingroup\$ 1) won't achieve the merging of two lit circles as shown in the first animation above. 2) would need a separate palletization pass after lighting to give the stepped falloff we see in the examples, and would risk creating an over-bright spot at the very center or where two light sources overlap. \$\endgroup\$ – DMGregory Dec 16 '15 at 14:01
  • \$\begingroup\$ What about light cookies ?? \$\endgroup\$ – Hash Buoy Dec 17 '15 at 10:01
  • \$\begingroup\$ If you can generate a texture with the right cutout for two merging holes in the fog of war, then you can just overlay that directly on your scene using a quad or blit, without needing a literal light object for it. It's making that texture that's the challenge. \$\endgroup\$ – DMGregory Dec 17 '15 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.