In a new scene, I applied Brownian motion to some differently-colored spheres and then set each metaball’s position to the sphere’s position.
Tracking the size of the comparison sphere using Unity’s WorldToScreenPoint
method wasn’t working right away. In order to fix it, I simplified my data so that only one metaball was moving around the screen. It turned out that I forgot to add an alpha value to the sphere’s material color, so when the script was comparing the sampled color to the material color, it wasn’t getting a match.
At first, I thought the crisscrossing of multiple metaballs would throw off the scale tracking on one of the balls. Then, once I added more metaballs into my scene, I realized that only the first metaball was being properly tracked.
After frowning at my code unsure of what was going haywire, I added back the log statements from before and realized that once again, I was experiencing a color issue. For whatever reason, the render texture colors were not being read correctly. Any value not RGB value not 0 or 255 was being interpreted differently by the GetPixel
method. Fortunately, with a maximum of 6 metaballs in my scene, I was able to concoct 6 working colors that scaled correctly: red, green, blue, cyan, yellow, and violet.
Unfortunately, the issue went deeper than reading color values. In some trials, the scaling worked as intended, but in most others, at least one ball scaled infinitely from the start. I figured out the real issue when I changed the initial size of the scaling spheres to be significantly different from the initial size of the metaballs—my boolean value check was reversed! I was scaling the spheres when they were bigger than the metaballs, and shrinking them when they were tinier. After correcting that code, the scaler started working as intended. Metaballs sure are beautiful.
I’d been concerned about the overlap of metaballs throwing off the data (ex. when a sphere is behind another sphere relative to the camera), but after some testing, that doesn’t seem to pose much of an issue. The sphere’s adjust scale quickly enough to compensate for the overlap, and since they track the size of the metaballs so consistently, I may not even need to figure out a slick way to collect data. We shall see.