NokiMo
FuroticVR
FuroticVR

patreon


Fluid Simulation Tests

Been working these last couple weeks on fluid simulation, and man is it a pain in the ass!

Some VR games use a fluid simulation plugin from the Unity store which looks great in videos but falls flat in actual usage becaue it fakes geometry, rendering it to a canvas which then has to be overlaid on the scene. This looks weird in VR because it's secretly 2D and lacks depth, and because it murders your framerate the devs who use it tend to render the canvas at a lower resolution than your display to try to improve the performance, but that then makes the fluid look blurry, and the visuals even worse.

So I decided to take a different approach and use a plugin which generates actual geometry on the GPU... But this plugin has some issues of its own, because its bypassing Unity's usual lighting setup, which means out of the box it doesn't support light probes, occlusion probes, or reflection probes.

In addition, transparent objects in Unity normally can't receive shadows, only cast them, which means transparent fluid would still be lit by the sun when inside the house!

Of course, I could work around this by disabling the sun for transparent objects that are indoors, but that wouldn't solve giant avatars casting shadows on it, and I still wouldn't have light probes, which would mean no baked ambient light. And with no ambient light AND no reflection probes, the fluid indoors would simply look flat, while the fluid outdoors would be extremely dark on the side facing away from the sun. Not ideal!

Now with all these limitations, it would seem impossible to make this work well, and look good in all conditions, and I might have to live with opaque fluid, or use a bunch of hacks, or maybe give up on realtime fluid calculations altogether... But I'm not one to admit defeat so easily!

Figuring there must be a way to make it work, as least most of the way, I dug deep into Unity's undocumented code base and pored over the sparse documentation for two weeks.

The first thing I figured out was how to tell the renderer which light probes were nearby! But light probes only got me ambient light. I still had the issue of no reflection probes, and the sun lighting my transparent fluid indoors.

I tried to use the same method as I used for the light probes to tell the renderer to use the reflection probes, but no no avail. Thankfully I had an idea for a workaround, where I would add a cube map to the shader, and then pass the cube map of the nearest reflection probe to the shader, and in the above screenshots you can see a quickly hacked together version of that trick in action, though there's still some work left to do there on the scriptng side of things.

Occlusion probes I couldn't seem to get to function, or if they were, I couldn't tell, but they just shadow the surface from ambient light and that's a minor visual detail I can do without.

So the final piece of the puzzle was getting shadows to work.

Shadows on transparent objects, it turns out, aren't impossible, they're just imperfect and not easy to enable in Unity. They're imperfect in the sense that if you have an object in shadow behind a transparent object receiving a shadow, you'll only see the shadow on the transparent surface and the one behind won't have a shadow. This is hardly noticable though, even with the giant fluid particles in my tests.

To get shadows working, which you can see from the above shots they are because the fluid indoors is not lit by the sun, I tried dozens of different combinations of render type, render queue, alpha blending, and z-testing until I found one that almost worked... but the shadows of objects behind the fluid were either rendering on top of it, or shadowed regions would be dithered, which looked terrible. With a little help from the creators of Amplify shader editor though, I was able to squash that last bug, and I finally had a working implementation that looked almost exactly how I wanted!

But it's still not quite there. I still need to finish up the code to pass the nearest reflection probe to the shader, and I'm trying to implement light scattering, though I don't know if I can make that work, and it may only be useful outdoors in direct sunlight.

I may also try to add some fresnel rim lighting, and I'm currently trying to implement a two-pass approach to allow for depth fade, which would allow me to render the back faces and make the fluid appear thicker in the center and thinner at the edges!

Anyway, that's all for now! I'm still planning to release a build later this month. I'd planned to release one this week, but I got caught up in making this fluid sim stuff work right! It's kinda important and I wanted to include it in the build in some capacity to see how it performs for everyone!

Fluid Simulation Tests Fluid Simulation Tests

Comments

Your dedication and attention to detail is astounding!

Dinosorceror


Related Creators