- Home /
illusion of liquid in bottle with cull and clip shader?
I am searching now a little while for a solution to create a liquid in a bottle that behaves like real liquid but dont uses realtime fluid simulation. Because that would not work good and is really cpu and gpu heavy!
Today I had a really great idea. I found once on the internet a project from someone that showed a box that is moving left to right and everything that is inside this box is displayed and everything that is outside this box is clipped.
I found the project on my pc again but there are 2 big problems. 1. the shader is outdated and unity has excluded it from OpenGL Es 2.0 and DX11 and my Project uses DX11. 2. this method uses a texture to clip stuff and that texture is projected via script over the world from the clipping objects local position and rotation. (I tryed it and it works for the liquid part but because it is projected over the world I will never be able to create a top face for the liquid.)
I found also another try from someone who made a shader that does nothing and this shader works like a mask. but it clips the whole object if it is between the camera and the bottle.
sooo. what I need is a way to clip everything from a specific object that is outside of a clippingobject. with that I could work. Is there a way to do this?
EDIT: I draw a little picture to better describe how I will setup this Idea. btw to clip the liquid Surface I would use a copy of the liquid model. link text
I made this really quick in blender to show how it would look if the bottle would be rotated. the blue face is the liquid surface. the transparent blue plane is that whats been clipped away by the liquid model. the gray liquid is inside of this box so it is visible. the upper half of the liquid is outside of the box so it is clipped. I would write a script to the clipping box and the liquid surface so that it will not rotate with the bottle but will move with the bottle.
Answer by MarkD · Nov 06, 2013 at 11:25 PM
Ok well, mostly for my liquids I simply create a shape that fits inside the bottle and just animate it. Throw one of the refraction shaders standard in unity on it and the animation of makes the refraction move. Also why go over the complete trouble of a clipping shader when you can create a simple round plane that fits in the bottle? (in modeling software) the size can be animated / controlled by scripts, so if the bottle is full the circle plane is smaller when the bottle is more empty the circle plane scales up to fit the bottle. Like I said before this would work better with a complete model fitting inside the bottle as you can give it color / effects with shaders.
I hope this was of any sort of help.
Yes that would be ok for static objects that never move.
I did forget to write that I want the bottle to be a rigid body and the liquid should move like real fluid if the player or a object moves the bottle or tilt it or throw it around.
I update my question a bit with more pictures.
Ok, that picture realy helped putting it in perspective.
Well for that effect you could use the stencil buffer, There are several people creating portal effects with it, but you should be able to inverse that effect. So ins$$anonymous$$d of cutting out a hole in a wall you could cut out all the stuff around it and keep the center in the shape of the object, you could use the vertex position of your model to deter$$anonymous$$e how far each part needs to be cut off.
I can't give you a code because i'm not that great with shader coding, at least not with stencil buffers, but that should be a good way to start investigating.
hmmmm.... I tryed it with a shader that uses only the z-buffer to cut away the top part of the bottle... but this was not good because when I looked at the bottle from above it cutted all liquid away... interesting idea but I really have no idea where to start... I will search a bit more for stencil buffers.
I really think my idea could be a very cool way to render a liquid effect for every game. If I find a working solution I hope I can get it into the asset store.
if you find anything useful for me please let me know. ^^
If I bump into anything useful i will surely let you know :) And indeed it is a cool idea, which also could be very performance friendly since no real time vertex displacement occurs. Combine that with a refract and detract shader and you have a pretty good water emulation going on, which would be definitely worth something on the asset store :)
Answer by Harry64 · Nov 08, 2013 at 09:29 PM
I think I can forget to try this with shaders. But I think I have a solution for this but to do the liquid in the bottle thing I would need to write a script that makes a boolean intersection operation in realtime.
I found on the internet only 2 people that created a realtime boolean script but the first ones link to the asset store is dead. and the second one made a big pack with lots of stuff that I never need and it costs 140$ or something.
sofar I did not found any hint on the internet how to code this...