• RuntimesUnity
  • How to set or change skeleton parts Render's bounds pivot?

I used the Skeleton Render Separator to separate the animation into multiple skeleton parts render.I noticed that the anchor point of each renderer child object is based on the center of the animation.


I need to map the click coordinates, map the screen coordinates to the meshrender within the rect local coordinates.Because of the skeleton parts render's pivot offset difference that the maping result is incorrect(when used API: Transform.InverseTransformPoint(clickPos)).
Here is my test code:

private Renderer myRender; //skeleton parts render
    private Vector2 mainTexSize, boundSize, halfBoundSize;
    void GetBrushPos(Vector2 screenPos)
    {
        myRender = GetComponent<MeshRenderer>();
        boundSize = myRender.bounds.size;
        mainTexSize = new Vector2(myRender.sharedMaterial.mainTexture.width, myRender.sharedMaterial.mainTexture.height);
        halfBoundSize = boundSize / 2;
        var clickPos = Camera.main.ScreenToWorldPoint(screenPos);
        var clicklocalPos = Vector2.Scale(transform.InverseTransformPoint(clickPos), transform.lossyScale) + halfBoundSize;
        var pixelPerInch = new Vector2(mainTexSize.x / boundSize.x / transform.lossyScale.x, mainTexSize.y / boundSize.y / transform.lossyScale.y);
        brushPos = Vector2.Scale(Vector2.Scale(clicklocalPos, transform.lossyScale), pixelPerInch);
    }

I tried to offset the vertices of the child mesh and redraw the mesh, but it didn't work because the mesh center point was zero.
So I would like to know whether it is possible to modify the center point of attachments in Spine?Or how to get the offset of renderer child bounds pivot in Unity?

    Related Discussions
    ...

    RalphYDY .I noticed that the anchor point of each renderer child object is based on the center of the animation.

    If by "center of the animation" you mean origin, yes.

    I need to map the click coordinates, map the screen coordinates to the meshrender within the rect local coordinates

    Unfortunately I don't quite understand what you mean by "within the rect local coordinates". Do you mean that you want to map the mouse-click screen coordinates to the skeleton coordinates? Or to which other RectTransform coordinates do you want to map it to?

    Please note that if you only want to map screen coordinates to any RectTransform coordinates, it has nothing to do with spine-unity. Thus you could search the normal Unity forums on how to transform coordinates from one space to another. In general if you want to transform local coords between Transform A and Transform B, you just need to transform from A to world-space and from world-space back to B.

    var clicklocalPos = Vector2.Scale(transform.InverseTransformPoint(clickPos), transform.lossyScale) + halfBoundSize;

    From reading this line of code, could it be that you want to map to 0-1 normalized RectTransform bounds? If so, why? In general it would be good if you could describe what you want to do, what problem you want to solve in the first place. From your attempted solution it's difficult to guess.

      RalphYDY What I forgot in my previous posting is that RectTransform and SkeletonGraphic mesh scale require a bit of additional work when trying to map screen space coords to skeleton coords.

      Please check out this forum thread where we have just added a related commit. Especially this code in the newly added example component might perhaps help you with your use case:
      SpineboyTargetControllerGraphic.cs : L55-L57

      Please note that you will need to update the spine-unity package since the skeletonGraphic.MeshScale property has just been added in the latest commit.

        Harald Thank you so much for your reply!I'm sorry I wasn't clear about my issues.Yes,I want to map to 0-1 normalized ‘RectTransform’ bounds.(The 01 result is multiplied by the size of the mesh.Bounds)Because I want to realize the function of drawing on the separated animation in parts Render.

        The anchor point of each renderer child object is based on the orgin of the animation. But their local position is zero.When I map the coordinates, It's always missing this offset.(When Used API transform.InverseTransformPoint(clickPos) Transforms position from world space to local space. )
        If i can modify the anchor point to zero in their separated parts Render as show below.

        or calculate this offsets,My mapping coordinates turned out to be correct.

          Harald Thank you for your prompt again~ Actually, The Spine I imported is not a SkeletonGraphic but SpineAnimation object.To be exact,I want to map the mouse-click screen coordinates to the skeleton MeshReder bounds coordinates.(The Render Bounds Rect like Previous picture) How can I achieve same functionality with this code? Or another way of map coordinates: Used Mesh Collider to detection mouse-click screen coordinates and map it to uv uv texture coordinate. But my object will scaling,
          I don't think it's appropriate.

          RalphYDY Yes,I want to map to 0-1 normalized ‘RectTransform’ bounds.
          Actually, The Spine I imported is not a SkeletonGraphic but SpineAnimation object.To be exact,I want to map the mouse-click screen coordinates to the skeleton MeshReder bounds coordinates.

          Now unfortunately these two lines confused me, as they seem somewhat contradictory. If you are using RectTransform, then you're using it under a Canvas which would mean that you should be using SkeletonGraphic (UI). But your screenshot shows a normal Transform, not a RectTransform. I assume that you are using RectTransform incorrectly here and didn't mean to, right?

          Because I want to realize the function of drawing on the separated animation in parts Render.

          How do you intend to do that?

          The anchor point of each renderer child object is based on the orgin of the animation. But their local position is zero.

          Yes, what do you expect it to be? It just splits one mesh draw call into multiple.

          When I map the coordinates, It's always missing this offset.

          What do you mean by "it's missing this offset"? There is no offset, and there should be none. 🙂

          RalphYDY If i can modify the anchor point to zero in their separated parts Render as show below.

          or calculate this offsets,My mapping coordinates turned out to be correct.

          Please note that we don't know your project as you do. We have no idea what is shown here and what you would expect it to look like.

          I'm afraid we need a detailed step-wise description of what you want to do and how you are doing it (show code), what you expect to see and what you see instead. The information you posted just mentions that you are mapping coordinates in an incorrect way related to certain offsets, but without knowing how you are attempting to do that, we can't really help I'm afraid.

            Harald I'm sorry, my translation wasn't good enough.I want to create a patining effect.(Paint on the separated rmeshrenders),Therefore, I need to accurately map the screen coordinates to meshReder.bounds range(That's why I used quotation marks in my previous response ‘RectTransform’.I don't know how to express it.Now I know it's mesh.bounds.)
            So, because the anchor point of the rendered skeleton parts is based on the center of the animation. when i click here ( upon red point),it map there(green point),I don't know if you can understand what I'm saying.

            However, I have already calculated the offset difference.this is unrelated to Spine-Unity.
            It's actually because I calculated the parameters at the wrong timing.
            Fortunately, I have already solved this issue.

            In any case, thank you very much for your patient responses!

              RalphYDY No need to be sorry! Very glad to hear you could resolve your issue!

              What I didn't quite understand was why you need to map to the MeshBounds. Anyway, since you now have solved the problem, there is no need to explain that any more. 🙂

                Harald Thank you very much! Harald 🙂 :By the way, I also wanted to ask if there is a callback event after changing the character's skin When i invoke Spine-Unity API :GetRemappedClone() ? As fellow:

                Because I want to change the properties on the material after separated meshrenders switching materials.I know that it is replacing the material as AtlasRegion.AtlasPage.rendererObject.
                I am currently using a delayed function to implement the change of material properties after replacement.The result is not very satisfactory. I cannot assign the properties to the material in advance and then change the skin.
                Do you have any good suggestions?
                Thank you again.

                  RalphYDY I also wanted to ask if there is a callback event after changing the character's skin
                  Because I want to change the properties on the material after separated meshrenders switching materials.

                  You can find a list of the available callback delegates here on the spine-unity documentation page:
                  https://esotericsoftware.com/spine-unity#Life-cycle
                  In your case you would register your method at skeletonRenderer.OnMeshAndMaterialsUpdated. Note that this callback is issued each frame, not only if the Materials have changed compared to the last frame.

                    Harald Thank you, I have learned something, and I will take a closer look at the Spine-Unity documentation.

                    Glad to hear it helped. 🙂