Best way to fill a container with liquid with varying fullness?

Currently my liquid is part of the same mesh as the bottle with a separate material but I could change that. I’ve tried using opacity mask in the material but I don’t know if there’s a way to generate a flat surface on top of what’s left, so it ends up like this for a sphere:

251324-capture.png

Slicing/restoring a procedural mesh works in theory but it’s harder to do than changing a material, and this has a big impact on my FPS:

I guess I may not need physics since my liquid should just be following the container and taking that off does seem to help, but I’d actually need to do both of those things every tick the height changes and not flip-flop so it could still be bad.

Since most containers will be able to move and rotate, it would be nice if the liquid could move around semi-realistically and keep the same volume but that’s probably asking too much…

if you make the material two sided and unlit it will look as if it has a surface on the top.

To get the physical sloshing motion you can make a bp with your cup and then add an static mesh that is connected via a physics constraint. You can mess with the physics constraint settings to get a nice swinging motion. Position that mesh at the base of the cup and set it to be hidden in game.

Then go back into you material and add a spheremask. It will need to be a kinda large. the location will be above the cup. specifically it needs to be the hidden mesh’s location offset on the z axis.

So what you are doing is moving the cup which moves the hidden mesh which moves the sphere mask.

if you decide you don’t need physics then here are a few links with liquid shader examples you can just copy.

https://www.artstation.com/artwork/byxPE

https://simonschreibt.de/gat/left-4-dead-2-puke/

if you make the material two sided and unlit it will look as if it has a surface on the top.

That works for flat colours but I want at least some of my potions to be a bit more interesting.

251338-capture.png

So I’ll probably use the mesh slicing method for those since they’ll only need to change their fullness when you drink a dose.

To get the physical sloshing motion you can make a bp with your cup and then add an static mesh that is connected via a physics constraint. You can mess with the physics constraint settings to get a nice swinging motion. Position that mesh at the base of the cup and set it to be hidden in game.

Then go back into you material and add a spheremask. It will need to be a kinda large. the location will be above the cup. specifically it needs to be the hidden mesh’s location offset on the z axis.

Will this work if the invisible mesh is outside of the container? Mine keeps clipping through even though both meshes have CCD enabled… anyway I’ll look into this more tomorrow for things that aren’t potions, where a solid colour maybe enhanced by particle effects would be good enough.

you can place the invisible mesh anywhere makes the math for the fill height easier. Placing it in the center may make more sense if you have a sealed container.
you can just probably just set the invisible mesh to overlap all.

Yeah I got it basically working by disabling collision in the physicsconstraint. Looks like it won’t be easy to make it work when the container is on its side or upside down though. I’ll probably just use it for things that have limited rotation or remove the sphere mask when the x/y rotation approaches 90 degrees.

I never got around to finishing it but for my project I was just doing an open cup so I could just lower the fill level so by the time it tipped to 90 the cup would be empty. I didn’t test a sealed container, but I think if the model height and width are roughly the same it would work upside down… idk just picturing it in my mind eye.

there is a marketplace asset somebody added called liquid x. I haven’t bought it, but I’m assuming he’s using basically the same method we’ve been talking about. his containers seem to work fine upside down.

Oh I saw that before. I might even buy it but now that you reminded me I decided to incorporate the foam into my material to fake a surface for textured liquids. It was looking like this:

251493-capture.png

and I was going to ask why but then I guessed the liquid mesh might be clipping with the glass mesh and scaled it down a bit. Now it works better so I’ll just leave this info here for whoever may find it…