- Home /
Multiple alpha maps / moving alpha maps
Hello, I'm fairly new to Unity so I'm not even sure if what I want to do is even possible but here goes.
I am making a 2D game and the effect I want to create is to have a black overlay slowly fade in across the screen except for where the player is. The player is able to move horizontally across the screen while the background constantly scrolls down. I had previously achieved this affect by making a static cutout with a transparent gradient in the overlay image and moving it with the player, but it looks like we want to have multiple light sources on the screen at once and this method does not work for that.
My next approach was to create an alpha map for the black overlay and use that for each light source, but I'm not sure how to go about doing that or if it's even possible. The two things I need to be able to do are dynamically create an alpha map and add it to the overlay's material and also move the alpha map independently of the object.
Also if anyone has a better idea of how to accomplish this affect please let me know!
Thank you in advance.
Answer by DESTRUKTORR · May 16, 2013 at 11:23 PM
You'll need to look into shaders. More specifically you'll want to look into alpha masks. Take a look at unity's documentation for ShaderLab and HLSL (high level shader language). Specifically, take a look at this tutorial. What you need to do is called "alpha masking," or using an alpha channel to clip out sections of the viewport that would otherwise be rendered.
Unity supports alpha masking, but only in cutouts. If you want to have a gradiented effect, you'll need to make a texture and attach it where the cutout occurs. Alternatively, you could just use a standard black-to-transparent gradient texture in front of the camera, which would probably be a lot simpler.
I got the alpha masking to work easily enough, but I've been having a lot of issues getting the gradiented effect to work on top of it. Using a 2D overlay on top of the cutout creates some pixelation around the border where the overlay doesn't match up to the polygons of the sphere. I've been looking into creating a radial transparent gradient with shaders with little success, but I had another thought on creating the effect I want:
Is it possible to have a light affect the alpha of a material ins$$anonymous$$d of the RGB? I'm fairly new to shaders so I'm not entirely sure if that's possible or what that would look like.