4
\$\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
    \$\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 Jan 23 '14 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\$ – Pedro Boechat Jan 23 '14 at 20:53
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\$
  • \$\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 Apr 3 '15 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 Apr 3 '15 at 17:47
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.