AnyPortrait > Forum > How to Use
Bone coordinates calculation
- This topic has 1 reply, 2 voices, and was last updated 4 days, 20 hours ago by
RainyRizzle.
-
AuthorPosts
-
July 21, 2026 at 10:12 pm #1622
m00nk
Hello.
In my project, I need to control character bones from my custom code. To position the bones, I use the SetBonePosition() method from the AnyPortrait API, and the bones move. But I don’t understand how to correctly calculate the coordinates of the required point within the AnyPortrait object.
Here’s a more detailed description of what I’m trying to do: I have a Canvas object in my scene, and one of its descendants contains an AnyPortrait component whose bones I’m trying to control. The Canvas itself operates in “Screen Space – Camera” mode, and the camera that renders this Canvas renders to a 2048×2048 (square) texture. The resulting texture is inserted into one of the objects of another Canvas, which is then rendered to the screen.
Since the texture’s aspect ratio doesn’t match the screen’s aspect ratio, and the final texture can also be scaled differently, I decided to convert the required screen coordinates of the point into relative texture coordinates of the rendered texture. That is, I have texture coordinates of the necessary point in the range (0-1, 0-1), where (0,0) is the lower-left corner of the texture, and (1,1) is the upper-right corner.
The question is, how do I convert these coordinates into coordinates suitable for passing to the SetBonePosition() method?
Sorry if the description is confusing; English is not my native language. I’m happy to provide a more detailed explanation if needed.
July 22, 2026 at 2:02 am #1623
RainyRizzle
Hi!
Since characters created with AnyPortrait are composed of Mesh Renderers, you can use functions for 3D objects when converting coordinate systems.
If the Canvas’s Render Mode is set to “Screen Space – Camera”, you can convert the coordinate system using the camera connected to the Canvas.Coordinate transformation consists of two steps.
The first step is to convert the input position values into a Screen Position based on the Camera.
The second step is to convert the transformed Screen Position back into a World Position input it into the character.
(The following explanation assumes that the “Canvas” object is referenced in the script.)1. Calculating Screen Position
When using a camera that utilizes a Canvas and Render Texture, the method for calculating the Screen Position varies depending on the characteristics of the input object or value.(1) If you want to use the location of another 3D object within the Canvas, you can write it as follows.
Vector2 screenPos = canvas.worldCamera.WorldToScreenPoint((3D object variable of type Transform).position);
(2) For 2D objects such as SpriteRenderer within a Canvas, they are referenced as “RectTransform”, so the Screen Position must be calculated in a slightly different way.
Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, (2D object variable of type RectTransform).position);
(3) If you want to directly input on-screen coordinates in a ratio of (0 to 1), you must convert the values using the camera’s screen size.
This method is the same when using Render Textures.Vector2 normalizedScreenPos = new Vector2(0.7f, 0.8f);
Vector2 screenPos = new Vector2(normalizedScreenPos.x * canvas.worldCamera.pixelWidth, normalizedScreenPos.y * canvas.worldCamera.pixelHeight);2. Applying Screen Position to AnyPortrait Character
The code to move the character’s bones by converting the previously calculated Screen Position to World Position is as follows:float distance = Vector3.Dot(portrait.transform.position – canvas.worldCamera.transform.position, canvas.worldCamera.transform.forward);
Vector3 worldPos = canvas.worldCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, distance));
portrait.SetBonePosition(boneName, worldPos, Space.World);Note) For the World Position Z value, if the Camera is Orthographic, it is fine to simply enter the character’s Z position value.
The World coordinate system referred to in AnyPortrait’s functions is the same as the World coordinate system used by “Mesh Renderers” and general Transforms.
Therefore, this topic is also about coordinate system transformations in Unity.
You can find more detailed explanations regarding coordinate system transformations in Unity’s documentation or forums.
Thank you.-
This reply was modified 4 days, 20 hours ago by
RainyRizzle.
-
This reply was modified 4 days, 20 hours ago by
-
AuthorPosts
- You must be logged in to reply to this topic.