• Unity
  • Question about custom shaders in Unity

Hello.
I am currently trying to make a custom shader for a Spine animation (I'm currently using shader graph) but encountered a problem.
Let's say I want to cover my object with a white blob like this:

The issue is that I can only directly modify the original sprite atlas texture:

I am looking for a way to make a tranformation of texture from object space to sprite atlas space. First thing I tried was creating a custom UV texture for my blob texture so it would deform it according to the sprite atlas. But that was a failure because image quantization would distort my original texture. I am also thinking about generating a custom UV in script but both of these ideas are flawed in a way that they might not take into consideration the pose of the skeleton when the blob is applied to it as well as mesh deformations.
My another idea was (since my blob texture is procedurally generated anyway) to generate the texture to fit the sprite atlas right away.
What would be the best approach to implement this? Or is it even worth the hustle? That effect is a pretty minor thing in my game so I would like to avoid spending too much time on it. Might be a useful thing to know for the future, however.

Related Discussions
...
  • Изменено

I'm afraid I don't completely understand the requirements of your use case:
How and where do you want to place this blob? Should it be placed like a decal, with arbitrary position (like a bullet hole)? Or limited to certain pre-defined locations?
Should it be placed once and never moved, or shall it update every frame (e.g. a circle following the mouse cursor)?
Should it only affect some attachments, and not others?

Thanks for the response.
So these blobs would act similar to bullet holes. The only difference is that the turret would get hit from the sides and the blob would spawn at the point of impact. And there also can be multiple of them from different sides. Actually I wanted to use it as a mask for another texture which is just a wrecked version of the original one. I've attached a file where I attempted to illustrate what I mean.
I this case it shouldn't be moving (aside from moving along with the animations of course) but I'm also curious whether it is possible to make it move for the future cases.
Yes, there are some attachments, that shouldn't be affected by this (for example muzzle flashes) but it's not really that important in my case.

Thanks for the clarification. Unfortunately your task is not trivial. Bullet hole decals are easier in comparison, as they are typically just additional geometry overlaid onto existing static geometry by adding additional triangles on top of the hit surface.

Depending on how you would want to implement your solution, it would be equivalent to painting to the texture of a 3D model in Substance Painter. So the task would be to invert texture uv mapping, transforming the blob center position to texture space. Perhaps you can find something on the Unity Asset Store like this package (I didn't check whether this asset can be applied in your case).

There are other approaches for modifying blending of textures, often based on vertex colors (like e.g. this asset). However, in your case it is not a static mesh but a dynamic one, so this will not be a viable solution here.

An easier solution could be to use a vector based approach, storing e.g. a maximum of 10 impact center positions and the impact radius. In a custom pixel shader you can then evaluate whether the pixel lies within any of the disk areas.

Another solution would be to use the stencil buffer as a mask buffer. You could then attach your impact disk GameObject to the bone of the hit attachment, and then render the impact disk into the stencil buffer only. Then you can use your custom shader to render your object damaged where the stencil buffer is > 0.

The easiest solution would be to have predefined damage for e.g. 4 directions and enable them by switching the Attachment at the Slot, or overlay a damage Attachment.

In case anyone has other suggestions that I did not think of, I would be happy to hear them. :nerd:

Thanks for the answer!
I'll have to think whether I want to spend that much effort on this.