- Home /
Add vertex transparency to shader
I still have a lot to learn when it comes to shaders, but I just added something to my menus which allows me to create a simple rectangle mesh during runtime and assign color values to its vertices to make a gradient. I don't need any sort of lighting or effects, I just need to be able to have an alpha gradient from one side to the other. Ideally this should just be as basic as possible to run nice and fast for mobile processors.
Here's the code that I picked up to make the shader currently in use:
Material mat = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
I was pretty surprised that it's really that simple to make a vertex-only shader. But this one doesn't support alpha on the vertex colors. Is there any way to do that? It doesn't seem like any of the default shaders support vertex alpha either.
Edit: I did manage to find a shader that works for my needs... but it only does something on PC. When I use Android it suddenly fails on me. The shader just doesn't render at all.
Here's the shader I'm using now:
Shader "Custom/VertexAdd"
{
Subshader
{
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
Blend one one
BindChannels
{
Bind "vertex", vertex
Bind "color", color
}
Pass
{
}
}
}
Trying to dig into the log files from DDMS and I unearthed this:
04-13 01:33:53.841: I/Unity(16620): NullReferenceException 04-13 01:33:53.841: I/Unity(16620): at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in :0 04-13 01:33:53.841: I/Unity(16620): at MenuSelection.CreateGradient () [0x001f8] in C:\Users\Max\Documents\PuzzleByte\Assets\Custom Assets\Scripts\Menus\MenuSelection.cs:63
So the shader is coughing up errors. In fact, while that scene is open it's just pouring that error out repeatedly. I tried assigning default materials to see if it was a code error rather than a shader one, but they showed up in Android just as one would expect.
Here's the kicker, though... all this afternoon I've been replacing that with a billion different shaders, trying to get that effect to work. I FINALLY had some success with an extremely similar shader. You may know it as "mobile/particles/additive". And I'm FUMING right now, that it was so simple.
But here's the thing: if I cut out the texture part, the shader continues to work just fine in the editor but it stops showing up on Android. It's literally the one magical ingredient that I needed, and I don't even put a texture in there. And even worse, the surfaces I'm putting it on have no UVs because they're being generated in runtime, and I haven't the faintest clue how to generate UVs for them. (nor do I have any need to do so) And now the editor is the one suffering, because it's throwing out a constant stream of messages about texture coordinates.
So I guess you could say my problem is "solved" now because I have something working, but I REALLY want to know why the solution was so simple and totally unrelated...
Hello bud, With respect to mobile platforms, it is probably best to use an additive shader for alpha transparency as alpha-blended shaders can be fairly hefty etc. Have you looked at the Unity in-built mobile shader set. Thses may save you alot of work your self, or you could adapt them an alpha based one to use half values to plug in to the lighting model(obviously withthe result of a reduction in lighting quality/accuracy n all that). Hop ethat helps some and here is a link to the Unity Shader Docs online, a really helpful resource at all times for any shader adventures, not to mention here too, which is the Unify Community Shaders Page, well worth a good look and test of. Take care bud Gruffy
I ended up adapting the existing shader into a more legible format with its own file so I could actually work with it a bit. But additive blending seems to be good enough for this project. I can just blend the desired color with some amount of black to get that gradient effect I was looking for.
I've had several misadventures with trying to learn shaders, but never really had much stick with me. I can read them and understand generally what's going on but a lot of it still tends to go over my head. I pick up just a bit more every time.
Such is the plight of Shader learning for some reason, a bit like a rite of passage, they seem to elude your until one day this moment of clarity hits you and you realise, though platform dependent admittedly(D3D, OGL etc) that they all have a very small set of keywords and with a list by your desk of those and some general knowledge based on general program$$anonymous$$g theory (dare I say including maths, though I dunno because in fairness I`m a S#!tty at math overall)....
But ultimately, through other shaders, like on the Unify community wiki etc, I noticed(as is often the case with code generally) that many of the same functions and calls were found with respect to lighting calculations made inside any methods. Suddenly, they started to seem not so bad after all. Like everything codey for me, if I use it, I get used to it, if I leave it for a while, often I find myself returning with a small cloud of forgetfulness for some of the simplest of stuff. A few hours later i`m motoring again, but holding all these different API`s in ya head can make for a messy scenario brain-wise.... SO....
I use things like these to help me refresh during use, I dont know if it`d help you too, but you never know (particularly, like I said, with Shaders these lil things proved invaluable) Not to mention this link too, though you prob already foudn this on your way...ShaderlabReference Take care bud and happy shading/coding Gruffy
Okay, so the shader worked in the editor, but totally didn't do anything on Android. After wasting my whole day on this ridiculous problem, the solution was even more ridiculous: add in a texture for no reason and it just works. Which really throws a wrench into my procedurally generated rectangles because it means I need to figure out how to give them UVs just to keep Unity from slowing down the editor with warning messages about the textures I don't need.