Advertisement
Vaerys_Dawn

BeeModelRenderer

Feb 5th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System.Text;
  6. using System;
  7.  
  8. public class BeeModelRender : MonoBehaviour {
  9.     // Start is called before the first frame update
  10.  
  11.     public IBeeObject bee;
  12.  
  13.     void Start() {
  14.       string json = ReadFile(Application.streamingAssetsPath + "/Json/BeeModel.json");
  15.       BeeObject beeObj = JsonUtility.FromJson<BeeObject>(json);
  16.       bee = new IBeeObject(beeObj);
  17.     }
  18.  
  19.     // Update is called once per frame
  20.     void Update() {
  21.  
  22.     }
  23.  
  24.     private string ReadFile(string fileName) {
  25.       try {
  26.         string output = "";
  27.         string line;
  28.         StreamReader theReader = new StreamReader(fileName, Encoding.Default);
  29.         using (theReader) {
  30.           do {
  31.             line = theReader.ReadLine();
  32.             if (line != null){
  33.               output += line + "\n";
  34.             }
  35.           } while (line != null);
  36.           output = ReplaceLast(output, "\n", "");
  37.           theReader.Close();
  38.           return output;
  39.         }
  40.       } catch (Exception e){
  41.         Console.WriteLine("{0}\n", e.Message);
  42.         return null;
  43.       }
  44.     }
  45.  
  46.     private string ReplaceLast(string source, string key, string replace) {
  47.       int place = source.LastIndexOf(key);
  48.       if(place == -1) return source;
  49.       string result = source.Remove(place, key.Length).Insert(place, replace);
  50.       return result;
  51.     }
  52.  
  53.     [Serializable]
  54.     public class IBeeObject {
  55.       public IBone body;
  56.       public IBone torso;
  57.       public IBone stinger;
  58.       public IBone leftAntenna;
  59.       public IBone rightAntenna;
  60.       public IBone leftWing;
  61.       public IBone rightWing;
  62.       public IBone frontLegs;
  63.       public IBone middleLegs;
  64.       public IBone backLegs;
  65.  
  66.       List<IBone> bones = new List<IBone>();
  67.  
  68.       public IBeeObject(BeeObject bee) {
  69.         body = new IBone(bee.body, "body");
  70.         torso = new IBone(bee.torso, "torso");
  71.         stinger = new IBone(bee.stinger, "stinger");
  72.         leftAntenna = new IBone(bee.leftAntenna, "leftAntenna");
  73.         rightAntenna = new IBone(bee.rightAntenna, "rightAntenna");
  74.         leftWing = new IBone(bee.leftWing, "leftWing");
  75.         rightWing = new IBone(bee.rightWing, "rightWing");
  76.         frontLegs = new IBone(bee.frontLegs, "frontLegs");
  77.         middleLegs = new IBone(bee.middleLegs, "middleLegs");
  78.         backLegs = new IBone(bee.backLegs, "backLegs");
  79.         bones.Add(body);
  80.         bones.Add(torso);
  81.         bones.Add(stinger);
  82.         bones.Add(leftAntenna);
  83.         bones.Add(rightAntenna);
  84.         bones.Add(leftWing);
  85.         bones.Add(rightWing);
  86.         bones.Add(frontLegs);
  87.         bones.Add(middleLegs);
  88.         bones.Add(backLegs);
  89.  
  90.         AddParents();
  91.       }
  92.  
  93.       public void AddParents() {
  94.         foreach (IBone bone in bones) {
  95.           if (bone.parent != "") {
  96.             bone.bone.transform.SetParent(GetBoneTransform(bone.parent));
  97.           }
  98.         }
  99.       }
  100.  
  101.       public Transform GetBoneTransform(string name) {
  102.         IBone bone = GetBone(name);
  103.         if (bone == null) return null;
  104.         else return bone.bone.transform;
  105.       }
  106.  
  107.       public IBone GetBone(string name) {
  108.         foreach (IBone bone in bones) {
  109.           if (bone.bone.name == name) {
  110.             return bone;
  111.           }
  112.         }
  113.         return null;
  114.       }
  115.     }
  116.  
  117.     [Serializable]
  118.     public class IBone {
  119.       [SerializeField]
  120.       public Vector3 pivotPoint;
  121.       public bool isPivotCentred;
  122.       [SerializeField]
  123.       public Vector3 rotation;
  124.       public List<IPart> parts = new List<IPart>();
  125.       public string parent;
  126.       public GameObject bone = new GameObject();
  127.  
  128.       public IBone(Bone bone, string name) {
  129.         pivotPoint = bone.GetPivotPoint();
  130.         isPivotCentred = bone.isPivotCentred;
  131.         rotation = bone.GetRotation();
  132.         foreach (Part part in bone.parts) {
  133.           parts.Add(new IPart(part, this));
  134.         }
  135.         parent = bone.parent;
  136.         this.bone.name = name;
  137.       }
  138.     }
  139.  
  140.     [Serializable]
  141.     public class IPart {
  142.       [SerializeField]
  143.       private Vector3 position;
  144.       [SerializeField]
  145.       private Vector4 size;
  146.       [SerializeField]
  147.       private Vector3 pivotPoint;
  148.       public bool isPivotCentred;
  149.       [SerializeField]
  150.       private Vector3 rotation;
  151.       [SerializeField]
  152.       private Vector2 textureOffset;
  153.  
  154.       public GameObject scale = new GameObject();
  155.       public GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  156.  
  157.       public IBone parent;
  158.  
  159.       public IPart(Part part, IBone parent) {
  160.         this.parent = parent;
  161.         this.position = part.GetPosition();
  162.         this.size = part.GetSize();
  163.         this.pivotPoint = part.GetPivotPoint();
  164.         this.isPivotCentred = part.isPivotCentred;
  165.         this.rotation = part.GetRotation();
  166.         this.textureOffset = part.GetTextureOffset();
  167.         this.scale.name = "part";
  168.         this.scale.transform.SetParent(parent.bone.transform);
  169.         this.cube.transform.SetParent(scale.transform);
  170.         this.cube.transform.localPosition = pivotPoint;
  171.         this.scale.transform.localScale = new Vector3(size[3], size[3], size[3]);
  172.         this.cube.transform.localScale = new Vector3(size[0], size[1], size[2]);
  173.         this.scale.transform.localPosition = position;
  174.         this.cube.transform.localRotation = Quaternion.Euler(rotation[0], rotation[1], rotation[2]);
  175.       }
  176.     }
  177.  
  178.     [Serializable]
  179.     public class BeeObject {
  180.       public Bone body;
  181.       public Bone torso;
  182.       public Bone stinger;
  183.       public Bone leftAntenna;
  184.       public Bone rightAntenna;
  185.       public Bone leftWing;
  186.       public Bone rightWing;
  187.       public Bone frontLegs;
  188.       public Bone middleLegs;
  189.       public Bone backLegs;
  190.     }
  191.  
  192.     [Serializable]
  193.     public class Bone {
  194.       public float[] pivotPoint;
  195.       public bool isPivotCentred;
  196.       public float[] rotation;
  197.       public Part[] parts;
  198.       public string parent;
  199.  
  200.       public Vector3 GetPivotPoint() {
  201.         return new Vector3(pivotPoint[0], pivotPoint[1], pivotPoint[2]);
  202.       }
  203.  
  204.       public Vector3 GetRotation() {
  205.         return new Vector3(rotation[0], rotation[1], rotation[2]);
  206.       }
  207.     }
  208.  
  209.     [Serializable]
  210.     public class Part {
  211.       public float[] position;
  212.       public float[] size;
  213.       public float[] pivotPoint;
  214.       public bool isPivotCentred;
  215.       public float[] rotation;
  216.       public float[] textureOffset;
  217.  
  218.       public Vector3 GetPosition() {
  219.         return new Vector3(position[0], position[1], position[2]);
  220.       }
  221.  
  222.       public Vector4 GetSize() {
  223.         return new Vector4(size[0], size[1], size[2], size[3]);
  224.       }
  225.  
  226.       public Vector3 GetPivotPoint() {
  227.         return new Vector3(pivotPoint[0], pivotPoint[1], pivotPoint[2]);
  228.       }
  229.  
  230.       public Vector3 GetRotation() {
  231.         return new Vector3(rotation[0], rotation[1], rotation[2]);
  232.       }
  233.  
  234.       public Vector2 GetTextureOffset() {
  235.         return new Vector3(textureOffset[0], textureOffset[1]);
  236.       }
  237.     }
  238. }
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement