- Home /
Scripting Shader: How to control Alpha with slider?
I'm creating a skybox with physical spheres. And I duplicated the Unlit>Transparent shader to create a skybox shader. I have a color for brightness and a slider to control the alpha (from 0 to 1). The idea is manipulate the slider in other scripts to create the blend effect and see the other skybox through the first one.
But I have no idea how to script shaders. I look around and learned a bit from the manual and other sites but still don't really understand how the code must be written.
In properties I have the slider, brightness color(or tint) and the texture:
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Brightness ("Brightness", Color) = (.5, .5, .5, 0.5)
_Alpha ("Alpha", Range(0.0,1.0)) = 1
But then I don't know how to apply the value of the slider to the alpha of the texture. The alpha should be the same on the whole image, there is no gradient or anything.
Thanks!
Answer by Seth-Bergman · Dec 28, 2012 at 02:34 PM
you can access a shader via renderer.material....
in this case, there are already several working examples out there if you look.. This one works:
http://forum.unity3d.com/threads/115455-Unlit-with-adjustable-Alpha
then you can access the alpha as you normally would, by saying:
renderer.material.color.a = someValue; //from 0 to 1;
this would work with the transparent diffuse, but not the unlit/transparent, though I don't actually know exactly why,just that the unlit/transparent doesn't have a color property.. If there is a way to script the unlit/transparent I don't know it..
I have it changing color based on the brightness color. It's more like a tint color. But still, the alpha doesn't work. I change it in the color palette to 0 but the sky is still fully visible.
you change what? it's very unclear what you are actually doing..
To change the tint, in the above linked script, you could likewise say:
renderer.material.color.r = someValue;
renderer.material.color.g = someValue;
renderer.material.color.b = someValue;
if you are accessing the color.a but it is not affecting alpha, you may need to access a different material than the one you are trying...
either way, it seems you are adding extra unnecessary properties, the _Color property inherently handles all of what you are trying to do
I tried with the Transparent/Diffuse shader. It seems to work fine. The problem is that shader is affected by fog and light. I want the same visual result as the "Unlit" shader, with the transparency working as the Transparent/Diffuse shader.
I know I have to add Fog{$$anonymous$$ode off}, lighting Off, etc, within the Pass{} block. But this shader doesn't have one. And when I add it, it just stops working and show black.
Did you notice the LIN$$anonymous$$ in my answer???!!!!!??!??
http://forum.unity3d.com/threads/115455-Unlit-with-adjustable-Alpha
here is a nice one too, in general: