Tanoda
GLTFastLoader.cs
Go to the documentation of this file.
1using GILES;
2using GILES.Interface;
3using GLTFast;
4using NaughtyAttributes;
5using System;
6using System.Collections;
7using System.Collections.Generic;
8using System.Threading;
9using System.Threading.Tasks;
10using UnityEngine;
11
12public class GLTFastLoader : MonoBehaviour
13{
14 public static GLTFastLoader instance;
15
16 public string CurrentFileName = "";
17 public GameObject LastLoadedGameObject = null;
18
19 private void Awake()
20 {
21 instance = this;
22 }
23 public GameObject Load(byte[] data, string filename, GameObject wrapper = null, bool singleModel = false, bool global = false)
24 {
25 CurrentFileName = System.IO.Path.GetFileName(filename);
26 var parent = pb_Scene.instance.gameObject;
27 if (wrapper)
28 parent = wrapper;
29
30 var gltf = new GltfImport();
31 var loadingSuccess = AsyncHelpersBuild.RunSync(() => gltf.LoadGltfBinary(data));
32 if (loadingSuccess)
33 {
34 gltf.InstantiateMainScene(new GLTFastInstantiator(gltf, parent.transform));
35 }
36
37 try
38 {
39 LastLoadedGameObject.name = filename.Remove(0, filename.LastIndexOf("\\") + 1);
40 }
41 catch (Exception)
42 {
43 LastLoadedGameObject.name = filename;
44 }
45
46 var tc = LastLoadedGameObject.AddComponent<TagCloud>();
47 tc.filePath = filename;
48 tc.TryLoad();
49
50 ComponentAdder(LastLoadedGameObject);
51
52 LastLoadedGameObject.AddComponent<RenameObject>();
53
55
57 }
58
59
60 void ComponentAdder(GameObject go)
61 {
62 if (go.GetComponent<MeshFilter>() && !go.GetComponent<Collider>())
63 {
64 var coll = go.AddComponent<MeshCollider>();
65 coll.convex = true;
66 }
67 if (go.GetComponent<MeshRenderer>())
68 {
69 var mr = go.GetComponent<MeshRenderer>();
70 foreach (var material in mr.materials)
71 {
72 if (material.mainTexture == null)
73 {
74 material.mainTexture = Resources.Load<Texture>("2k_white");
75 }
76 }
77 //var inkCanvas = go.AddComponent<InkCanvas>();
78 //BUG: memory leak lényegében túl sokat zabál
79 //TODO: csinálni egy Paintable flag-et, és/vagy fixelni a irdatlan memória használatát
80 }
81
82 for (int i = 0; i < go.transform.childCount; i++)
83 {
84 ComponentAdder(go.transform.GetChild(i).gameObject);
85 }
86 }
87
88 [Button]
89 void Teszt()
90 {
91 Load(System.IO.File.ReadAllBytes(@"D:\GIT\UNITY\vrtanoda\VRtanoda tools\Tooltable\tooltable.glb"), "tooltable.glb");
92 }
93
94}
95
96public static class AsyncHelpersBuild
97{
98 private class ExclusiveSynchronizationContext : SynchronizationContext
99 {
100 private bool done;
101
102 private readonly AutoResetEvent workItemsWaiting = new AutoResetEvent(initialState: false);
103
104 private readonly Queue<Tuple<SendOrPostCallback, object>> items = new Queue<Tuple<SendOrPostCallback, object>>();
105
106 public Exception InnerException
107 {
108 get;
109 set;
110 }
111
112 public override void Send(SendOrPostCallback d, object state)
113 {
114 throw new NotSupportedException("We cannot send to our same thread");
115 }
116
117 public override void Post(SendOrPostCallback d, object state)
118 {
119 lock (items)
120 {
121 items.Enqueue(Tuple.Create(d, state));
122 }
123
124 workItemsWaiting.Set();
125 }
126
127 public void EndMessageLoop()
128 {
129 Post(delegate
130 {
131 done = true;
132 }, null);
133 }
134
135 public void BeginMessageLoop()
136 {
137 while (!done)
138 {
139 Tuple<SendOrPostCallback, object> tuple = null;
140 lock (items)
141 {
142 if (items.Count > 0)
143 {
144 tuple = items.Dequeue();
145 }
146 }
147
148 if (tuple != null)
149 {
150 tuple.Item1(tuple.Item2);
151 if (InnerException != null)
152 {
153 throw new AggregateException("AsyncHelpers.Run method threw an exception.", InnerException);
154 }
155 }
156 else
157 {
158 workItemsWaiting.WaitOne();
159 }
160 }
161 }
162
163 public override SynchronizationContext CreateCopy()
164 {
165 return this;
166 }
167 }
168
169 public static void RunSync(Func<Task> task)
170 {
171 SynchronizationContext current = SynchronizationContext.Current;
172 ExclusiveSynchronizationContext synch = new ExclusiveSynchronizationContext();
173 SynchronizationContext.SetSynchronizationContext(synch);
174 synch.Post(async delegate
175 {
176 try
177 {
178 await task();
179 }
180 catch (Exception ex)
181 {
182 Exception e = ex;
183 synch.InnerException = e;
184 throw;
185 }
186 finally
187 {
188 synch.EndMessageLoop();
189 }
190 }, null);
191 synch.BeginMessageLoop();
192 SynchronizationContext.SetSynchronizationContext(current);
193 }
194
195 public static T RunSync<T>(Func<Task<T>> task)
196 {
197 SynchronizationContext current = SynchronizationContext.Current;
198 ExclusiveSynchronizationContext synch = new ExclusiveSynchronizationContext();
199 SynchronizationContext.SetSynchronizationContext(synch);
200 T ret = default(T);
201 synch.Post(async delegate
202 {
203 try
204 {
205 ret = await task();
206 }
207 catch (Exception ex)
208 {
209 Exception e = ex;
210 synch.InnerException = e;
211 throw;
212 }
213 finally
214 {
215 synch.EndMessageLoop();
216 }
217 }, null);
218 synch.BeginMessageLoop();
219 SynchronizationContext.SetSynchronizationContext(current);
220 return ret;
221 }
222}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
void AddGameObject(GameObject gobject, LoadPanel panel=LoadPanel.None, string filePath="", bool global=false)
static pb_PrefabBrowser instance
static GLTFastLoader instance
GameObject LastLoadedGameObject
GameObject Load(byte[] data, string filename, GameObject wrapper=null, bool singleModel=false, bool global=false)
string CurrentFileName
string filePath
Definition: TagCloud.cs:11