- Home /
Projector - blob shadow. How to change the material's alpha channel C#
Hi, Im using a projector for a blob shadow and I need to set up a script that allows me to lerp the shadow brightness from light to dark. How can I do this?
I know the material the blob shadow uses must have an alpha channel, so is that what I need to control to get from dark to light?
Answer by Sajidfarooq · Aug 18, 2013 at 04:15 PM
You could modify the materials colour directly, without going through the shader:
Color newColour = GetComponent<Projector>().material.color;
newColour.a = 0.1f;
GetComponent<Projector>().material.color = newColour;
This will change your materials colour even outside play mode. Not a big deal, because you could change it back from the inspector if you want.
Just a note, if you end up using a shader with the material that doesn't have a named _Color property, this method won't work. You'd have to use SetColor ins$$anonymous$$d.
@whebert: Correct, but the OP specifically mentioned the blob projector, which does have that property.
Ah, thought the OP was using the Blob Shadow Projector, which uses the "Projector/$$anonymous$$ultiply" shader by default, which has no color property at all.
@whebert: Actually I just checked, and indeed the Shadow Projector doesnt have a colour property. I was using the light projector.
Answer by whebert · Aug 18, 2013 at 04:09 PM
Sounds like you might be able to do something like that by modifying your projector material's color - depending on what shader it is using.
Assuming modifying the color will work, including modifying the alpha channel of the color, you could do something like the following. Also, you have to know the name of the color property in the shader. Most of the time the main color is named "_Color" and you can access it with myMaterial.color. But in case it is named something else, you can do:
// Get the current color value
Color color = myMaterial.GetColor("_Color"); // or "_TintColor", whatever the shader uses.
// Modify the color however you want, the whole color, just a channel like so, whatever.
color.a = .1f;
// Set the color back
myMaterial.SetColor("_Color", color);