madbaek

Hello. I'm a solo dev working on a 2d side scroller game.
Recently, I made a character that uses the clipping function to hide the body beneath the ground, and the animation turned out to be great.
However, the hit-flickering code I made doesn't apply to the material parameter during the animation that uses a clipping attachment.
What I'm trying to say is, well, here is an example.

here's a code I made for the hit-flickering.
public class HitFlickering : MonoBehaviour
{

public MeshRenderer meshRenderer;


public string colorProperty = "_OverlayColor";
public string brightnessProperty = "_Brightness";

private bool flashing = false;

MaterialPropertyBlock block;


// Start is called before the first frame update
IEnumerator flashCoroutine()
{
yield return new WaitForSeconds(0.1f);
meshRenderer.material.SetColor(colorProperty, new Color(0, 0, 0, 0));
meshRenderer.material.SetFloat(brightnessProperty, 1);

flashing = false;

yield break;
}

public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.layer == 20 || other.gameObject.layer == 10)
{
flashing = true;
meshRenderer.material.SetColor(colorProperty, new Color(1, 1, 1, 0.7f));
meshRenderer.material.SetFloat(brightnessProperty, 2);

StartCoroutine(flashCoroutine());
}
}
}
And here is a video I took to show you the problem.

When he is doing the intro animation, the clipping attachment is activated. After the animation is finished, the attachment becomes disabled. And as you can see, the flickering doesn't work when the animation clip has the activated clipping attachment. Is this a bug from Spine? Or is this a Unity shader problem that I don't know? (and if it is, I hope I can get the solution from here, since I don't know anything about shaders, to be honest. :tear: )

In case if you need more info, I'm using the URP and the material's shader is the default URP/2D/Spine/Sprite.
madbaek
  • Postovi: 5

Jamez0r

Hey Madbaek - cool looking game!

I'm under the impression that the Clipping in Spine is not related to the material/shader in any way. I believe it processes the clipping when it creates the Mesh, which I believe is also why it can be very expensive on the CPU to calculate the clipping. So as a quick side note, definitely be aware of how expensive it is to calculate the clipping, and only use it when absolutely necessary, and set it up so that the clipping only affects the slots/images that you need it to affect - we had a character who had an animation where his sword was sticking in the ground, and were using clipping to clip the end of the sword so it looked like it was in the ground, but the clipping was set up to affect ALL of the slots/images on the entire character (instead of just the sword), and when I profiled it in Unity it was taking something like 20% of the CPU :rofl:

But the fact that you're only having this issue on the animation that uses the clipping is interesting - have you tried disabling the clipping on the animation and testing that out, to confirm that it is directly related to the clipping itself and not something else?

Lemme know what happens when you try that and I'll see if I can help some more.
Avatar
Jamez0r
  • Postovi: 356

madbaek

Jamez0r je napisao/la:Hey Madbaek - cool looking game!
Awww, thank you.
Jamez0r je napisao/la:But the fact that you're only having this issue on the animation that uses the clipping is interesting - have you tried disabling the clipping on the animation and testing that out, to confirm that it is directly related to the clipping itself and not something else?

Lemme know what happens when you try that and I'll see if I can help some more.
So here is a test result.

Also, as you can see, even the sprite mask disables the material parameter tweak. I suspect this "bug" is heavily related to the mask function itself? :think:
madbaek
  • Postovi: 5

Jamez0r

Huh - very interesting - maybe the clipping (and masking?) is affecting something with the vertices that doesn't jive with the "Color Adjustment" setting on the shader. I admittedly haven't used or tested the Color Adjustment settings on the URP2D/Spine/Sprite shader before (might be a relatively new addition, haven't seen it before).

Harald can probably provide some more info on whats going on, but I'm not sure what everyone's schedule will be like since its close to Christmas :p

Off topic, but if you're using URP with the 2D Renderer, I made a discord server for a small group of devs for asking questions / getting feedback (most of us are also using Spine for animation) - if you want to join heres a link: https://discord.gg/2tTVhzCy We're trying to keep the server pretty small / tight knit - everyone in there is pretty busy working on their projects, so the server isn't super active, but anytime anyone has a question about something its a good place to ask :yes:
Avatar
Jamez0r
  • Postovi: 356

Harald

@madbaek Your game has a cool mood!
madbaek je napisao/la:
meshRenderer.material.SetColor(colorProperty, new Color(1, 1, 1, 0.7f));
meshRenderer.material.SetFloat(brightnessProperty, 2);
In general it is not recommended to set the two parameters directly at a material, but to use MaterialPropertyBlock instead, or even better suited for your case, to use CustomMaterialOverride instead. Please see the spine-unity documentation here) on changing Materials per instance.

Multiple problems with the above code will arise when you have more than one atlas page, which will result in two (or more) materials at the MeshRenderer. Your code creates a copy of the Material (twice) and it also only changes the material of the first submesh (since you're using meshRenderer.material instead of .materials[i]). When draw order or similar things change, materials can be re-assigned at the MeshRenderer from the atlas page settings (to fit the atlas page material to the submesh), which will leave your modified Material copy floating around in the void.

So easiest solutions would be to use the CustomMaterialOverride workflow.

Please also be sure to use the latest spine-unity 4.0 unitypackage, there have recently been some bugfixes regarding hidden modified Materials that Unity creates when masks are involved.
Avatar
Harald

Harri
  • Postovi: 3914

madbaek

Yeah, everything is working out to be good. Many thanks.
madbaek
  • Postovi: 5

Harald

Glad to hear you've figured it out, thanks for getting back to us.
Avatar
Harald

Harri
  • Postovi: 3914


Natrag na Unity