Now bear with me here, I have essentially no experience with ShaderLab, outside of reading through the documentation and trying to experiment a smidgen.
I’d hoped to avoid writing any custom shaders for my current project and stick to diffuse and lightmapping, but this is something I really need to work.
I have a bunch of mountain cliffs, and I want to cliffs to fade out as they extend further down on the y axis, simulating volumetric fog (as the player views them from above).
Basically all I need is a shader that changes the alpha level of the material based on the pixel’s (or vertices) position on the y axis.
I really just wanted to know if this is something that is possible to do, or if I’d have to find another solution.
This is quite possible, although it might not be something you want to do in a shader. Shaders are a great place to do certain things if those things are graphics-related and change on a per-vertex or per-pixel basis. If your cliffs aren’t moving, you might want to consider using a shader that uses vertex or texture alpha to do transparency, instead of basing it on world position.
I did think of just using a texture for the alpha. For the map I would probably just use a basic gradient, white on top, black on the bottom, but the problem would come down to making the alpha texture fit into my current UVW map on the cliffs. The cliffs are textured using one big tiling texture, and it really wouldn’t be possible to get one UVW set to fit both the tiled texture and the alpha.
I know it’s possible to export multiple UVW sets for lightmapping, but could I have one UVW set for the texture, and another for the alpha?
Also, how would this work (if it does work!) with lightmapping? Can Unity import more than 2 UVW sets?
You don’t actually need a separate UV set. You just need a shader that allows you to use separate UV offsets (and probably a separate texture) for your alpha channel. That way you could have your main texture tiled, and your alpha channel/texture fill up the entire cliff.
Doesn’t this sound like a much better fit for vertex alpha, not that from a texture?
Personally, I’ve only bothered with painted lightmaps. I’d suggest using the 2nd UV for both the lightmap and the alpha, if you wanted to use a texture instead, but that could be rather difficult if you have one of the standard tons-o-broken-blocks baked-lightmap-focused uv maps.
Ahh sorry, I was a little unclear. The texture’s tiling settings are still 1.0/1.0, but I have the UVs scaled and set up so it tiles mutliple times across the texture…like so.
Even if this wasn’t the case, a simple gradient alpha from top to bottom would not fit the UVs I have set up, and I would have to manually make a different alpha map for every single cliff model depending on how it’s UVs are set up.
You tell me…I don’t know a whole lot about it. I’m gonna guess vertex alpha is where the alpha value is decided based on blending between two vertices…which sounds like it would work. How would this be implemented in Unity?
You hand paint your lightmaps?
But yeah, I do use the Auto unwrap for lightmapping, since I’ve never really seen a huge need for manually unwrapping a second set of UVs…I spose this would be necessary if you hand painted the maps though.
Sorry about my low skill level when it comes to shaders guys…looking into some tutorials now, and your help is really appreciated!
I don’t think you’ve got it right. Vertices have 4-component colors associated with them. The colors do get interpolated over the polygons on the way to the other vertices, if that’s what you meant.
I explain everything I think you’d need to know in the videos up to the alpha blending tutorial in the ShaderLab section. If you can blend to a solid color or the vertex colors, then you can do this in a really fast and easy to use shader. (If you need pixel lighting then I don’t recommend this series.)
Either I do or my partner does. I don’t recommend it for the type of stuff that you’d bake. It’s just a more fun approach to getting variation over tiling textures; you can also paint things that you’d never be able to bake.
Of course, I’ll have to add in lightmapping, and actually plan the models out better now that I know how to achieve this effect, but that will have to wait a bit as I’m working an awful lot in the coming few days.
And terrific tutorials Jessy, really good stuff, Shaders are actually quite interesting, had alot of fun messing around. Probably will end up going through them all, so great stuff!
Nice work, glad to help! That shader looks really clean / to the point / fast, just like I tried to teach 'em. The only thing I see that should be an issue, in its current state of completion, is:
Bind "texCoord", texCoord
…which you’ll need to have UV-based texture mapping happen. Please let me know if there’s somewhere I could have made that more clear.
Also, as it is, you’re not using the _Color variable anywhere. Leave it in if you plan on using it in a further refined version, of course.
I was messing around the the color values, and forgot to remove it. Good catch.
I didn’t have the texCoord binding in the shader, but it seemed to UV fine. Does it maybe default to the first UVW coord, or do you mean something else?
Anyway, awesome work. I do assume that you can trim the last line to
Combine texture * previous, primary
though. Also, please let me know if you figure out what LightmapMode does. I tested it a while back and messing around with it did nothing for me but crash Unity a lot.
You also may want to consider if you can avoid using a texture for the lightmap. To me, it looks like you can get some good results just by multiplying in some vertex colors (same thing as static vertex lighting, which I go over in tutorial chapter 8 ).
Absolutely no idea. I put it in there originally because I was looking at parts of the Lightmapping shader, but realized nothing changes if I get rid of it, forgot it take it out.
I’m guessing this wouldn’t allow me to use baked shadows, though. Could I bake lighting into vertex colors, allowing me to just make a bunch of lights rather than tweaking every vertex?
Btw, do you have any of artwork with painted lightmaps in? I’m kind of curious as to what kind of effect you could achieve with it…I’m a big fan of painted textures, but I’ve never really thougt of the idea of painting the lightmaps.
It can be done. I’ve never done it myself, and I don’t know if Blender, the tool I use, provides the means to do it. I’ve seen it used to great effect with ambient occlusion; here’s one example:
We overhauled our game recently and it’s not in a state to get screenshots from presently. :x You can see an earlier version of something we’re using, here:
It looks more like the broken plaster we were going for, in its current incarnation, but even here, where I don’t think it looks particularly “real”, you can see that a lot of nice and very fake depth is added.