Tanoda
RuntimeCADLoader.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using CADLoader;
7using BasicLoader;
8using BasicLoader.Interface;
9using GILES;
10using UnityEngine;
11using UnityEngine.Networking;
12
13public class RuntimeCADLoader : MonoBehaviour
14{
15 public Material useMaterial;
16 private CADLoader.CADLoader _loader;
17 private Transform parent;
18
19 void Start()
20 {
21 parent = pb_Scene.instance.transform;
22 }
23 public void LoadUploadedModel(byte[] bytearray, string filename)
24 {
25 _loader = new CADLoader.CADLoader(new List<IParser> {STPLoader.ParserFactory.Create(), STLLoader.ParserFactory.Create(), ThreeDXMLLoader.ParserFactory.Create() });
26
27
28 //UnityWebRequest www = UnityWebRequest.Get(url);
29 //yield return www.SendWebRequest();
30 //
31 //if (www.isNetworkError || www.isHttpError)
32 //{
33 // Debug.Log(www.error);
34 //}
35 //else
36 {
37 var dataLoader = LoaderFactory.CreateStreamLoader(new MemoryStream(bytearray));
38 var type = CADTypeUtils.FromFileExtension(filename);
39 var model = _loader.Load(type, dataLoader);
40 var triangles = 0;
41 var vertices = 0;
42 try
43 {
44 Debug.Log("model.Triangles.Count: " + model.Triangles.Count);
45 triangles = model.Triangles.Count;
46 Debug.Log("model.Vertices.Count: " + model.Vertices.Count);
47 vertices = model.Vertices.Count;
48 Debug.Log("model.Parts.Count: " + model.Parts.Count);
49 Debug.Log("model.Facets.Count: " + model.Facets.Count);
50 }
51 catch (Exception)
52 {
53 // ignored
54 }
55 if (model.Parts != null && model.Parts.Count > 0)
56 {
57 var ParentObject = new GameObject("UploadedModel");
58 ParentObject.transform.parent = parent;
59 foreach (var modelPart in model.Parts)
60 {
61 var modelObject = Create("ModelPart");
62 UpdateMesh(modelObject, modelPart);
63 modelObject.transform.parent = ParentObject.transform;
64 }
65 SetupGameObject(ParentObject);
66 }
67 else
68 {
69 if (triangles == 0 || vertices == 0)
70 {
71 PopupManager.instance.ShowPopup("Invalid model", "The model was empty, or not recognizable. Try to save models as ASCII.");
72 // invalid model ?
73 }
74 else
75 {
76 var modelObject = Create("UploadedModel");
77 UpdateMesh(modelObject, model);
78 modelObject.transform.parent = parent;
79 SetupGameObject(modelObject);
80 }
81 }
82
83 dataLoader.Close();
84 }
85
86 //yield return null;
87 }
88
89
90
91 private void SetupGameObject(GameObject go)
92 {
93 go.AddComponent<AutoStageItem>();
94 go.AddComponent<Positioner>();
95 go.AddComponent<JSONPositioner>();
96 if (go.transform.childCount > 0)
97 for (int i = 0; i < go.transform.childCount; i++)
98 {
99 if (go.transform.GetChild(i).childCount > 0)
100 {
101 SetupGameObject(go.transform.GetChild(i).gameObject);
102 }
103 go.transform.GetChild(i).gameObject.AddComponent<AutoStageItem>();
104 go.transform.GetChild(i).gameObject.AddComponent<Positioner>();
105 go.transform.GetChild(i).gameObject.AddComponent<JSONPositioner>();
106 }
107 }
108
109
110 public GameObject Create(string name)
111 {
112 var modelObject = new GameObject(name);
113
114
115 modelObject.transform.GetComponent<MeshFilter>();
116
117 if (!modelObject.transform.GetComponent<MeshFilter>() || !modelObject.transform.GetComponent<MeshRenderer>())
118 {
119 modelObject.transform.gameObject.AddComponent<MeshFilter>();
120 modelObject.transform.gameObject.AddComponent<MeshRenderer>();
121 }
122
123 modelObject.transform.GetComponent<MeshRenderer>().material = useMaterial;
124
125 var mesh = new Mesh();
126 modelObject.transform.GetComponent<MeshFilter>().mesh = mesh;
127
128 return modelObject;
129 }
130
131 public static void UpdateMesh(GameObject gameObject, IModel model)
132 {
133 var mesh = gameObject.transform.GetComponent<MeshFilter>().mesh;
134
135 mesh.vertices = model.Vertices.Select<AForge.Math.Vector3, Vector3>(ToUnity).ToArray();
136 mesh.triangles = model.Triangles.ToArray();
137
138 mesh.RecalculateNormals();
139
140 gameObject.GetComponent<MeshFilter>().mesh = mesh;
141 }
142
143 public static void UpdateMesh(GameObject gameObject, IPart model)
144 {
145 var mesh = gameObject.transform.GetComponent<MeshFilter>().mesh;
146
147 mesh.vertices = model.Vertices.Select<AForge.Math.Vector3, Vector3>(ToUnity).ToArray();
148 mesh.triangles = model.Triangles.ToArray();
149
150 mesh.RecalculateNormals();
151
152 gameObject.GetComponent<MeshFilter>().mesh = mesh;
153 gameObject.transform.position = ToUnity(model.Position);
154 float x, y, z;
155 model.Rotation.ExtractYawPitchRoll(out y, out x, out z); //radian
156 x *= 180 / Mathf.PI;
157 y *= 180 / Mathf.PI;
158 z *= 180 / Mathf.PI;
159 gameObject.transform.rotation = Quaternion.Euler(new Vector3(x, y, z));
160 }
161
162 private static Vector3 ToUnity(AForge.Math.Vector3 v3)
163 {
164 return new Vector3(v3.X, v3.Z, v3.Y);
165 }
166
167 //private static Quaternion ToUnity(AForge.Math.Matrix3x3 m)
168 //{
169 // return Quaternion.LookRotation(new Vector3(m.V00,m.V01,m.V02), new Vector3(m.V10,m.V11,m.V12));
170 //}
171}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static void UpdateMesh(GameObject gameObject, IModel model)
void LoadUploadedModel(byte[] bytearray, string filename)
static void UpdateMesh(GameObject gameObject, IPart model)
GameObject Create(string name)