5
\$\begingroup\$

I want to simulate blobs of water in a 2d game in Unity3d. One possible way of doing it is to use 3d metaballs moving in a 2d plane though this is very processor intensive. Do you think it would be possible to make 2d metaballs, so I end up with a 2d meshes something like in the picture (In this case its 2 metaballs in conjunction) and then apply some kind of shader to make it look like they are bulging out and refracting the texture behind them like drops of water? I ask because I dont have much experience with shaders and am not sure what is possible.

please excuse terrible drawing!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Since the question is asking whether it is possible or not - I'll tell you. Yes it's possible to do it with shaders - and the droi - I mean shader you are looking for is a fragment shader. \$\endgroup\$
    – Grey
    Commented Jan 23, 2014 at 18:29
  • \$\begingroup\$ Furthermore I'd say you'll need Unity Pro, because you'll need to grab the framebuffer as a texture to apply some effect on it. \$\endgroup\$ Commented Jan 23, 2014 at 20:53

2 Answers 2

4
\$\begingroup\$

ilmale's answer is very comprehensive for 3D.

For the 2D case, a trick I've used in the past is to render a bunch of blobs with feathered edges using additive blending. (This example I had kicking around used rounded squares, but any shape works. The soft edges let them merge smoothly together when they get close.)

First render pass

This creates something like a height field. Then in a second pass you threshold this field to generate your blob contours. You can use finite differences to extract normals from the field for shading & refraction.

Second render pass

This gets the visual effect, but the exact countours live only in the raster data on the GPU side. If you need to know where the boundaries of the metaball surface are for gameplay, you may want to use an analytical approach instead (although the two techniques could also be combined, as long as the implementations agree on where the contour goes).

\$\endgroup\$
3
  • \$\begingroup\$ Hi! I tried this approach and it works well to get a "blobby" shape, but I can't get a smooth surface like your's when converting the generated height map to a normal map (I'm using a Sobel filter). Are you doing this conversion too, or have you another trick to get good looking lighting effect by only using the height map? Then could you share it please? \$\endgroup\$
    – Kikiwa
    Commented Apr 3, 2015 at 16:06
  • \$\begingroup\$ I don't have easy access to this particular sample right now, but I don't recall doing anything particularly special; I think I just used finite differences. If you post a question with screengrabs of your inputs & results, I or someone else might be able to help you diagnose the issue. \$\endgroup\$
    – DMGregory
    Commented Apr 3, 2015 at 17:47
  • \$\begingroup\$ Here's an example I made several years later for a weekend game jam, if you'd like to play with this and see how it can look/feel. The normals in this one are calculated by sampling 4 corners around the pixel with bilinear filtering and taking the differences up-down/left-right, turning that to a unit vector, then scaling it based on a curve processed from the height level. \$\endgroup\$
    – DMGregory
    Commented Mar 24, 2023 at 13:55
3
\$\begingroup\$

If you want to draw 2d drop of water a solution is to draw sphere normals and then blur the normal buffer. Then use the buffer to calculate the refraction.

This technique is briefly explained here (in 3d):

http://developer.download.nvidia.com/presentations/2010/gdc/Direct3D_Effects.pdf

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .