• RuntimesUnity
  • Question About the "SPINE_TRIANGLECHECK"

Good afternoon,

In order to delve into performance-related issues, I have recently been carefully examining the code of the Spine-Unity runtime.
The Spine-Unity runtime has many performance optimization techniques, such as double buffering and single submesh optimization.
I found one of the optimizations involves avoiding repeated calls to mesh.SetTriangles by comparing render instructions between frames. But The comment for #define SPINE_TRIANGLECHECK says: "Not for optimization. Do not disable."

I would like to ask if this switch is used for performance optimization. If not, why is it advised not to disable it? When the switch is enabled, although it avoids the call to SetTriangles, it adds overhead due to mesh comparisons. Which of these two performance costs is greater? And Why don't I see similar operations in other spine runtimes (ue, threejs,etc)?

If you could provide some insights or clarify this, I would greatly appreciate it. Thank you very much for your assistance.

    Related Discussions
    ...

    The SPINE_TRIANGLECHECK directive in the Spine-Unity runtime is not primarily intended for performance optimization. Instead, it serves a crucial role in ensuring the correctness of the rendering process. The comment "Not for optimization. Do not disable." indicates that this check is essential for maintaining the integrity of the mesh data and preventing potential rendering issues.

    When SPINE_TRIANGLECHECK is enabled, it avoids unnecessary calls to mesh.SetTriangles by comparing render instructions between frames. While this does introduce some overhead due to mesh comparisons, it is generally outweighed by the cost of repeatedly setting triangles, which can be more expensive in terms of performance. The comparison ensures that SetTriangles is only called when there is an actual change, thus balancing the performance cost.

    The reason you might not see similar operations in other Spine runtimes (like UE or Three.js) is due to differences in how these platforms handle mesh data and rendering. Each runtime is optimized according to the specific needs and capabilities of the platform it targets. Unity, for instance, has particular performance characteristics and constraints that necessitate such checks to ensure efficient rendering.

    For more detailed information on performance optimization in Spine-Unity, you can refer to the following resources:

    These resources provide comprehensive guidelines on optimizing your Spine animations and understanding the trade-offs involved in different optimization techniques.

      Spinebot
      Sorry, I didn’t quite understand what "maintaining the integrity of the mesh data and preventing potential rendering issues" specifically refers to. Could you clarify?

      @jonomidi Sorry that Spinebot's reply was somewhat confusing.

      We just noticed that the define SPINE_TRIANGLECHECK was actually leading to compile errors when disabled. We've fixed this in this commit. We also adjusted the misleading documentation, the SPINE_TRIANGLECHECK flag is for optimization and can be disabled again. Be sure to disable all 4 occurrances of it in the project.

      Nevertheless, it is quite unlikely that you will receive better performance with the flag disabled, as also the previously mentioned faster BuildMeshWithArrays method call will only be used when the flag is enabled. As always, only profiling with your real-life use case on the target device can give a definitive answer.

      A new spine-unity 4.2 unitypackage is available:
      https://esotericsoftware.com/spine-unity-download

      Issue ticket URL for later reference
      EsotericSoftware/spine-runtimes2632

      jonomidi And Why don't I see similar operations in other spine runtimes (ue, threejs,etc)?

      This is because mesh generation depends heavily on the respective game toolkit's API and restrictions.

        Harald Thank you so much for your response. I noticed that you even submitted a PR to solve the issue very quickly. I'm truly grateful!

        Thanks for your kind words, glad it helped! Also thanks for bringing this to our attention!

        @Harald
        Hi, I've encountered another question and wanted to confirm something. In the GeometryNotEqual method of SkeletonRenderInstruction, this piece of code only takes effect when the SPINE_TRIANGLECHECK switch is turned off:

        // In normal immutable triangle use, immutableTriangles will be initially false, forcing the smartmesh to update the first time but never again after that, unless there was an immutableTriangles flag mismatch..
        if (a.immutableTriangles || b.immutableTriangles)
        return (a.immutableTriangles != b.immutableTriangles);

        return true;

        I'm wondering, are SkeletonRenderer's immutableTriangles and SPINE_TRIANGLECHECK mutually exclusive? I searched the code and found that the variable immutableTriangles is only used in GeometryNotEqual.

        @jonomidi Thanks for reporting, good find, this might actually be a bug. We will investigate the issue and post again here on the forum.

        5 jours plus tard

        @jonomidi This indeed seems to be a bug, the property currently has no effect. Unfortunately we can't include the fix in 4.2, as it is a breaking change, it will be integrated in the next 4.3-beta.

        We've opened an issue ticket here:
        EsotericSoftware/spine-runtimes2636

        You can find a manual update of the SkeletonRendererInstruction file here (also attached as comment to the issue ticket):

        skeletonrendererinstruction-fix-immutable-2636.zip
        3kB