Making a Game in Unity 015: Water With Shadows

Just so you know, this is not a tutorial series! I am writing this series mainly to give you a behind the scenes look at the process of making a game in Unity, and to document the development of this project.

See other posts in this series.


Making a game in Unity 015

It’s been quite a while since I posted one of these updates because I took a rather lengthy break from programming. I’ll explain my reasoning later on in this post. As for actual progress, I finally managed to render shadows on transparent objects, which isn’t nearly as easy as it should be in Unity. That’s something that I’d been stuck on for months, and it feels so good to have figured it out!

Monday (July 1st)

This morning, I played around with the terrain shader some more. It now now renders triangles whose slopes are steep as a stony gray color.

Tuesday

I spent most of today completing various tasks that needed doing, including making a few website modifications. I’m also going to start building a website for my dad this week, so it’s likely that I won’t make as much progress as I did last week.

Wednesday

Today I made players float when they’re in the water. I still have to modify the values quite a bit, since I want the player to be capable of sinking, just at a reduced speed. Currently, the water’s surface is basically a trampoline—going below it is virtually impossible.

Ships also pose a problem to player flotation. Since the water really just consists of an equation, flotational forces are applied based on the player’s distance from the surface. The issue is that this occurs universally, even inside the ship’s hull, where the water technically “exists”. Players floating inside ships is obviously not ideal, but at this point I’m relatively clueless as to how I’ll go about solving it.

Thursday (August 22)

After I came back from a week-long camp in July, I decided to take a break from programming. Since I’m finished with high school now, I need to begin figuring out how I’m going to survive on my own. I didn’t feel like I could fully focus on developing this game without having some sort of idea of how I’ll proceed in the future, so I decided to take a break to gain some clarity. It ended up being a rather long one.

From now on, I’ll try to do some development regularly, but I also intend to start learning React.js (among a few other things) which will likely result in slower progress on this project.

Anyways, last night I came across a thread containing some speculation about possible solutions to the problem of having shadows on transparent objects—and more specifically—on water. While it took virtually all day, I managed to get the shadows on a rudimentary transparent test object, and then, finally, on my water. The problem is that in order to get the shadows, the water object must write to the depth buffer. Since my foam and shoreline effects rely on the water not being written to said buffer, those no longer work.

Friday

Today I put together a workaround to re-achieve the water effects which rely on the depth buffer. Essentially, instead of relying on the default depth texture, I render a custom one that doesn’t include the water layer and then use that. While it actually sounds quite simple, it’s an annoyingly involved process to setup, and it’s probably super overkill. Unfortunately, it’s the only way of achieving shadows on transparent objects while simultaneously maintaining depth capabilities (that I’m aware of).

Saturday

This morning I started working on an issue I noticed yesterday—the depth effects don’t work on objects that use my regular low poly shader. After some experimentation and looking at the shaders that were properly writing to my custom depth texture, I noticed that unlike them, my shaders didn’t have the render type set in the SubShader (I do it programmatically). After I added that in, the problem was solved.

I spent the rest of the day working out a few more kinks in the system and doing some much-needed project cleanup.

Shadows on transparent objects, specifically the water
I’m super happy that I finally got shadows working on my water—it looks so much better than before.

Sunday

Today I cleaned up and reorganized the water shader to make it more readable and easier to maintain. I also improved the efficiency by removing some if statements (shaders don’t handle those well).

What’s next?

Since I’m going to start learning React.js, I’ll be spending less time developing this game. I don’t think I’ll completely stop the way I did over the last few weeks, but progress will likely be slow. I’ll only post these updates when there’s been a decent amount of progress, so they’ll be a bit irregular.

As for what specifically I’ll be working on, for the time being it’ll largely be client side. Something isn’t right with my computer’s internet connection (pretty sure it’s the wireless adapter), which is making it a real pain to test anything server-related. I need to make a few more improvements to the water shader, and then I might start working on some underwater effects.

See other posts in this series.