-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3DFileManager.cs
More file actions
351 lines (317 loc) · 13.8 KB
/
_3DFileManager.cs
File metadata and controls
351 lines (317 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public enum T3DFileState { idle, loading, loaded, error };
public enum T3DFileOrigin { productModelJSON, url };
/// <summary> Struct to store the asset that has to be load</summary>
public class C3DFileData : CGeometryFileData
{
public uint fileID;
public T3DFileOrigin fileOrigin;
public T3DFileState fileState;
public C3DFileData(uint _fileID, string _fileName, string _fileUrl, string _fileType) {
this.fileID = _fileID;
this.fileName = _fileName;
this.fileUrl = _fileUrl;
this.fileType = _fileType;
}
public C3DFileData(uint _fileID, CGeometryFileData _fileData)
{
this.Clear();
this.fileID = _fileID;
this.fileName = _fileData.fileName;
this.fileUrl = _fileData.fileUrl;
this.fileType = _fileData.fileType;
this.version = _fileData.version;
this.crc = _fileData.crc;
this.assetName = _fileData.assetName;
this.active = _fileData.active;
this.invertZAxis = _fileData.invertZAxis;
this.position_x = _fileData.position_x;
this.position_y = _fileData.position_y;
this.position_z = _fileData.position_z;
this.rotation_w = _fileData.rotation_w;
this.rotation_x = _fileData.rotation_x;
this.rotation_y = _fileData.rotation_y;
this.rotation_z = _fileData.rotation_z;
this.scale_x = _fileData.scale_x;
this.scale_y = _fileData.scale_y;
this.scale_z = _fileData.scale_z;
}
public void Clear()
{
this.fileID = 0;
this.fileName = null;
this.fileUrl = null;
this.fileType = null;
this.version = 0;
this.crc = null;
this.assetName = null;
this.active = false;
this.position_x = 0d;
this.position_y = 0d;
this.position_z = 0d;
this.rotation_w = 0d;
this.rotation_x = 0d;
this.rotation_y = 0d;
this.rotation_z = 0d;
this.fileOrigin = T3DFileOrigin.productModelJSON;
this.fileState = T3DFileState.idle;
}
}
public class _3DFileManager : MonoBehaviour {
bool downloadingFile; // To store if we are downloading a file or not
bool coroutineStarted; // To know when the corouine is working
//public GameObject goScript { get; set; } // To store a link to the object that contain the scripts
Queue <C3DFileData> _3DFilesQueue;
List<CResourceFileData> resourceFilesList;
//List <C3DFileData> ResourceFilesList;
private void Awake()
{
_3DFilesQueue = new Queue<C3DFileData>();
resourceFilesList = new List<CResourceFileData>();
downloadingFile = false;
coroutineStarted = false;
}
/// <summary>Load 3D files</summary>
/// <param name="_3DFilesToDownload">List of Files to download</param>
public void Load3DFiles(List<C3DFileData> _3DFilesToDownload, List<CResourceFileData> _ResourcesFilesToDownload)
{
resourceFilesList.AddRange(_ResourcesFilesToDownload); //Store into resources files list
//Enqueue 3D file and start download
foreach (C3DFileData file in _3DFilesToDownload) { _3DFilesQueue.Enqueue(file); }
if (!coroutineStarted)
{
coroutineStarted = true;
StartCoroutine(CoroutineLoad3DFiles());
}
}
/// <summary>Load 3D file one by one</summary>
IEnumerator CoroutineLoad3DFiles()
{
C3DFileData file;
while (_3DFilesQueue.Count > 0)
{
file = _3DFilesQueue.Dequeue();
switch (file.fileType)
{
case "assetbundle":
StartAssetBundleDownload(file);
break;
case "obj":
StartOBJDownload(file);
break;
default:
SendMessageToUI("File type does't support by HOM3R");
break;
}
//We download one by one, so we wait until this file download have been finished
while (downloadingFile) { yield return new WaitForSeconds(1.0f); }
}
coroutineStarted = false;
}
/////////////////////////////////////
// Actions to start files Download
/////////////////////////////////////
/// <summary>Reset product root scale</summary>
private void ResetProductScale()
{
GameObject productRoot = hom3r.quickLinks._3DModelRoot;
productRoot.transform.localScale = new Vector3(1f, 1f, 1f);
}
/// <summary>Report to Model manager that one file has started its download</summary>
/// <param name="fileID">File ID</param>
private void SetFileDownloadStarted(uint fileID)
{
ResetProductScale();
downloadingFile = true;
this.GetComponent<ModelManager>().SetFileDownloadStarted(fileID); //Notify to the model management
}
/// <summary>Start Assetbundle file download</summary>
/// <param name="file">File to be downloaded</param>
private void StartAssetBundleDownload(C3DFileData file)
{
if (!downloadingFile)
{
SetFileDownloadStarted(file.fileID);
this.GetComponent<AssetBundleLoader>().ProcessAssetBundleDownload(file);
}
}
/// <summary>Start OBJ file download</summary>
/// <param name="file">File to be downloaded</param>
private void StartOBJDownload(C3DFileData file)
{
if (!downloadingFile)
{
SetFileDownloadStarted(file.fileID);
SendMessageToUI("Downloading 3D model from " + file.fileUrl, 1f);
this.GetComponent<OBJLoader>().StartOBJDownload(file, hom3r.quickLinks._3DModelRoot);
}
}
///////////////////////////////////////////////
// Actions after finished the file download
///////////////////////////////////////////////
/// <summary>Actions after assetbundle download from json product model</summary>
/// <param name="asset">asset data read from json</param>
/// <param name="loadedGameObject">Gameobject created by the aseetbundleloader</param>
public void ProcessAfterAssetBundleLoad(C3DFileData file, GameObject loadedGameObject)
{
if (file.fileOrigin == T3DFileOrigin.productModelJSON)
{
EmplaceAsset(file, loadedGameObject); //Make actions after load the assetBundle
}
else
{
this.GetComponent<ModelManager>().Add3DFileToProductModel(file.fileID, loadedGameObject);
}
AddComponents_and_Others(loadedGameObject); //Make actions after load the assetBundle
}
///// <summary>Actions after assetbundle download without json product model</summary>
///// <param name="fileID">file ID</param>
///// <param name="loadedGameObject">Gameobject created by the aseetbundleloader</param>
//public void ProcessAfterAssetBundleLoad(uint fileID, GameObject loadedGameObject)
//{
// this.GetComponent<ModelManager>().Add3DFileToProductModel(fileID, loadedGameObject);
// AddComponents_and_Others(loadedGameObject); //Make actions after load the assetBundle
//}
/// <summary>Actions after OBJ download without json product model</summary>
/// <param name="fileID">file ID</param>
/// <param name="loadedGameObject">Gameobject created by the OBJloader</param>
public void ProcessAfterOBJLoad(C3DFileData file, GameObject loadedGameObject)
{
if (file.fileOrigin == T3DFileOrigin.productModelJSON)
{
EmplaceAsset(file, loadedGameObject); //Make actions after loading the obj file
}
else
{
this.GetComponent<ModelManager>().Add3DFileToProductModel(file.fileID, loadedGameObject);
}
AddComponents_and_Others(loadedGameObject); //Make actions after loading the obj file
}
/// <summary>Report Model manager that one file has finished its download</summary>
/// <param name="fileID">file ID</param>
/// <param name="error">error result</param>
public void SetFileDownloadFinished(uint fileID, bool error)
{
if (downloadingFile)
{
//Error managemenet
T3DFileState downloadResult;
if (error) { downloadResult = T3DFileState.error; }
else { downloadResult = T3DFileState.loaded; }
//hom3r.coreLink.EmitEvent(new CCoreEvent(TCoreEvent.ModelManagement_3DLoadFinished));
this.GetComponent<ModelManager>().SetFileDownloadFinished(fileID, downloadResult); //Notify that we are finish the download of this file
downloadingFile = false;
}
}
/// <summary>Empace a Asset, loaded from a file, in the position and rotation read from the model.</summary>
/// <param name="_assetName">Id of the asset</param>
/// <param name="_object">Object that represent the asset when has been loaded</param>
private void EmplaceAsset(C3DFileData geometryData, GameObject _object)
{
////Find the asset in de Model by it's id.
float position_x = (float)geometryData.position_x;
float position_y = (float)geometryData.position_y;
float position_z = (float)geometryData.position_z;
float rotation_w = (float)geometryData.rotation_w;
float rotation_x = (float)geometryData.rotation_x;
float rotation_y = (float)geometryData.rotation_y;
float rotation_z = (float)geometryData.rotation_z;
//Emplace the object
_object.transform.position = new Vector3(position_x, position_y, position_z); //Move the object
_object.transform.rotation = new Quaternion(rotation_x, rotation_y, rotation_z, rotation_w); //Rotate the object
// Aplly scale
Vector3 newScale = new Vector3((float)geometryData.scale_x, (float)geometryData.scale_y, (float)geometryData.scale_z);
if (newScale != Vector3.zero)
{
_object.transform.localScale = newScale;
}
}
/// <summary>Go through all the objects loaded and add to them: scripts, colliders, ID.</summary>
/// <param name="loadedRoot">Gameobject parent of all the object loaded</param>
public void AddComponents_and_Others(GameObject loadedRoot)
{
GameObject productRoot = hom3r.quickLinks._3DModelRoot;
if (loadedRoot.transform.parent != productRoot.transform)
{
loadedRoot.transform.parent = productRoot.transform; //Establish this object as child of the product model main object
}
AddComponents_and_Others_Recursively(loadedRoot, productRoot); //In a recursive way we go through all the objects loaded and add to them: scripts and colliders.
}
/// <summary>Recursively runs through the 3D model and add componets to the objects.</summary>
/// <param name="obj">Gameobject parent</param>
void AddComponents_and_Others_Recursively(GameObject obj, GameObject productRoot)
{
//We add components if the object is an area, and its happen if it has a mesh.
if (obj.GetComponent<Renderer>())
{
if ((obj.GetComponent<Renderer>() != null))
{
obj.AddComponent<ObjectStateManager>(); //Add Scripts
obj.AddComponent<MeshCollider>(); //Add collider to be able to do raycasting and Convert the meshcollider to convex mesh
obj.layer = productRoot.layer; //Add layer to the objects
//obj.GetComponent<ObjectState_Script>().InitializeAreaID(); //Initilize the var
this.GetComponent<ModelManager>().SetAreaID_And_GameObject(obj.name, obj); //Add its ID to the object and Add a pointer to its dictionary
}
}
//Call again to itself with one child
for (int i = obj.transform.childCount - 1; i >= 0; i--)
{
GameObject newChild = obj.transform.GetChild(i).gameObject;
AddComponents_and_Others_Recursively(newChild, productRoot);
}
}
//////////////////
// Files Manager
/////////////////
/// <summary>Get the resource file url</summary>
/// <param name="fileName"></param>
/// <returns></returns>
public string GetResourceFileURL(string fileName)
{
int index = resourceFilesList.FindIndex(r=> r.fileName == fileName);
//If the file URL has been received from the interface we return it,
if (index != -1) { return resourceFilesList[index].fileUrl; }
else { return null; }
}
/// <summary>
/// Check if it is a valid 3D file
/// </summary>
/// <param name="fileType"></param>
/// <returns></returns>
public bool isValid3DFile(string fileType)
{
bool control = false;
switch (fileType)
{
case "assetbundle":
control = true;
break;
case "obj":
control = true;
break;
default:
control = false;
break;
}
return control;
}
/////////////////
// UI Messages
////////////////
void SendCleanMessageToUI()
{
SendMessageToUI("", 0.0f);
}
void SendMessageToUI(string _message)
{
SendMessageToUI(_message, 0.0f);
}
void SendMessageToUI(string _message, float _time)
{
hom3r.coreLink.EmitEvent(new CCoreEvent(TCoreEvent._3DFileManager_ShowMessage, _message, _time));
Debug.Log(_message);
}
}