We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
Among the various coding challenges I set for myself there's one at the top of my list that still remains obscure and rather complex to me: coding a 3d metaball.
Since I first saw Zack Lieberman's work on 3d blobs and metaballs I've been obssessed with them.
made by Zack Lieberman with OpenFrameworks
made by Satoshi Horii (Rhizomatics) probably with OpenFrameworks
made by Mike Walczyk with OpenFrameworks and rendered in TouchDesigner (? not sure)
made by Martin Meurin with Processing
I've read the wikipedia page, I know from other sources that it may involve Ray Marching shaders and Signed distance Functions but the problem is I haven't been able to find a source code, even a snippet, to work on. I'm relatively new to Processing and starting from scratch for this challenge seems really tough.
I would love to read your suggestions and advices. If you have any link to tutorials, step-by-step roadmaps or even code in Java, Python or even C++ I would really appreciate if you could share it here.
Thank you !
Answers
Did you read the 2D metaball? It’s essentially a heat map of summing up distances to the balls.
if you think this in 3D you would have a 3D heat map. That means triple nested for loop over a section of space and adding up colors following distance to balls.
Of course very slow... But okay to make a movie
Of course I've read it but it seems that coding a 3d metaball is slightly more complicated than just adding another dimension to the 2d version.
The solution you suggest is computationnally very expensive and doesn't take into account the tracing of the metaball edges.
Quoting Zack Lieberman:
Quoting Mike Walczyk:
More on his blog post
This could be relevant: https://forum.processing.org/two/discussion/comment/119555/#Comment_119555
Kf
It looks like Walczyk's blog post and sample code is about implementing this particular approach inside a GLSL shader.
Have you looked at the PShader tutorial for Processing?
@kfrajer @jeremydouglass sorry for not replying I'm doing some research. I'll comment back as soon as I'm finished with this metaball thing. (Thank you both for the links. It seems the ray marching technique is more widespread than I thought. Not sure yet if I'll implement it in a shader)
Update: I've managed to make a bloby metaball (sort of fusion of several metaballs) using the toxclibs library.
But there's still one major thing I can't figure out:
On a similar issue with the toxiclibs library, @amnon replied:
Following this answer I tried to draw each face individually:
It becomes then possible to draw different colors depending on the face's index, however it still doesn't tell me how to give each ball a different color
Do you guys have any idea how to solve this problem ?
Full code:
What bugs me is that although I'm drawing multiple objects (from line 28) toxiclibs won't allow to assign different colors.
For example, if I store different colors in a list:
And then specify within the
for
loop (from line 28) something like:fill(colors[i][0],colors[i][1],colors[i][2])
... I still end up with 10 balls filled with the same color.
Are you referring to lines 40-48?
fill()
doesn't affect vertex-vertex in TRIANGLES. It only gets used once atendShape()
-- so you can change it as many times as you want in the loop while specifying points; the state of the color won't matter until the final value. To have fill take effect in that way you would need to callfill(); triangle();
on each iteration of the loop, not a bunch of vertex points in a beginShape() / endShape.@jeremydouglass I was referring to line 28:
for i, p in enumerate(points): force = a.attract(p) p.applyForce(force) p.update() p.display()
points
here refer to the balls. So I was thinking that for each ball I could assign a specific color:for i, p in enumerate(points): force = a.attract(p) p.applyForce(force) p.update() p.display() fill(colors[i][0],colors[i][1],colors[i][2])
You're suggesting to call
triangle()
but this function only takes 2d coordinates, not 3d (here f.a.x(), f.a.y() and f.a.z()).Re: triangle() ... "this function only takes 2d coordinates"
Fair enough: for 3D you could also use a shape TRIANGLES that is only a single vertex triple, or use a polygon. So "draw each face individually with it's connected color" would involve a separate beingShape / endShape inside the loop for each vertex triple.
For line 28, does
p.display()
-- that is, a call toArrayIsoSurface.drawAtAbsolutePos()
-- care about fill() as a parameter? Or is it just generating mesh geometry, not rendering? I'm not familiar with toxiclibs, but if after breaking out your loop into separate PShapes you are still getting one color, then color-dependent rendering might actually be happening in 35-38.Possibly relevant:
http://www.codeplastic.com/2018/02/24/painting-noise-with-toxiclibs-volumetric-brush-in-processing/
https://forum.processing.org/one/topic/toxiclibs-27-3-2012.html
https://forum.processing.org/one/topic/toxiclibs-trianglemesh-problem.html
That's exactly what I thought so I included these lines in the loop, trying each and every configuration possible. The following snippet seems to draw different colors:
But I get strange glitch-like results (not to mention the sketch running super slow). From the outside the metaballs seem to be covered with one single color (some triangle faces are visible):
From the inside, you can clearly see that each ball has in fact its own color. However they do not "melt" organically:
And by "organically" I mean something like this:
I'm really lost here. How such a simple thing can be made that difficult in toxiclibs.
@kfrajer @jeremydouglass Since my script is heavily depending on the tocixlibs library, would you guys mind if I post a question related to this metaball in the Library Questions" category ? I was hoping that maybe amnon or toxmeister would help even though they both don't seem to be very active on this forum anymore.
That makes sense. Perhaps link back to this thread in your new question -- or just move this thread into Library questions, having already tagged @amnon or @toxmeister, and see if continuing this thread there helps resolve the issue.
Thanks Jeremy. I prefer to open a new thread to be as clear as possible.