Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing liquid with LibGDX

I'm quite new to LibGDX and OpenGL, but I managed to make a simple liquid simulation using Box2D API. See this link (this is someone's else animation):

Physics Liquid

Currently I render the liquid particles as circles just like in the first image, but I want to make it look more natural like on the third one.

The answer might be to use distance field and I tried this approach, but with no effect. I'm drawing each particle as a texture using SpriteBatch class, but that can be changed. I made a texture (from a procedural Pixmap) that represents each particle as a filled circle, with alpha channel decreasing further from the center, so the effect is similar to the second picture.

Now, I must enable a threshold filter to alpha channel, something like: "draw only pixels with alpha > 0.5". This is post-processing step, because it matters what is the alpha channel of a pixel after all particles have been draw. Might or might not be done with shaders (ProgramShader), but after some research I still have no clue how to do this. Thanks for ANY help.

EDIT: this example explains the method, but it's implemented in ActionScript.

like image 482
vic Avatar asked Nov 10 '22 15:11

vic


1 Answers

This can easily be done using shaders, but funny thing is that you don't need to write them.

"draw only pixels with alpha > 0.5" is also used while rendering distance field fonts (fonts which look good even when scaled up).

  • Follow this link : https://github.com/libgdx/libgdx/wiki/Distance-field-fonts
  • Directly jump to the last step.
  • You should find exactly what you want.

Hope this helps.

like image 139
Tanmay Patil Avatar answered Nov 15 '22 12:11

Tanmay Patil