2 using BlenderMeshReader;
4 using System.Threading;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.Collections;
20 private volatile List<List<UnityMesh>> unityMeshes =
new List<List<UnityMesh>>();
22 private volatile List<BlenderObjectBlock> blenderObjects =
new List<BlenderObjectBlock>();
24 private bool loaded =
false;
25 private bool triggerEvent =
false;
26 private volatile string Path =
"";
31 public List<GameObject> MeshGameObjectContainers {
get; set; }
34 MeshGameObjectContainers =
new List<GameObject>();
40 PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Closed,
RemoveMesh);
46 PatientEventSystem.stopListening(PatientEventSystem.Event.PATIENT_Closed,
RemoveMesh);
54 StartCoroutine(
"LoadFileExecute");
55 unityMeshes =
new List<List<UnityMesh>>();
60 blenderObjects =
new List<BlenderObjectBlock>();
65 PatientEventSystem.triggerEvent (PatientEventSystem.Event.LOADING_RemoveLoadingJob,
"Mesh");
66 PatientEventSystem.triggerEvent(PatientEventSystem.Event.MESH_LoadedAll);
75 if (!File.Exists(pathToMeshJson))
77 Debug.LogWarning(
"Could not load mesh from: " + pathToMeshJson +
", file not found.");
82 string fileContent =
"";
84 System.IO.StreamReader file =
new System.IO.StreamReader(pathToMeshJson);
85 while ((line = file.ReadLine()) != null)
90 meshJson = JsonMapper.ToObject<
MeshJson>(fileContent);
93 Debug.LogWarning(
"Error while parsing mesh.json");
96 Patient currentPatient = Patient.getLoadedPatient();
97 string path = currentPatient.path +
"/" + meshJson.pathToBlendFile;
100 if (File.Exists (path)) {
103 PatientEventSystem.triggerEvent (PatientEventSystem.Event.LOADING_AddLoadingJob,
"Mesh");
106 MeshGameObjectContainers =
new List<GameObject>();
109 meshNode.transform.localScale =
new Vector3 (0.007f, 0.007f, 0.007f);
110 meshNode.transform.parent.localScale =
new Vector3 (1.0f, 1.0f, 1.0f);
111 meshNode.transform.parent.localRotation = Quaternion.identity;
113 meshNode.transform.parent.GetComponent<
ModelRotator>().setTargetOrientation( Quaternion.Euler(90,0,0) );
118 Debug.LogWarning (
"Could not load mesh from: '" + path +
"', file not found.");
125 private void LoadFileWorker(
object sender, DoWorkEventArgs e)
128 List<BlenderMesh> blenderMeshes =
new List<BlenderMesh>();
129 blenderObjects = b.readObject();
130 blenderMeshes = b.readMesh();
131 unityMeshes = BlenderFile.createSubmeshesForUnity(blenderMeshes);
136 private void LoadFileCallback(
object sender, RunWorkerCompletedEventArgs e)
141 Debug.Log(
"Loading cancelled");
142 }
else if (e.Error != null)
144 Debug.LogError(
"[MeshLoader.cs] Loading error");
145 Debug.LogError(e.Error);
156 private IEnumerator LoadFileExecute()
158 Bounds bounds =
new Bounds ();
159 bool boundsInitialized =
false;
174 foreach (List<UnityMesh> um
in unityMeshes) {
175 GameObject containerObject =
new GameObject(um[0].Name);
176 containerObject.layer = meshNode.layer;
177 containerObject.transform.SetParent( meshNode.transform, false );
179 containerObject.transform.localPosition =
new Vector3(0, 0, 0);
181 Color col = matColorForMeshName (um[0].Name);
182 matControl.setColor (col);
183 MeshGameObjectContainers.Add(containerObject);
188 if (b.uniqueIdentifier == um[0].UniqueIdentifier)
191 attachedObject.objectName = b.objectName;
192 attachedObject.location = b.location;
193 attachedObject.rotation = b.rotation;
196 containerObject.transform.localPosition =
new Vector3(b.location.x, b.location.y, -b.location.z);
210 GameObject objToSpawn =
new GameObject(unityMesh.Name);
211 objToSpawn.layer = meshNode.layer;
213 objToSpawn.transform.SetParent (containerObject.transform,
false);
216 objToSpawn.AddComponent<MeshFilter>();
217 objToSpawn.AddComponent<MeshCollider>();
218 objToSpawn.AddComponent<MeshRenderer>();
221 Mesh mesh =
new Mesh();
222 mesh.name = unityMesh.Name;
223 mesh.vertices = unityMesh.VertexList;
224 mesh.normals = unityMesh.NormalList;
225 mesh.triangles = unityMesh.TriangleList;
227 objToSpawn.GetComponent<MeshFilter>().mesh = mesh;
228 objToSpawn.GetComponent<MeshCollider>().sharedMesh = mesh;
230 objToSpawn.transform.localPosition =
new Vector3(0, 0, 0);
232 unityMeshes =
new List<List<UnityMesh>>();
239 Bounds worldBounds = TransformUtil.TransformBounds( objToSpawn.transform.parent, mesh.bounds );
240 Bounds localBounds = TransformUtil.InverseTransformBounds (containerObject.transform.parent, worldBounds);
241 if (!boundsInitialized) {
242 bounds = localBounds;
243 boundsInitialized =
true;
245 bounds.Encapsulate (localBounds);
249 matControl.changeOpactiyOfChildren (1f);
252 PatientEventSystem.triggerEvent (PatientEventSystem.Event.MESH_LoadedSingle, objToSpawn);
255 objToSpawn.SetActive( false );
261 matControl.startLoadingEffect ();
267 meshNode.GetComponent<
ModelMover> ().targetPosition = Vector3.Scale(-bounds.center,
meshNode.transform.localScale);
278 for (
int i = 0; i < meshNode.transform.childCount; i++)
280 if (
meshNode.transform.GetChild (i).name !=
"DICOMBounds" &&
281 meshNode.transform.GetChild (i).name !=
"DICOMVolume" ) {
282 Destroy (
meshNode.transform.GetChild (i).gameObject);
287 public Color matColorForMeshName(
string meshName )
291 if(mle.name == meshName)
293 return HexToColor (mle.color);
296 return new Color (0.7f, 0.5f, 0.2f);
301 public static string ColorToHex(Color32 color)
303 string hex = color.r.ToString(
"X2") + color.g.ToString(
"X2") + color.b.ToString(
"X2");
309 public static Color HexToColor(
string hex)
312 byte r = byte.Parse(hex.Substring(1,2), System.Globalization.NumberStyles.HexNumber);
313 byte g = byte.Parse(hex.Substring(3,2), System.Globalization.NumberStyles.HexNumber);
314 byte b = byte.Parse(hex.Substring(5,2), System.Globalization.NumberStyles.HexNumber);
315 return new Color32(r,g,b, 255);
List< MeshListElement > meshList
void LoadFile(string pathToMeshJson)
void RemoveMesh(object obj=null)