Guest User

Untitled

a guest
Apr 14th, 2023
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8.  
  9. public class SkeletonLoad : MonoBehaviour
  10. {
  11.     private const string fileName = "avatar_skeleton.xml";
  12.     public GameObject bonePrefab;
  13.     public GameObject collisionPrefab;
  14.  
  15.     public Dictionary<string, Transform> bones = new Dictionary<string, Transform>();
  16.     public Dictionary<string, int> boneIndices = new Dictionary<string, int>();
  17.     public Dictionary<string, Transform> collisionVolumes = new Dictionary<string, Transform>();
  18.  
  19.     public SkinnedMeshRenderer[] renderers;
  20.  
  21.     public Transform[] boneTransforms;
  22.  
  23.     public class LLMBone
  24.     {
  25.         public Transform transform;
  26.         public Vector3 position;
  27.         public Vector3 rotation;
  28.         public Vector3 scale;
  29.         public Vector3 end;
  30.         public Vector3 pivot;
  31.         public string group;
  32.         public string aliases;
  33.         public bool connected;
  34.         public string support;
  35.  
  36.         public LLMBone(Transform transform, Vector3 position, Vector3 rotation, Vector3 scale, Vector3 end, Vector3 pivot, string group, string aliases, bool connected, string support)
  37.         {
  38.             Quaternion boneRotOffset = Quaternion.Euler(0f,90f,0f);
  39.             this.transform = transform;
  40.             //this.position = position; //use transform position for now because it inherits child positions properly
  41.                                         //this might need to change to a purely mathematical version in the future
  42.                                         //but we could always just spawn he skeleton like this at load and then
  43.                                         //instantiate it any time it's needed by a mesh.
  44.             this.position = transform.position;
  45.             this.rotation = rotation;
  46.             this.scale = scale;
  47.             //this.end = boneRotOffset * end;
  48.             this.end = end;
  49.             this.pivot = pivot;
  50.             this.aliases = aliases;
  51.             this.connected = connected;
  52.             this.support = support;
  53.         }
  54.  
  55.     }
  56.  
  57.     public Dictionary<string, LLMBone> llmBones = new Dictionary<string, LLMBone>();
  58.    
  59.     private void Awake()
  60.     {
  61.         var xmlDoc = new XmlDocument();
  62.         var stream = new FileStream(@$"{Application.dataPath}/character/{fileName}", FileMode.Open);
  63.         xmlDoc.Load(stream);
  64.  
  65.         var rootNode = xmlDoc.DocumentElement;
  66.  
  67.         var numBones = int.Parse(rootNode.Attributes["num_bones"].Value);
  68.  
  69.         var boneList = rootNode.GetElementsByTagName("bone");
  70.         var collisionList = rootNode.GetElementsByTagName("collision_volume");
  71.         int count = 0;
  72.         foreach (XmlNode boneNode in boneList)
  73.         {
  74.             var boneName = boneNode.Attributes["name"].Value;
  75.             var bonePosition = ParseVector3(boneNode.Attributes["pos"].Value);
  76.             var boneRotation = ParseVector3Rot(boneNode.Attributes["rot"].Value);
  77.             var boneScale = ParseVector3(boneNode.Attributes["scale"].Value);
  78.             var bonePivot = ParseVector3(boneNode.Attributes["pivot"].Value);
  79.  
  80.             var boneGameObject = Instantiate<GameObject>(bonePrefab);
  81.             boneGameObject.name = boneName;
  82.             boneGameObject.transform.parent = transform;
  83.             boneGameObject.transform.localPosition = bonePosition;
  84.             boneGameObject.transform.localEulerAngles = boneRotation * Mathf.Rad2Deg;
  85.             boneGameObject.transform.localScale = boneScale;
  86.             bones.Add(boneName, boneGameObject.transform);
  87.         }
  88.  
  89.         boneTransforms = new Transform[bones.Count];
  90.  
  91.         foreach (XmlNode boneNode in boneList)
  92.         {
  93.             var boneName = boneNode.Attributes["name"].Value;
  94.             //if (boneName == "mPelvis") continue;
  95.             var bonePosition = ParseVector3(boneNode.Attributes["pos"].Value);
  96.             var boneRotation = ParseVector3Rot(boneNode.Attributes["rot"].Value);
  97.             var boneScale = ParseVector3(boneNode.Attributes["scale"].Value);
  98.             if (boneName != "mPelvis") bones[boneName].parent = bones[boneNode.ParentNode.Attributes["name"].Value];
  99.             bones[boneName].localPosition = bonePosition;
  100.             bones[boneName].localEulerAngles = boneRotation * Mathf.Rad2Deg;
  101.             bones[boneName].localScale = boneScale;
  102.  
  103.             boneIndices.Add(boneName, count);
  104.             boneTransforms[count] = bones[boneName];
  105.  
  106.             llmBones.Add(boneName, new LLMBone
  107.             (
  108.                 bones[boneName],
  109.                 bonePosition,
  110.                 boneRotation * Mathf.Rad2Deg,
  111.                 boneScale,
  112.                 ParseVector3(boneNode.Attributes["end"].Value),
  113.                 ParseVector3(boneNode.Attributes["pivot"].Value),
  114.                 boneNode.Attributes["group"].Value,
  115.                 boneNode.Attributes["aliases"].Value,
  116.                 (boneNode.Attributes["connected"].Value != "false"),
  117.                 boneNode.Attributes["support"].Value
  118.             ));
  119.  
  120.             count++;
  121.  
  122.         }
  123.  
  124.         foreach (SkinnedMeshRenderer r in renderers)
  125.         {
  126.             //r.rootBone = bones["mPelvis"];
  127.             AvatarLadLoad av = r.GetComponent<AvatarLadLoad>();
  128.             av.skeleton = this;
  129.         }
  130.  
  131.         foreach (XmlNode boneNode in boneList)
  132.         {
  133.             Vector3 bonePos = ParseVector3(boneNode.Attributes["end"].Value);
  134.             Vector3 boneEnd = ParseVector3(boneNode.Attributes["end"].Value);
  135.             Debug.DrawLine(bonePos, boneEnd, Color.red, 100000f);
  136.         }
  137.  
  138.         /*foreach (XmlNode collision in collisionList)
  139.         {
  140.             var boneName = collision.ParentNode.Attributes["name"].Value;
  141.             var collisionName = collision.Attributes["name"].Value;
  142.             if (boneName == "mPelvis") continue;
  143.             var bonePosition = ParseVector3(collision.Attributes["pos"].Value);
  144.             var boneRotation = ParseVector3Rot(collision.Attributes["rot"].Value);
  145.             var boneScale = ParseVector3(collision.Attributes["scale"].Value);
  146.             var collisionGameObject = Instantiate<GameObject>(collisionPrefab);
  147.             collisionVolumes.Add(collisionName, collisionGameObject.transform);
  148.             //var bonePivot = ParseVector3(collision.Attributes["pivot"].Value);
  149.             collisionGameObject.name = collisionName;
  150.             collisionGameObject.transform.parent = bones[boneName];
  151.             collisionGameObject.transform.localPosition = bonePosition;
  152.             collisionGameObject.transform.eulerAngles = (boneRotation * Mathf.Rad2Deg) + new Vector3(0f,90f,0f);
  153.             collisionGameObject.transform.localScale = boneScale;
  154.         }*/
  155.  
  156.         stream.Close();
  157.     }
  158.  
  159.     bool showBento = false;
  160.     private void Update()
  161.     {
  162.         //Draw bone from position to parent position
  163.         /*foreach (KeyValuePair<string, Transform> kvp in bones)
  164.         {
  165.             Transform childTransform = kvp.Value;
  166.             Transform parentTransform = childTransform.parent;
  167.  
  168.             // If the child has a parent, draw a line between them
  169.             if (parentTransform != null && kvp.Key != "mPelvis")
  170.             {
  171.                 Debug.DrawLine(childTransform.position, parentTransform.position, Color.red, 0.05f);
  172.             }
  173.         }*/
  174.         if(Input.GetKeyDown(KeyCode.B))
  175.         {
  176.             showBento = !showBento;
  177.         }
  178.         //Draw bone from position to end
  179.         foreach (KeyValuePair<string, LLMBone> kvp in llmBones)
  180.         {
  181.             if(kvp.Value.support=="base" || showBento)
  182.                 Debug.DrawLine(kvp.Value.transform.position, kvp.Value.transform.position + (kvp.Value.transform.rotation * kvp.Value.end), GetColorFromStringHash(kvp.Key), 0.025f);
  183.         }
  184.  
  185.     }
  186.  
  187.     public static Color GetColorFromStringHash(string str)
  188.     {
  189.         // Generate a hash code from the input string
  190.         int hash = str.GetHashCode();
  191.  
  192.         // Convert the hash code to a float between 0 and 1
  193.         float hue = Mathf.Abs(hash % 360) / 360f;
  194.  
  195.         // Set the saturation and value to fixed values
  196.         float saturation = 0.8f;
  197.         float value = 0.9f;
  198.  
  199.         // Convert the HSV values to RGB values
  200.         Color color = Color.HSVToRGB(hue, saturation, value);
  201.  
  202.         return color;
  203.     }
  204.  
  205.     private static Vector3 ParseVector3(string value)
  206.     {
  207.         var elements = value.Split(' ');
  208.         var x = float.Parse(elements[0]);
  209.         var y = float.Parse(elements[1]);
  210.         var z = float.Parse(elements[2]);
  211.         return new Vector3(x, z, y);
  212.     }
  213.  
  214.     private static Vector3 ParseVector3Rot(string value)
  215.     {
  216.         var elements = value.Split(' ');
  217.         var x = float.Parse(elements[0]);
  218.         var y = float.Parse(elements[1]);
  219.         var z = float.Parse(elements[2]);
  220.         return new Vector3(y, x, z);
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment