I’m experimenting with an approach to discoloring meshes that are nearby explosions. The general idea is, if an explosion occurs, it should discolor meshes within a sphere of the explosion, ideally with more intense darkening the closer to the explosion the surface is.
I was trying out an approach where I generate a texture and write to its pixels, which correspond to UV2 coordinates on the mesh. It’s pretty straightforward to “paint” individual points on a mesh via the texcoords2 of a Raycast, but I’m struggling to find a performant approach to get all coords that fall within the sphere of the explosion.
I’ve thought of reversing the order of the search, where I test whether any given pixel in the generates texture corresponds to a world position within the sphere. But iterating over every pixel of a texture and testing the distance feels like it’s not very efficient.
Just curious if anyone knows of some simpler approach to getting the UV2 coords of all geometry within a sphere. Or, given a raycast hit’s texture coords, some approach to “growing” the coordinate to include more of the mesh directly around the coordinates?
Not sure this approach would be performant in general. Flipping individual pixels in Unity is just something it was never really meant to do. Really the only way to do it is to keep the number of pixels really small.
Have you considered perhaps doing vertex coloring? Some of your geometry might then need to be subdivided so that might not be practical, but it might be an easy performant way to grab nearby verts and darken them. Eh, thinking more after writing that, it might not really look that good actually. Hm.
Hmm. Vertex coloring is worth pursuing. A lot of the geometry I’ll be using this on tends to be biggish ProBuilder rooms, like this:
I suspect that the verts aren’t dense enough to really look good. However, it may actually be fine. The whole idea here is to create a mask to determine where the dirt should be placed, and I suspect that mask can be fairly low resolution and still maybe look decent. I’ll look into it.
Actually with vert coloring it would not work on that entire wall… only near the corners. The colors are only applied at vertices (hence vert coloring). I take back what I suggested above, unless you subdivide all your walls into lots of little grid (do not recommend!!), and even then it might look wiggy depending on the triangulation.