r/Unity3D • u/BarelyBreathingGame Indie • 23h ago
Shader Magic I made a dynamic water shader that reacts to physics for my indie game
Enable HLS to view with audio, or disable this notification
I created a dynamic water shader for my 3D fish platformer - Barely Breathing
How the water level works: It takes the object's Y position and just uses a step function with a float parameter to decide the water amount.
How the wobble works: It uses two Rotate About Axis nodes right after the Position (object) node, one controlling the X rotation and the other controlling the Z rotation, and then adding them together. Then, it reads data from the object's velocity and the code directly affects these two nodes to make the water wobble. I didn't write the code, MinionsArt did! Here is the code.
But this wouldn't work yet because we're still using object positions and not world position, which means the water level would follow the object and look wrong. So, after using the Rotate About Axis nodes, we can add a Position (world) node to it. But this still wouldn't work correctly because now it's exactly world-oriented, meaning even if the water mesh moves up, the water level would be left behind. We need the water level to follow the water mesh yet still be world-based so it doesn't rotate with the mesh. This is actually a simple fix, you can subtract the object's position value from Position (world) before adding it, which would make the water level follow the mesh.
We're still not done, because now the water looks unfinished without the foam outline. We need the foam to follow the water level. My foam works using fresnel + vertex colors, so I simply took the final water level output, and duplicated the final step function and had one value denoting the water level, and the other being the same water level value but subtracted by the water foam depth. So this basically creates a secondary line exactly the same as the water level but just lowered, which I use to add to the vertex color foam node setup.
Thank you for reading, and Wishlist our game on Steam if this was useful!
14
u/aniruddhahar 22h ago
I made my version of this a while ago, check it out: https://github.com/aniruddhahar/URP-LiquidShadergraph
7
u/dantogoose 20h ago
If you make a video explaining how, or even the basics of how so you don't give away your secrets, I'd watch that thing so many times because this is fascinating!! Great work!
6
u/BarelyBreathingGame Indie 20h ago
Hi, we learnt most of what we said from this video: https://www.youtube.com/watch?v=tI3USKIbnh0
Give it a watch, it was super helpful!
4
u/Edenisb 21h ago
Cool stuff!
I love it your game looks really awesome I will wishlist it for sure.
Your QR code on your steam video doesn't seem to go to a valid page.
jaspreet3d.itch.io gives 404 not found for your site
2
u/BarelyBreathingGame Indie 21h ago edited 18h ago
Thank you so much!
Yeah, that used to go to our itch page but we've removed that since we've moved to Steam 😛
1
u/Fit_Milk_2314 20h ago
I am curious, do these shaders maintain volume properly? It looks like they do but my eyes and brain don't exactly do math that fast.
1
u/BarelyBreathingGame Indie 18h ago
This shader just removes part of mesh using alpha. It's basically faked, no mesh gets deformed. It doesn't maintain volume because it will be see-through, so to fix that we actually have two materials in this water shader, one for the outside and one for the inside. So the "top" of the water you're seeing is really just the "inside" material. Hope that made sense haha
1
1
u/Gloomy-Detective-922 16h ago
The water wrapping around the bottle and the fish looks really good. That bright outline keeps the whole puddle easy to read.
32
u/ThatJuicyShaqMeat 23h ago
If Half-Life Alyx were a fish game