Search

And Another Thing: Modeling and Materials (Part III)

Updated: Aug 22, 2019

I'm still collecting my thoughts about menu UI and animations (topics which have occupied so much of my time recently that I've been slacking on posting here), so in the meantime I have a few follow-up remarks on materials. A few days ago, I created a barn model using the usual methods and without much trouble, but when it came time to apply materials, it took several tries for me to get it right.


You're looking at my model in blender. Notice the Materials drawer on the bottom right, and how those three materials have been applied to faces of the model. I would almost certainly get scolded by my modeling and animation professors for doing it this way (creating extra faces in the model to define all the frames rather than relying on UVs and 2D textures to accomplish the same thing), but this is much easier and should have been enough for my purposes. When you export the model, this material information goes with it. Note that the materials themselves don't (I'm sure there's a way...), but that's okay, because I'm just using the blender materials as placeholders anyway. When my barn comes into Unity, it sees that there are three material slots that can be filled, and I can fill them with any material that I want to. These new materials apply themselves to the model exactly the way the placeholders did in blender, so in this case whatever I fill the first slot with defines the main color of the barn, the second the frame, and the third the window.


This is pretty simple, and it works well if you don't need to do anything more complicated. Unfortunately, it fails if you need the ability to layer materials, as I described briefly at the end of my Part II blog. I need both a texture design and a fill color for the window, and I wanted to put my thick, cartoony outline shader on everything. If a model you bring in only has one material slot, Unity assumes that it's going to apply that material to the entire object, which is usually true. So manually adding more just material slots causes Unity to apply those to the entire thing as well. No problem. But if you bring in a model that already has multiple slots, each keyed to specific faces like in my barn model, adding more slots afterwards just confuses Unity, and the extra materials likely won't show up at all.


I realized that I had already solved this problem without meaning to on my UFO. The windscreen, main body, nose, and ring are all separate geometry, so even in the imported model, each could be accessed separately and each individual piece only had one material slot. Without remodeling the entire barn in this way, the easiest thing to do was duplicate it several times and then delete all the faces that weren't part of the material group I was looking at currently. Let me show you what I mean:

Of course, these components still occupy the exact same physical space, so turning them all on at once gives back the same final image as above, just with each set of faces able to be selected and edited as a separate group. Bringing the new model into Unity gave me the one-slot-on-each setup, and adding more myself at that point allowed me to layer materials how I wanted.


...Okay, that's a bit of an oversimplification. The next problem was one of the render queue order. All my main materials use sprite shaders, because I want the colors to remain flat and not affected by lighting. That means they need to go in a specific place in the queue, and then things like the outline and other textures need to be placed correctly relative. In this model, I wanted to outline not only the silhouette edge, but also every place the color switched, so around the window and the rest of the trim as well. But after hours of experimentation, I discovered that this was literally impossible, as that effect would have required MatA to be rendered before MatB, which had to be rendered before MatC, which had to be rendered before MatA. Is it possible that UVing could have helped here, if I had taken the time to carefully unwrap, take the texture into Photoshop, and carefully trace over all the inside edges with black lines myself? Maybe. Would it be worth that effort? No it would not. The final result looks like this:


The most important part of all of this is that the materials applied to the barn are the same as the ones on the UFO. And I don't mean just the same colors, they are the same materials. Why is this important? Well, I'll be telling you more about the Player Select and other menus next time, but for now, one of the options there is to change your UFO's color. When I designed the back-end code to do this, I realized that the easiest way was to edit the properties of the material file itself, as opposed to swapping it for a different material file as I might have done in the past. This way, I don't have to worry about storing a "current material" for each player, and then iterating through and applying it to everything else linked to them (the barn, the drone, the lure, the force field, and anything else I might have in the future), and doing this for up to four players at the start of each match. Instead, by changing the material file itself as the player picks their color, everything that has that material applied to it automatically receives the changes. And, because I've layered my materials, I'm able to literally change one of them, P1_base (or whatever player number), and all the outlining, decal textures, and other effects stay the same as only the main color changes. This is incredibly powerful and works perfectly...as long as I remember to keep splitting up the models into one-material groups when I'm working in blender.


Next time: Menus and/or animations