Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using System.Text;
- using System;
- public class BeeModelRender : MonoBehaviour {
- // Start is called before the first frame update
- public IBeeObject bee;
- void Start() {
- string json = ReadFile(Application.streamingAssetsPath + "/Json/BeeModel.json");
- BeeObject beeObj = JsonUtility.FromJson<BeeObject>(json);
- bee = new IBeeObject(beeObj);
- }
- // Update is called once per frame
- void Update() {
- }
- private string ReadFile(string fileName) {
- try {
- string output = "";
- string line;
- StreamReader theReader = new StreamReader(fileName, Encoding.Default);
- using (theReader) {
- do {
- line = theReader.ReadLine();
- if (line != null){
- output += line + "\n";
- }
- } while (line != null);
- output = ReplaceLast(output, "\n", "");
- theReader.Close();
- return output;
- }
- } catch (Exception e){
- Console.WriteLine("{0}\n", e.Message);
- return null;
- }
- }
- private string ReplaceLast(string source, string key, string replace) {
- int place = source.LastIndexOf(key);
- if(place == -1) return source;
- string result = source.Remove(place, key.Length).Insert(place, replace);
- return result;
- }
- [Serializable]
- public class IBeeObject {
- public IBone body;
- public IBone torso;
- public IBone stinger;
- public IBone leftAntenna;
- public IBone rightAntenna;
- public IBone leftWing;
- public IBone rightWing;
- public IBone frontLegs;
- public IBone middleLegs;
- public IBone backLegs;
- List<IBone> bones = new List<IBone>();
- public IBeeObject(BeeObject bee) {
- body = new IBone(bee.body, "body");
- torso = new IBone(bee.torso, "torso");
- stinger = new IBone(bee.stinger, "stinger");
- leftAntenna = new IBone(bee.leftAntenna, "leftAntenna");
- rightAntenna = new IBone(bee.rightAntenna, "rightAntenna");
- leftWing = new IBone(bee.leftWing, "leftWing");
- rightWing = new IBone(bee.rightWing, "rightWing");
- frontLegs = new IBone(bee.frontLegs, "frontLegs");
- middleLegs = new IBone(bee.middleLegs, "middleLegs");
- backLegs = new IBone(bee.backLegs, "backLegs");
- bones.Add(body);
- bones.Add(torso);
- bones.Add(stinger);
- bones.Add(leftAntenna);
- bones.Add(rightAntenna);
- bones.Add(leftWing);
- bones.Add(rightWing);
- bones.Add(frontLegs);
- bones.Add(middleLegs);
- bones.Add(backLegs);
- AddParents();
- }
- public void AddParents() {
- foreach (IBone bone in bones) {
- if (bone.parent != "") {
- bone.bone.transform.SetParent(GetBoneTransform(bone.parent));
- }
- }
- }
- public Transform GetBoneTransform(string name) {
- IBone bone = GetBone(name);
- if (bone == null) return null;
- else return bone.bone.transform;
- }
- public IBone GetBone(string name) {
- foreach (IBone bone in bones) {
- if (bone.bone.name == name) {
- return bone;
- }
- }
- return null;
- }
- }
- [Serializable]
- public class IBone {
- [SerializeField]
- public Vector3 pivotPoint;
- public bool isPivotCentred;
- [SerializeField]
- public Vector3 rotation;
- public List<IPart> parts = new List<IPart>();
- public string parent;
- public GameObject bone = new GameObject();
- public IBone(Bone bone, string name) {
- pivotPoint = bone.GetPivotPoint();
- isPivotCentred = bone.isPivotCentred;
- rotation = bone.GetRotation();
- foreach (Part part in bone.parts) {
- parts.Add(new IPart(part, this));
- }
- parent = bone.parent;
- this.bone.name = name;
- }
- }
- [Serializable]
- public class IPart {
- [SerializeField]
- private Vector3 position;
- [SerializeField]
- private Vector4 size;
- [SerializeField]
- private Vector3 pivotPoint;
- public bool isPivotCentred;
- [SerializeField]
- private Vector3 rotation;
- [SerializeField]
- private Vector2 textureOffset;
- public GameObject scale = new GameObject();
- public GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
- public IBone parent;
- public IPart(Part part, IBone parent) {
- this.parent = parent;
- this.position = part.GetPosition();
- this.size = part.GetSize();
- this.pivotPoint = part.GetPivotPoint();
- this.isPivotCentred = part.isPivotCentred;
- this.rotation = part.GetRotation();
- this.textureOffset = part.GetTextureOffset();
- this.scale.name = "part";
- this.scale.transform.SetParent(parent.bone.transform);
- this.cube.transform.SetParent(scale.transform);
- this.cube.transform.localPosition = pivotPoint;
- this.scale.transform.localScale = new Vector3(size[3], size[3], size[3]);
- this.cube.transform.localScale = new Vector3(size[0], size[1], size[2]);
- this.scale.transform.localPosition = position;
- this.cube.transform.localRotation = Quaternion.Euler(rotation[0], rotation[1], rotation[2]);
- }
- }
- [Serializable]
- public class BeeObject {
- public Bone body;
- public Bone torso;
- public Bone stinger;
- public Bone leftAntenna;
- public Bone rightAntenna;
- public Bone leftWing;
- public Bone rightWing;
- public Bone frontLegs;
- public Bone middleLegs;
- public Bone backLegs;
- }
- [Serializable]
- public class Bone {
- public float[] pivotPoint;
- public bool isPivotCentred;
- public float[] rotation;
- public Part[] parts;
- public string parent;
- public Vector3 GetPivotPoint() {
- return new Vector3(pivotPoint[0], pivotPoint[1], pivotPoint[2]);
- }
- public Vector3 GetRotation() {
- return new Vector3(rotation[0], rotation[1], rotation[2]);
- }
- }
- [Serializable]
- public class Part {
- public float[] position;
- public float[] size;
- public float[] pivotPoint;
- public bool isPivotCentred;
- public float[] rotation;
- public float[] textureOffset;
- public Vector3 GetPosition() {
- return new Vector3(position[0], position[1], position[2]);
- }
- public Vector4 GetSize() {
- return new Vector4(size[0], size[1], size[2], size[3]);
- }
- public Vector3 GetPivotPoint() {
- return new Vector3(pivotPoint[0], pivotPoint[1], pivotPoint[2]);
- }
- public Vector3 GetRotation() {
- return new Vector3(rotation[0], rotation[1], rotation[2]);
- }
- public Vector2 GetTextureOffset() {
- return new Vector3(textureOffset[0], textureOffset[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement