
At the conclusion of last month’s DevBlog, I mentioned I’d be spending a couple of weeks house-sitting, and that I wasn’t sure what sort of work I’d be able to get done during that time but that I hoped to make some progress on the animations for the player character. To put things bluntly, this did not turn out as planned: I was there for a day before the wind-storm that precipitated the calamitous Oregon fire season sprung up, and in the midst of that the power went out and a fire started in the neighboring golf course. I wasn’t sure how big a deal this was, but I didn’t want to be the smoldering skeleton who had regarded it as an insufficiently big deal and the sheriff’s deputy outside was suggesting that folks might want to leave just so that if things did suddenly take a turn for the disastrous the firefighters would have less to worry about.
So I left. It turned out that that particular fire was not, in fact, a big deal, and it was swiftly gotten under control, but by this time there were other fires near where I lived (as well as nearly everywhere else in the state) and it was uncertain how far they would spread. I was a little uncomfortable with the idea both of leaving my entire life behind to the caprices of fire and fate and the idea of spending a lot of time on the road, so I ended up only getting back to the house I was to sit once to let some other people in, friends of the owners, who themselves had had to evacuate from their own home because of the fires.
Even though I was back home with my regular setup, no work was getting done during this time, on account of the apocalyptic toxic smoke just hanging about outside. A sort of impromptu vacation, enforced by act of god. It still helped, a little, and I got some rest, which I imagine is why when I started actually working again I made so much progress so quickly.
Last month I talked about issues with getting the grass effect working, most notably the huge performance cost of manually coloring and rotating each particle representing a clump of grass. I experimented briefly with some ideas of making this process more efficient, but the end conclusion I came to was that, short of creating my own completely new particle system component, there was no real way to fix these issues. There was, however, a solution completely separate from particle systems: I could instead create a shader to warp the grass sprite to create a visual effect of it flowing in wind, and control both that warping effect and the color change effect I created last month using the same texture I was currently using to modify the grass particles.
Here’s what the old effect look like, manually updating the position of each particle:
And here’s what it looked like when I just displaced and recolored pixels for the underlying terrain in the shader with no particles:
This actually looked better than I’d expected it to, but it definitely looked like a computer effect rather than something natural. It really needed the grass clump particles to add variety, irregularity, and a bit of texture and noise. I could just use the same pixel displacement technique to render the grass particles, but what I really wanted was for each particle, each clump of grass, to be blown by the wind – to rotate as a single piece rather than just to have their individual pixels pushed around. I tried to create another effect, where the shader could rotate the texture around its pivot, but quickly ran into issues trying to use that along with the grass sprite sheet – because all of the images were stored as one large texture in memory, displacing them significantly would cause pixels from adjacent images to leak into the edges of the other. This is something I could easily fix if Unity provided any way to get information about where these boundaries lie in shaders – but it doesn’t. Without that, I’d have to manually add a script to any sprite that is stored this way in memory in order to get it to render correctly. I did not want to do this, for various reasons.
However, there’s more than one way to rotate a texture. Rather than rotating the UV coordinates that the texture used for drawing, I could just rotate the vertices of the quad that was being rendered. Now, in addition to an effect that displaces pixels, I have added an effect that displaces vertices – which is relatively narrow in its applications, but great for, say, rotating a bunch of particles in a performant fashion. Putting it all together, we get the finished grass effect:
The other big issue I ran into creating these effects is that, though things like transformation and color changes make the most sense when performed using a matrix, as these sorts of transformations are what matrices are all about, matrices are actually not very easy to use inside Unity shaders. The reason is, as it turns out, that Unity simply doesn’t bother to serialize or save any information regarding matrices in the materials of objects, so they always reset to their default values whenever the game updates. This seems like an oversight to me, but regardless of the reasons it makes matrix values a nightmare to work with. In the end, I bypassed this by just saving the matrices as a set of 4 vectors and then combining them together later to perform the necessary operations.
Most of this was basically over the course of one incredibly intense week, with a second slightly less intense week to fix bugs and create nice interfaces for all these new features. By the end of this I’d started to get extremely tired, and decided that I really needed a little break from programming, and that this would be a good time to get to that animation work I’d been wanting to do before. I also decided this would be a good week to try quitting caffeine to see how it affected my sleep patterns. These two goals were not entirely compatible. I got some animation work done, but it was mostly just an hour or two here and there – nothing even really ready to show here. I’ve still got a headache from the lack of caffeine, and I have no idea how to start my day absent the ritual of a nice coffee or tea, but it’s in the name of experimentation I suppose. If I can fix my sleep issues without making it impossible to wake up in the morning that would be nice, but so far the sleep issues persist and waking up is much more difficult. Oh well.
What adventures await next month? I have two goals for October: First, to finally do that animation work. I’ve at least started to get into the swing of it, and I have a sense for how long these will take – I think I have a pretty good shot at finishing all of the basic motion animations for the character in two weeks, or at least getting close. Second, to get something onto the Unity Asset Store: I’m not sure what. I have numerous tools that I’ve been developing for this project, and I think a number of them would be useful to other developers – this grass effect is made using the advanced sprite shader I’ve been working on for much of the last year and a texture mixer which I think could be expanded into something powerful, and looking over what I’ve already done I have the 2d conditional animator which is close to complete and a couple of pretty interesting post-processing effects which could be expanded and upgraded. In order to feel comfortable putting something up on the asset store, though, I’ll need to push these things to a higher degree of stability and user-friendliness than I usually undertake for tools for my own use, so this could end up being quite labor-intensive. I’ll most likely start with the sprite shader I’ve been working on, since I’ve spent so much time with it – but, between aforementioned polish issues and a couple of extra features it will need to be competitive, this is probably something that will take at least a solid week to complete.
