AssetLoader.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEngine;
  6. namespace TriLib
  7. {
  8. public class AssetLoader : IDisposable
  9. {
  10. static AssetLoader()
  11. {
  12. AssetLoader.LoadAllStandardMaterials();
  13. }
  14. public static Texture2D NormalBaseTexture
  15. {
  16. get
  17. {
  18. return AssetLoader._normalBaseTexture;
  19. }
  20. }
  21. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  22. public event MeshCreatedHandle OnMeshCreated;
  23. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  24. public event MaterialCreatedHandle OnMaterialCreated;
  25. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  26. public event TextureLoadHandle OnTextureLoaded;
  27. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  28. public event TexturePreLoadHandle OnTexturePreLoad;
  29. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  30. public event AnimationClipCreatedHandle OnAnimationClipCreated;
  31. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  32. public event ObjectLoadedHandle OnObjectLoaded;
  33. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  34. public event MetadataProcessedHandle OnMetadataProcessed;
  35. public static bool IsExtensionSupported(string extension)
  36. {
  37. return AssimpInterop.ai_IsExtensionSupported(extension);
  38. }
  39. public static string GetSupportedFileExtensions()
  40. {
  41. string result;
  42. AssimpInterop.ai_GetExtensionList(out result);
  43. return result;
  44. }
  45. public void Dispose()
  46. {
  47. this.OnMeshCreated = null;
  48. this.OnMaterialCreated = null;
  49. this.OnAnimationClipCreated = null;
  50. this.OnTextureLoaded = null;
  51. this.OnObjectLoaded = null;
  52. if (this._nodeDataDictionary != null)
  53. {
  54. foreach (NodeData nodeData in this._nodeDataDictionary.Values)
  55. {
  56. nodeData.Dispose();
  57. }
  58. this._nodeDataDictionary = null;
  59. }
  60. if (this._meshData != null)
  61. {
  62. foreach (MeshData meshData2 in this._meshData)
  63. {
  64. meshData2.Dispose();
  65. }
  66. this._meshData = null;
  67. }
  68. if (this._materialData != null)
  69. {
  70. foreach (MaterialData materialData2 in this._materialData)
  71. {
  72. materialData2.Dispose();
  73. }
  74. this._materialData = null;
  75. }
  76. }
  77. private static void LoadAllStandardMaterials()
  78. {
  79. if (!AssetLoader.LoadNotFoundTexture())
  80. {
  81. throw new Exception("Please import 'NotFound' asset from TriLib package 'TriLib\\Resources' to the project.");
  82. }
  83. if (!AssetLoader.LoadNormalBaseTexture())
  84. {
  85. throw new Exception("Please import 'NormalBase.png' asset from TriLib package 'TriLib\\Resources' to the project.");
  86. }
  87. if (!AssetLoader.LoadStandardMaterials())
  88. {
  89. throw new Exception("Please import all material assets from TriLib package 'TriLib\\Resources' to the project.");
  90. }
  91. }
  92. private static bool LoadStandardMaterials()
  93. {
  94. if (AssetLoader._standardBaseMaterial == null)
  95. {
  96. AssetLoader._standardBaseMaterial = (Resources.Load("StandardMaterial") as Material);
  97. }
  98. if (AssetLoader._standardSpecularMaterial == null)
  99. {
  100. AssetLoader._standardSpecularMaterial = (Resources.Load("StandardSpecularMaterial") as Material);
  101. }
  102. if (AssetLoader._standardBaseAlphaMaterial == null)
  103. {
  104. AssetLoader._standardBaseAlphaMaterial = (Resources.Load("StandardBaseAlphaMaterial") as Material);
  105. }
  106. if (AssetLoader._standardSpecularAlphaMaterial == null)
  107. {
  108. AssetLoader._standardSpecularAlphaMaterial = (Resources.Load("StandardSpecularAlphaMaterial") as Material);
  109. }
  110. if (AssetLoader._standardBaseCutoutMaterial == null)
  111. {
  112. AssetLoader._standardBaseCutoutMaterial = (Resources.Load("StandardBaseCutoutMaterial") as Material);
  113. }
  114. if (AssetLoader._standardSpecularCutoutMaterial == null)
  115. {
  116. AssetLoader._standardSpecularCutoutMaterial = (Resources.Load("StandardSpecularCutoutMaterial") as Material);
  117. }
  118. return AssetLoader._standardBaseMaterial != null && AssetLoader._standardSpecularMaterial != null && AssetLoader._standardBaseAlphaMaterial != null && AssetLoader._standardSpecularAlphaMaterial != null && AssetLoader._standardBaseCutoutMaterial != null && AssetLoader._standardSpecularCutoutMaterial != null;
  119. }
  120. private static bool LoadNotFoundTexture()
  121. {
  122. if (AssetLoader._notFoundTexture == null)
  123. {
  124. AssetLoader._notFoundTexture = (Resources.Load("NotFound") as Texture2D);
  125. }
  126. return AssetLoader._notFoundTexture != null;
  127. }
  128. private static bool LoadNormalBaseTexture()
  129. {
  130. if (AssetLoader._normalBaseTexture == null)
  131. {
  132. AssetLoader._normalBaseTexture = (Resources.Load("NormalBase") as Texture2D);
  133. }
  134. return AssetLoader._normalBaseTexture != null;
  135. }
  136. private void LoadContextOptions(GameObject rootGameObject, AssetLoaderOptions options)
  137. {
  138. rootGameObject.transform.rotation = Quaternion.Euler(options.RotationAngles);
  139. rootGameObject.transform.localScale = Vector3.one * options.Scale;
  140. }
  141. public GameObject LoadFromMemory(byte[] fileBytes, string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
  142. {
  143. if (options == null)
  144. {
  145. options = AssetLoaderOptions.CreateInstance();
  146. }
  147. IntPtr intPtr;
  148. try
  149. {
  150. string fileHint = (!File.Exists(filename)) ? filename : Path.GetExtension(filename);
  151. intPtr = AssetLoader.ImportFileFromMemory(fileBytes, fileHint, options);
  152. }
  153. catch (Exception innerException)
  154. {
  155. throw new Exception("Error parsing file.", innerException);
  156. }
  157. if (intPtr == IntPtr.Zero)
  158. {
  159. string arg = AssimpInterop.ai_GetErrorString();
  160. throw new Exception(string.Format("Error loading asset. Assimp returns: [{0}]", arg));
  161. }
  162. GameObject gameObject = null;
  163. try
  164. {
  165. gameObject = this.LoadInternal(filename, intPtr, options, wrapperGameObject);
  166. }
  167. catch
  168. {
  169. if (gameObject != null)
  170. {
  171. UnityEngine.Object.Destroy(gameObject);
  172. }
  173. throw;
  174. }
  175. if (this.OnObjectLoaded != null)
  176. {
  177. this.OnObjectLoaded(gameObject);
  178. }
  179. AssimpInterop.ai_ReleaseImport(intPtr);
  180. return gameObject;
  181. }
  182. public GameObject LoadFromFile(string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
  183. {
  184. if (options == null)
  185. {
  186. options = AssetLoaderOptions.CreateInstance();
  187. }
  188. IntPtr intPtr;
  189. try
  190. {
  191. intPtr = AssetLoader.ImportFile(filename, options);
  192. }
  193. catch (Exception innerException)
  194. {
  195. throw new Exception(string.Format("Error parsing file: {0}", filename), innerException);
  196. }
  197. if (intPtr == IntPtr.Zero)
  198. {
  199. string arg = AssimpInterop.ai_GetErrorString();
  200. throw new Exception(string.Format("Error loading asset. Assimp returns: [{0}]", arg));
  201. }
  202. GameObject gameObject = null;
  203. try
  204. {
  205. gameObject = this.LoadInternal(filename, intPtr, options, wrapperGameObject);
  206. }
  207. catch
  208. {
  209. if (gameObject != null)
  210. {
  211. UnityEngine.Object.Destroy(gameObject);
  212. }
  213. throw;
  214. }
  215. AssimpInterop.ai_ReleaseImport(intPtr);
  216. if (this.OnObjectLoaded != null)
  217. {
  218. this.OnObjectLoaded(gameObject);
  219. }
  220. return gameObject;
  221. }
  222. public bool LoadFromMemoryWithTextures(byte[] data, string assetExtension, ObjectLoadedHandle onAssetLoaded, out string error, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
  223. {
  224. if (assetExtension.ToLowerInvariant() == ".zip")
  225. {
  226. error = "Please enable TRILIB_USE_ZIP to load zip files";
  227. onAssetLoaded(null);
  228. return false;
  229. }
  230. if (onAssetLoaded != null)
  231. {
  232. this.OnObjectLoaded += onAssetLoaded;
  233. }
  234. if (onTexturePreLoad != null)
  235. {
  236. this.OnTexturePreLoad += onTexturePreLoad;
  237. }
  238. else
  239. {
  240. this.OnTexturePreLoad += delegate(IntPtr scene, string path, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode, string basePath, TextureLoadHandle onTextureLoaded, TextureCompression textureCompression, bool isNormalMap)
  241. {
  242. if (scene == IntPtr.Zero || string.IsNullOrEmpty(path))
  243. {
  244. return;
  245. }
  246. string text;
  247. byte[] data2;
  248. bool isRawData;
  249. int num;
  250. int num2;
  251. if (Texture2DUtils.LoadEmbeddedTextureData(scene, path, out text, out data2, out isRawData, out num, out num2))
  252. {
  253. Texture2DUtils.LoadTextureFromMemory(data2, path, material, propertyName, ref checkAlphaChannel, textureWrapMode, onTextureLoaded, textureCompression, isNormalMap, isRawData, 0, 0);
  254. return;
  255. }
  256. };
  257. try
  258. {
  259. this.LoadFromMemory(data, assetExtension, options, wrapperGameObject);
  260. }
  261. catch (Exception ex)
  262. {
  263. error = ex.ToString();
  264. onAssetLoaded(null);
  265. return false;
  266. }
  267. }
  268. error = null;
  269. return false;
  270. }
  271. private static IntPtr BuildPropertyStore(AssetLoaderOptions options)
  272. {
  273. IntPtr intPtr = AssimpInterop.ai_CreatePropertyStore();
  274. foreach (AssetAdvancedConfig assetAdvancedConfig in options.AdvancedConfigs)
  275. {
  276. AssetAdvancedConfigType assetAdvancedConfigType;
  277. string text;
  278. string text2;
  279. string text3;
  280. object obj;
  281. object obj2;
  282. object obj3;
  283. bool flag;
  284. bool flag2;
  285. bool flag3;
  286. AssetAdvancedPropertyMetadata.GetOptionMetadata(assetAdvancedConfig.Key, out assetAdvancedConfigType, out text, out text2, out text3, out obj, out obj2, out obj3, out flag, out flag2, out flag3);
  287. switch (assetAdvancedConfigType)
  288. {
  289. case AssetAdvancedConfigType.Bool:
  290. AssimpInterop.ai_SetImportPropertyInteger(intPtr, assetAdvancedConfig.Key, (!assetAdvancedConfig.BoolValue) ? 0 : 1);
  291. break;
  292. case AssetAdvancedConfigType.Integer:
  293. AssimpInterop.ai_SetImportPropertyInteger(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.IntValue);
  294. break;
  295. case AssetAdvancedConfigType.Float:
  296. AssimpInterop.ai_SetImportPropertyFloat(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.FloatValue);
  297. break;
  298. case AssetAdvancedConfigType.String:
  299. AssimpInterop.ai_SetImportPropertyString(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.StringValue);
  300. break;
  301. case AssetAdvancedConfigType.AiComponent:
  302. AssimpInterop.ai_SetImportPropertyInteger(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.IntValue << 1);
  303. break;
  304. case AssetAdvancedConfigType.AiPrimitiveType:
  305. AssimpInterop.ai_SetImportPropertyInteger(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.IntValue << 1);
  306. break;
  307. case AssetAdvancedConfigType.AiUVTransform:
  308. AssimpInterop.ai_SetImportPropertyInteger(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.IntValue << 1);
  309. break;
  310. case AssetAdvancedConfigType.AiMatrix:
  311. AssimpInterop.ai_SetImportPropertyMatrix(intPtr, assetAdvancedConfig.Key, assetAdvancedConfig.TranslationValue, assetAdvancedConfig.RotationValue, assetAdvancedConfig.ScaleValue);
  312. break;
  313. }
  314. }
  315. return intPtr;
  316. }
  317. private static IntPtr ImportFileFromMemory(byte[] fileBytes, string fileHint, AssetLoaderOptions options)
  318. {
  319. IntPtr result;
  320. if (options != null && options.AdvancedConfigs != null)
  321. {
  322. IntPtr intPtr = AssetLoader.BuildPropertyStore(options);
  323. result = AssimpInterop.ai_ImportFileFromMemoryWithProperties(fileBytes, (uint)options.PostProcessSteps, fileHint, intPtr);
  324. AssimpInterop.ai_CreateReleasePropertyStore(intPtr);
  325. }
  326. else
  327. {
  328. result = AssimpInterop.ai_ImportFileFromMemory(fileBytes, (uint)((!(options == null)) ? options.PostProcessSteps : ((AssimpPostProcessSteps)0)), fileHint);
  329. }
  330. return result;
  331. }
  332. private static IntPtr ImportFile(string filename, AssetLoaderOptions options)
  333. {
  334. IntPtr result;
  335. if (options != null && options.AdvancedConfigs != null)
  336. {
  337. IntPtr intPtr = AssetLoader.BuildPropertyStore(options);
  338. result = AssimpInterop.ai_ImportFileEx(filename, (uint)options.PostProcessSteps, IntPtr.Zero, intPtr);
  339. AssimpInterop.ai_CreateReleasePropertyStore(intPtr);
  340. }
  341. else
  342. {
  343. result = AssimpInterop.ai_ImportFile(filename, (uint)((!(options == null)) ? options.PostProcessSteps : ((AssimpPostProcessSteps)0)));
  344. }
  345. return result;
  346. }
  347. private GameObject LoadInternal(string filename, IntPtr scene, AssetLoaderOptions options, GameObject wrapperGameObject = null)
  348. {
  349. this._nodeDataDictionary = new Dictionary<string, NodeData>();
  350. this._nodeId = 0;
  351. this.LoadMetadata(scene);
  352. AssetLoader.LoadAllStandardMaterials();
  353. if (AssimpInterop.aiScene_HasMaterials(scene) && !options.DontLoadMaterials)
  354. {
  355. this._materialData = new MaterialData[AssimpInterop.aiScene_GetNumMaterials(scene)];
  356. this.BuildMaterials(filename, scene, options);
  357. }
  358. if (AssimpInterop.aiScene_HasMeshes(scene))
  359. {
  360. this._meshData = new MeshData[AssimpInterop.aiScene_GetNumMeshes(scene)];
  361. this.BuildMeshes(scene, options);
  362. }
  363. wrapperGameObject = this.BuildWrapperObject(scene, options, wrapperGameObject);
  364. if (AssimpInterop.aiScene_HasMeshes(scene))
  365. {
  366. this.BuildBones(scene);
  367. }
  368. if (AssimpInterop.aiScene_HasAnimation(scene) && !options.DontLoadAnimations)
  369. {
  370. this.BuildAnimations(wrapperGameObject, scene, options);
  371. }
  372. if (AssimpInterop.aiScene_HasCameras(scene) && !options.DontLoadCameras)
  373. {
  374. AssetLoader.BuildCameras(wrapperGameObject, scene, options);
  375. }
  376. if (AssimpInterop.aiScene_HasLights(scene) && !options.DontLoadLights)
  377. {
  378. AssetLoader.BuildLights(wrapperGameObject, scene, options);
  379. }
  380. this._nodeDataDictionary = null;
  381. this._meshData = null;
  382. this._materialData = null;
  383. if (options.AddAssetUnloader)
  384. {
  385. wrapperGameObject.AddComponent<AssetUnloader>();
  386. }
  387. return wrapperGameObject;
  388. }
  389. private void LoadMetadata(IntPtr scene)
  390. {
  391. if (this.OnMetadataProcessed == null)
  392. {
  393. return;
  394. }
  395. uint num = AssimpInterop.aiScene_GetMetadataCount(scene);
  396. uint num2 = 0u;
  397. while (num2 < num)
  398. {
  399. string metadataKey = AssimpInterop.aiScene_GetMetadataKey(scene, num2);
  400. AssimpMetadataType metadataType = AssimpInterop.aiScene_GetMetadataType(scene, num2);
  401. object metadataValue;
  402. switch (metadataType)
  403. {
  404. case AssimpMetadataType.AI_BOOL:
  405. metadataValue = AssimpInterop.aiScene_GetMetadataBoolValue(scene, num2);
  406. break;
  407. case AssimpMetadataType.AI_INT32:
  408. metadataValue = AssimpInterop.aiScene_GetMetadataInt32Value(scene, num2);
  409. break;
  410. case AssimpMetadataType.AI_UINT64:
  411. metadataValue = AssimpInterop.aiScene_GetMetadataInt64Value(scene, num2);
  412. break;
  413. case AssimpMetadataType.AI_FLOAT:
  414. metadataValue = AssimpInterop.aiScene_GetMetadataFloatValue(scene, num2);
  415. break;
  416. case AssimpMetadataType.AI_DOUBLE:
  417. metadataValue = AssimpInterop.aiScene_GetMetadataDoubleValue(scene, num2);
  418. break;
  419. case AssimpMetadataType.AI_AISTRING:
  420. goto IL_C3;
  421. case AssimpMetadataType.AI_AIVECTOR3D:
  422. metadataValue = AssimpInterop.aiScene_GetMetadataVectorValue(scene, num2);
  423. break;
  424. default:
  425. goto IL_C3;
  426. }
  427. IL_D1:
  428. this.OnMetadataProcessed(metadataType, num2, metadataKey, metadataValue);
  429. num2 += 1u;
  430. continue;
  431. IL_C3:
  432. metadataValue = AssimpInterop.aiScene_GetMetadataStringValue(scene, num2);
  433. goto IL_D1;
  434. }
  435. }
  436. private void BuildMeshes(IntPtr scene, AssetLoaderOptions options)
  437. {
  438. uint num = AssimpInterop.aiScene_GetNumMeshes(scene);
  439. for (uint num2 = 0u; num2 < num; num2 += 1u)
  440. {
  441. IntPtr ptrMesh = AssimpInterop.aiScene_GetMesh(scene, num2);
  442. uint num3 = AssimpInterop.aiMesh_VertexCount(ptrMesh);
  443. Vector3[] array = new Vector3[num3];
  444. Vector3[] array2 = null;
  445. bool flag = AssimpInterop.aiMesh_HasNormals(ptrMesh);
  446. if (flag)
  447. {
  448. array2 = new Vector3[num3];
  449. }
  450. Vector4[] array3 = null;
  451. Vector4[] array4 = null;
  452. bool flag2 = AssimpInterop.aiMesh_HasTangentsAndBitangents(ptrMesh);
  453. if (flag2)
  454. {
  455. array3 = new Vector4[num3];
  456. array4 = new Vector4[num3];
  457. }
  458. Vector2[] array5 = null;
  459. bool flag3 = AssimpInterop.aiMesh_HasTextureCoords(ptrMesh, 0u);
  460. if (flag3)
  461. {
  462. array5 = new Vector2[num3];
  463. }
  464. Vector2[] array6 = null;
  465. bool flag4 = AssimpInterop.aiMesh_HasTextureCoords(ptrMesh, 1u);
  466. if (flag4)
  467. {
  468. array6 = new Vector2[num3];
  469. }
  470. Vector2[] array7 = null;
  471. bool flag5 = AssimpInterop.aiMesh_HasTextureCoords(ptrMesh, 2u);
  472. if (flag5)
  473. {
  474. array7 = new Vector2[num3];
  475. }
  476. Vector2[] array8 = null;
  477. bool flag6 = AssimpInterop.aiMesh_HasTextureCoords(ptrMesh, 3u);
  478. if (flag6)
  479. {
  480. array8 = new Vector2[num3];
  481. }
  482. Color[] array9 = null;
  483. bool flag7 = AssimpInterop.aiMesh_HasVertexColors(ptrMesh, 0u);
  484. if (flag7)
  485. {
  486. array9 = new Color[num3];
  487. }
  488. for (uint num4 = 0u; num4 < num3; num4 += 1u)
  489. {
  490. array[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetVertex(ptrMesh, num4);
  491. if (flag)
  492. {
  493. array2[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetNormal(ptrMesh, num4);
  494. }
  495. if (flag2)
  496. {
  497. array3[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetTangent(ptrMesh, num4);
  498. array4[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetBitangent(ptrMesh, num4);
  499. }
  500. if (flag3)
  501. {
  502. array5[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetTextureCoord(ptrMesh, 0u, num4);
  503. }
  504. if (flag4)
  505. {
  506. array6[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetTextureCoord(ptrMesh, 1u, num4);
  507. }
  508. if (flag5)
  509. {
  510. array7[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetTextureCoord(ptrMesh, 2u, num4);
  511. }
  512. if (flag6)
  513. {
  514. array8[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetTextureCoord(ptrMesh, 3u, num4);
  515. }
  516. if (flag7)
  517. {
  518. array9[(int)((UIntPtr)num4)] = AssimpInterop.aiMesh_GetVertexColor(ptrMesh, 0u, num4);
  519. }
  520. }
  521. string text = AssimpInterop.aiMesh_GetName(ptrMesh);
  522. Mesh mesh = new Mesh
  523. {
  524. name = ((!string.IsNullOrEmpty(text)) ? text : ("Mesh_" + StringUtils.GenerateUniqueName(num2))),
  525. vertices = array
  526. };
  527. if (flag)
  528. {
  529. mesh.normals = array2;
  530. }
  531. if (flag2)
  532. {
  533. mesh.tangents = array3;
  534. }
  535. if (flag3)
  536. {
  537. mesh.uv = array5;
  538. }
  539. if (flag4)
  540. {
  541. mesh.uv2 = array6;
  542. }
  543. if (flag5)
  544. {
  545. mesh.uv3 = array7;
  546. }
  547. if (flag6)
  548. {
  549. mesh.uv4 = array8;
  550. }
  551. if (flag7)
  552. {
  553. mesh.colors = array9;
  554. }
  555. if (AssimpInterop.aiMesh_HasFaces(ptrMesh))
  556. {
  557. uint num5 = AssimpInterop.aiMesh_GetNumFaces(ptrMesh);
  558. int[] array10 = new int[num5 * 3u];
  559. for (uint num6 = 0u; num6 < num5; num6 += 1u)
  560. {
  561. IntPtr ptrFace = AssimpInterop.aiMesh_GetFace(ptrMesh, num6);
  562. uint num7 = AssimpInterop.aiFace_GetNumIndices(ptrFace);
  563. if (num7 > 3u)
  564. {
  565. throw new UnityException("More than three face indices is not supported. Please enable \"Triangulate\" in your \"AssetLoaderOptions\" \"PostProcessSteps\" field");
  566. }
  567. for (uint num8 = 0u; num8 < num7; num8 += 1u)
  568. {
  569. array10[(int)((UIntPtr)(num6 * 3u + num8))] = (int)AssimpInterop.aiFace_GetIndex(ptrFace, num8);
  570. }
  571. }
  572. mesh.SetIndices(array10, MeshTopology.Triangles, 0);
  573. }
  574. MeshData meshData = new MeshData
  575. {
  576. UnityMesh = mesh
  577. };
  578. this._meshData[(int)((UIntPtr)num2)] = meshData;
  579. }
  580. }
  581. private static void BuildLights(GameObject wrapperGameObject, IntPtr scene, AssetLoaderOptions options)
  582. {
  583. }
  584. private static void BuildCameras(GameObject wrapperGameObject, IntPtr scene, AssetLoaderOptions options)
  585. {
  586. for (uint num = 0u; num < AssimpInterop.aiScene_GetNumCameras(scene); num += 1u)
  587. {
  588. IntPtr ptrCamera = AssimpInterop.aiScene_GetCamera(scene, num);
  589. string name = AssimpInterop.aiCamera_GetName(ptrCamera);
  590. Transform transform = wrapperGameObject.transform.FindDeepChild(name, false);
  591. if (!(transform == null))
  592. {
  593. Camera camera = transform.gameObject.AddComponent<Camera>();
  594. camera.aspect = AssimpInterop.aiCamera_GetAspect(ptrCamera);
  595. camera.nearClipPlane = AssimpInterop.aiCamera_GetClipPlaneNear(ptrCamera);
  596. camera.farClipPlane = AssimpInterop.aiCamera_GetClipPlaneFar(ptrCamera);
  597. camera.fieldOfView = AssimpInterop.aiCamera_GetHorizontalFOV(ptrCamera);
  598. camera.transform.localPosition = AssimpInterop.aiCamera_GetPosition(ptrCamera);
  599. camera.transform.LookAt(AssimpInterop.aiCamera_GetLookAt(ptrCamera), AssimpInterop.aiCamera_GetUp(ptrCamera));
  600. }
  601. }
  602. }
  603. private void BuildMaterials(string filename, IntPtr scene, AssetLoaderOptions options)
  604. {
  605. string text = null;
  606. string textureFileNameWithoutExtension = null;
  607. if (filename != null)
  608. {
  609. FileInfo fileInfo = new FileInfo(filename);
  610. text = fileInfo.Directory.FullName;
  611. textureFileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
  612. }
  613. string basePath = string.IsNullOrEmpty(options.TexturesPathOverride) ? text : options.TexturesPathOverride;
  614. List<Material> list = options.MaterialsOverride ?? new List<Material>();
  615. for (uint num = 0u; num < AssimpInterop.aiScene_GetNumMaterials(scene); num += 1u)
  616. {
  617. IntPtr ptrMat = AssimpInterop.aiScene_GetMaterial(scene, num);
  618. bool isOverriden;
  619. Material material;
  620. if ((long)list.Count > (long)((ulong)num))
  621. {
  622. isOverriden = true;
  623. material = list[(int)num];
  624. }
  625. else
  626. {
  627. isOverriden = false;
  628. string name;
  629. if (AssimpInterop.aiMaterial_HasName(ptrMat))
  630. {
  631. if (!AssimpInterop.aiMaterial_GetName(ptrMat, out name))
  632. {
  633. name = "Material_" + StringUtils.GenerateUniqueName(num);
  634. }
  635. }
  636. else
  637. {
  638. name = "Material_" + StringUtils.GenerateUniqueName(num);
  639. }
  640. bool flag = false;
  641. float num2 = 1f;
  642. float num3;
  643. if (AssimpInterop.aiMaterial_HasOpacity(ptrMat) && AssimpInterop.aiMaterial_GetOpacity(ptrMat, out num3))
  644. {
  645. num2 = num3;
  646. }
  647. Texture2D texture2D = null;
  648. bool flag2 = false;
  649. string text2 = string.Empty;
  650. string name2 = string.Empty;
  651. TextureWrapMode textureWrapMode = TextureWrapMode.Clamp;
  652. uint num4 = AssimpInterop.aiMaterial_GetNumTextureDiffuse(ptrMat);
  653. string text3;
  654. uint num5;
  655. uint num6;
  656. float num7;
  657. uint num8;
  658. uint num9;
  659. if (num4 > 0u && AssimpInterop.aiMaterial_GetTextureDiffuse(ptrMat, 0u, out text3, out num5, out num6, out num7, out num8, out num9))
  660. {
  661. TextureWrapMode textureWrapMode2 = (num9 != 1u) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp;
  662. string text4 = StringUtils.GenerateUniqueName(text3);
  663. bool applyAlphaMaterials = options.ApplyAlphaMaterials;
  664. if (this.OnTexturePreLoad == null)
  665. {
  666. texture2D = Texture2DUtils.LoadTextureFromFile(scene, text3, text4, null, null, ref applyAlphaMaterials, textureWrapMode2, basePath, null, options.TextureCompression, textureFileNameWithoutExtension, false);
  667. flag2 = (texture2D != null);
  668. }
  669. else
  670. {
  671. flag2 = true;
  672. }
  673. flag = (options.ApplyAlphaMaterials && applyAlphaMaterials);
  674. text2 = text3;
  675. textureWrapMode = textureWrapMode2;
  676. name2 = text4;
  677. }
  678. bool flag3 = AssimpInterop.aiMaterial_HasSpecular(ptrMat);
  679. uint num10 = AssimpInterop.aiMaterial_GetNumTextureSpecular(ptrMat);
  680. if (!options.DisableAlphaMaterials && (flag || num2 < 1f))
  681. {
  682. if (options.UseCutoutMaterials)
  683. {
  684. material = new Material((!options.UseStandardSpecularMaterial || (!flag3 && num10 <= 0u)) ? AssetLoader._standardBaseCutoutMaterial : AssetLoader._standardSpecularCutoutMaterial);
  685. }
  686. else
  687. {
  688. material = new Material((!options.UseStandardSpecularMaterial || (!flag3 && num10 <= 0u)) ? AssetLoader._standardBaseAlphaMaterial : AssetLoader._standardSpecularAlphaMaterial);
  689. }
  690. }
  691. else
  692. {
  693. material = new Material((!options.UseStandardSpecularMaterial || (!flag3 && num10 <= 0u)) ? AssetLoader._standardBaseMaterial : AssetLoader._standardSpecularMaterial);
  694. }
  695. material.name = name;
  696. if (!flag2)
  697. {
  698. material.SetTexture("_MainTex", null);
  699. }
  700. else if (this.OnTexturePreLoad != null)
  701. {
  702. bool flag4 = false;
  703. this.OnTexturePreLoad(scene, text2, name2, material, "_MainTex", ref flag4, textureWrapMode, basePath, this.OnTextureLoaded, options.TextureCompression, false);
  704. }
  705. else
  706. {
  707. material.SetTexture("_MainTex", texture2D);
  708. if (this.OnTextureLoaded != null)
  709. {
  710. this.OnTextureLoaded(text2, material, "_MainTex", texture2D);
  711. }
  712. }
  713. bool flag5 = false;
  714. Color value;
  715. if (AssimpInterop.aiMaterial_HasDiffuse(ptrMat) && AssimpInterop.aiMaterial_GetDiffuse(ptrMat, out value))
  716. {
  717. value.a = num2;
  718. material.SetColor("_Color", value);
  719. flag5 = true;
  720. }
  721. if (!flag5)
  722. {
  723. material.SetColor("_Color", Color.white);
  724. }
  725. bool flag6 = false;
  726. bool flag7 = AssimpInterop.aiMaterial_HasEmissive(ptrMat);
  727. Color value2;
  728. if (flag7 && AssimpInterop.aiMaterial_GetEmissive(ptrMat, out value2))
  729. {
  730. material.SetColor("_EmissionColor", value2);
  731. flag6 = true;
  732. }
  733. if (!flag6)
  734. {
  735. material.SetColor("_EmissionColor", Color.black);
  736. }
  737. bool flag8 = false;
  738. uint num11 = AssimpInterop.aiMaterial_GetNumTextureEmissive(ptrMat);
  739. string text5;
  740. uint num12;
  741. uint num13;
  742. float num14;
  743. uint num15;
  744. uint num16;
  745. if (num11 > 0u && AssimpInterop.aiMaterial_GetTextureEmissive(ptrMat, 0u, out text5, out num12, out num13, out num14, out num15, out num16))
  746. {
  747. TextureWrapMode textureWrapMode3 = (num16 != 1u) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp;
  748. string name3 = StringUtils.GenerateUniqueName(text5);
  749. bool flag9 = false;
  750. if (this.OnTexturePreLoad != null)
  751. {
  752. this.OnTexturePreLoad(scene, text5, name3, material, "_EmissionMap", ref flag9, textureWrapMode3, basePath, this.OnTextureLoaded, options.TextureCompression, false);
  753. }
  754. else
  755. {
  756. Texture2DUtils.LoadTextureFromFile(scene, text5, name3, material, "_EmissionMap", ref flag9, textureWrapMode3, basePath, this.OnTextureLoaded, options.TextureCompression, textureFileNameWithoutExtension, false);
  757. }
  758. flag8 = true;
  759. }
  760. if (!flag8)
  761. {
  762. material.SetTexture("_EmissionMap", null);
  763. if (!flag6)
  764. {
  765. material.DisableKeyword("_EMISSION");
  766. }
  767. }
  768. bool flag10 = false;
  769. Color value3;
  770. if (flag3 && AssimpInterop.aiMaterial_GetSpecular(ptrMat, out value3))
  771. {
  772. value3.a = num2;
  773. material.SetColor("_SpecColor", value3);
  774. flag10 = true;
  775. }
  776. if (!flag10)
  777. {
  778. material.SetColor("_SpecColor", Color.black);
  779. }
  780. bool flag11 = false;
  781. string text6;
  782. uint num17;
  783. uint num18;
  784. float num19;
  785. uint num20;
  786. uint num21;
  787. if (num10 > 0u && AssimpInterop.aiMaterial_GetTextureSpecular(ptrMat, 0u, out text6, out num17, out num18, out num19, out num20, out num21))
  788. {
  789. TextureWrapMode textureWrapMode4 = (num21 != 1u) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp;
  790. string name4 = StringUtils.GenerateUniqueName(text6);
  791. bool flag12 = false;
  792. if (this.OnTexturePreLoad != null)
  793. {
  794. this.OnTexturePreLoad(scene, text6, name4, material, "_SpecGlossMap", ref flag12, textureWrapMode4, basePath, this.OnTextureLoaded, options.TextureCompression, false);
  795. }
  796. else
  797. {
  798. Texture2DUtils.LoadTextureFromFile(scene, text6, name4, material, "_SpecGlossMap", ref flag12, textureWrapMode4, basePath, this.OnTextureLoaded, options.TextureCompression, textureFileNameWithoutExtension, false);
  799. }
  800. flag11 = true;
  801. }
  802. if (!flag11)
  803. {
  804. material.SetTexture("_SpecGlossMap", null);
  805. material.DisableKeyword("_SPECGLOSSMAP");
  806. }
  807. bool flag13 = false;
  808. uint num22 = AssimpInterop.aiMaterial_GetNumTextureNormals(ptrMat);
  809. string text7;
  810. uint num23;
  811. uint num24;
  812. float num25;
  813. uint num26;
  814. uint num27;
  815. if (num22 > 0u && AssimpInterop.aiMaterial_GetTextureNormals(ptrMat, 0u, out text7, out num23, out num24, out num25, out num26, out num27))
  816. {
  817. TextureWrapMode textureWrapMode5 = (num27 != 1u) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp;
  818. string name5 = StringUtils.GenerateUniqueName(text7);
  819. bool flag14 = false;
  820. if (this.OnTexturePreLoad != null)
  821. {
  822. this.OnTexturePreLoad(scene, text7, name5, material, "_BumpMap", ref flag14, textureWrapMode5, basePath, this.OnTextureLoaded, options.TextureCompression, true);
  823. }
  824. else
  825. {
  826. Texture2DUtils.LoadTextureFromFile(scene, text7, name5, material, "_BumpMap", ref flag14, textureWrapMode5, basePath, this.OnTextureLoaded, options.TextureCompression, textureFileNameWithoutExtension, true);
  827. }
  828. flag13 = true;
  829. }
  830. bool flag15 = false;
  831. uint num28 = AssimpInterop.aiMaterial_GetNumTextureHeight(ptrMat);
  832. string text8;
  833. uint num29;
  834. uint num30;
  835. float num31;
  836. uint num32;
  837. uint num33;
  838. if (num28 > 0u && AssimpInterop.aiMaterial_GetTextureHeight(ptrMat, 0u, out text8, out num29, out num30, out num31, out num32, out num33))
  839. {
  840. TextureWrapMode textureWrapMode6 = (num33 != 1u) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp;
  841. string name6 = StringUtils.GenerateUniqueName(text8);
  842. bool flag16 = false;
  843. if (this.OnTexturePreLoad != null)
  844. {
  845. this.OnTexturePreLoad(scene, text8, name6, material, "_BumpMap", ref flag16, textureWrapMode6, basePath, this.OnTextureLoaded, options.TextureCompression, false);
  846. }
  847. else
  848. {
  849. Texture2DUtils.LoadTextureFromFile(scene, text8, name6, material, "_BumpMap", ref flag16, textureWrapMode6, basePath, this.OnTextureLoaded, options.TextureCompression, textureFileNameWithoutExtension, false);
  850. }
  851. flag15 = true;
  852. }
  853. if (!flag15 && !flag13)
  854. {
  855. material.SetTexture("_BumpMap", null);
  856. material.DisableKeyword("_NORMALMAP");
  857. }
  858. bool flag17 = false;
  859. float num34;
  860. if (AssimpInterop.aiMaterial_HasBumpScaling(ptrMat) && AssimpInterop.aiMaterial_GetBumpScaling(ptrMat, out num34))
  861. {
  862. if (Mathf.Approximately(num34, 0f))
  863. {
  864. num34 = 1f;
  865. }
  866. material.SetFloat("_BumpScale", num34);
  867. flag17 = true;
  868. }
  869. if (!flag17)
  870. {
  871. material.SetFloat("_BumpScale", 1f);
  872. }
  873. bool flag18 = false;
  874. float value4;
  875. if (AssimpInterop.aiMaterial_HasShininess(ptrMat) && AssimpInterop.aiMaterial_GetShininess(ptrMat, out value4))
  876. {
  877. material.SetFloat("_Glossiness", value4);
  878. flag18 = true;
  879. }
  880. if (!flag18)
  881. {
  882. material.SetFloat("_Glossiness", 0.5f);
  883. }
  884. bool flag19 = false;
  885. if (AssimpInterop.aiMaterial_HasShininessStrength(ptrMat))
  886. {
  887. float value5;
  888. if (AssimpInterop.aiMaterial_GetShininessStrength(ptrMat, out value5))
  889. {
  890. material.SetFloat("_GlossMapScale", value5);
  891. flag19 = true;
  892. }
  893. else
  894. {
  895. material.SetFloat("_GlossMapScale", 1f);
  896. }
  897. }
  898. if (!flag19)
  899. {
  900. material.SetFloat("_GlossMapScale", 1f);
  901. }
  902. }
  903. if (!(material == null))
  904. {
  905. MaterialData materialData = new MaterialData
  906. {
  907. UnityMaterial = material
  908. };
  909. this._materialData[(int)((UIntPtr)num)] = materialData;
  910. if (this.OnMaterialCreated != null)
  911. {
  912. this.OnMaterialCreated(num, isOverriden, material);
  913. }
  914. }
  915. }
  916. }
  917. private void BuildBones(IntPtr scene)
  918. {
  919. uint num = AssimpInterop.aiScene_GetNumMeshes(scene);
  920. for (uint num2 = 0u; num2 < num; num2 += 1u)
  921. {
  922. MeshData meshData = this._meshData[(int)((UIntPtr)num2)];
  923. IntPtr ptrMesh = AssimpInterop.aiScene_GetMesh(scene, num2);
  924. Mesh unityMesh = meshData.UnityMesh;
  925. if (AssimpInterop.aiMesh_HasBones(ptrMesh))
  926. {
  927. uint num3 = AssimpInterop.aiMesh_VertexCount(ptrMesh);
  928. uint num4 = AssimpInterop.aiMesh_GetNumBones(ptrMesh);
  929. Matrix4x4[] array = new Matrix4x4[num4];
  930. Transform[] array2 = new Transform[num4];
  931. BoneWeight[] array3 = new BoneWeight[num3];
  932. int[] array4 = new int[num3];
  933. for (uint num5 = 0u; num5 < num4; num5 += 1u)
  934. {
  935. IntPtr ptrBone = AssimpInterop.aiMesh_GetBone(ptrMesh, num5);
  936. string key = AssimpInterop.aiBone_GetName(ptrBone);
  937. if (this._nodeDataDictionary.ContainsKey(key))
  938. {
  939. NodeData nodeData = this._nodeDataDictionary[key];
  940. GameObject gameObject = nodeData.GameObject;
  941. Transform transform = gameObject.transform;
  942. array2[(int)((UIntPtr)num5)] = transform;
  943. Matrix4x4 matrix4x = AssimpInterop.aiBone_GetOffsetMatrix(ptrBone);
  944. array[(int)((UIntPtr)num5)] = matrix4x;
  945. uint num6 = AssimpInterop.aiBone_GetNumWeights(ptrBone);
  946. for (uint num7 = 0u; num7 < num6; num7 += 1u)
  947. {
  948. IntPtr ptrVweight = AssimpInterop.aiBone_GetWeights(ptrBone, num7);
  949. float num8 = AssimpInterop.aiVertexWeight_GetWeight(ptrVweight);
  950. uint num9 = AssimpInterop.aiVertexWeight_GetVertexId(ptrVweight);
  951. int num10 = array4[(int)((UIntPtr)num9)];
  952. int num11 = (int)num5;
  953. if (num10 == 0)
  954. {
  955. BoneWeight boneWeight = new BoneWeight
  956. {
  957. boneIndex0 = num11,
  958. weight0 = num8
  959. };
  960. array3[(int)((UIntPtr)num9)] = boneWeight;
  961. }
  962. else if (num10 == 1)
  963. {
  964. BoneWeight boneWeight = array3[(int)((UIntPtr)num9)];
  965. boneWeight.boneIndex1 = num11;
  966. boneWeight.weight1 = num8;
  967. array3[(int)((UIntPtr)num9)] = boneWeight;
  968. }
  969. else if (num10 == 2)
  970. {
  971. BoneWeight boneWeight = array3[(int)((UIntPtr)num9)];
  972. boneWeight.boneIndex2 = num11;
  973. boneWeight.weight2 = num8;
  974. array3[(int)((UIntPtr)num9)] = boneWeight;
  975. }
  976. else if (num10 == 3)
  977. {
  978. BoneWeight boneWeight = array3[(int)((UIntPtr)num9)];
  979. boneWeight.boneIndex3 = num11;
  980. boneWeight.weight3 = num8;
  981. array3[(int)((UIntPtr)num9)] = boneWeight;
  982. }
  983. else
  984. {
  985. BoneWeight boneWeight = array3[(int)((UIntPtr)num9)];
  986. boneWeight.boneIndex3 = num11;
  987. boneWeight.weight3 = num8;
  988. array3[(int)((UIntPtr)num9)] = boneWeight;
  989. }
  990. array4[(int)((UIntPtr)num9)]++;
  991. }
  992. }
  993. }
  994. SkinnedMeshRenderer skinnedMeshRenderer = meshData.SkinnedMeshRenderer;
  995. skinnedMeshRenderer.bones = array2;
  996. unityMesh.bindposes = array;
  997. unityMesh.boneWeights = array3;
  998. }
  999. if (this.OnMeshCreated != null)
  1000. {
  1001. this.OnMeshCreated(num2, unityMesh);
  1002. }
  1003. }
  1004. }
  1005. private void BuildAnimations(GameObject wrapperGameObject, IntPtr scene, AssetLoaderOptions options)
  1006. {
  1007. uint num = AssimpInterop.aiScene_GetNumAnimations(scene);
  1008. AnimationClip[] array = new AnimationClip[num];
  1009. for (uint num2 = 0u; num2 < num; num2 += 1u)
  1010. {
  1011. IntPtr ptrAnimation = AssimpInterop.aiScene_GetAnimation(scene, num2);
  1012. float num3 = AssimpInterop.aiAnimation_GetTicksPerSecond(ptrAnimation);
  1013. if (num3 <= 0f)
  1014. {
  1015. num3 = 60f;
  1016. }
  1017. string text = AssimpInterop.aiAnimation_GetName(ptrAnimation);
  1018. AnimationClip animationClip = new AnimationClip
  1019. {
  1020. name = ((!string.IsNullOrEmpty(text)) ? text : ("Animation_" + StringUtils.GenerateUniqueName(num2))),
  1021. legacy = true,
  1022. frameRate = num3
  1023. };
  1024. float num4 = AssimpInterop.aiAnimation_GetDuraction(ptrAnimation);
  1025. float animationLength = num4 / num3;
  1026. uint num5 = AssimpInterop.aiAnimation_GetNumChannels(ptrAnimation);
  1027. for (uint num6 = 0u; num6 < num5; num6 += 1u)
  1028. {
  1029. IntPtr ptrNodeAnim = AssimpInterop.aiAnimation_GetAnimationChannel(ptrAnimation, num6);
  1030. string text2 = AssimpInterop.aiNodeAnim_GetNodeName(ptrNodeAnim);
  1031. if (!string.IsNullOrEmpty(text2))
  1032. {
  1033. if (this._nodeDataDictionary.ContainsKey(text2))
  1034. {
  1035. NodeData nodeData = this._nodeDataDictionary[text2];
  1036. uint num7 = AssimpInterop.aiNodeAnim_GetNumRotationKeys(ptrNodeAnim);
  1037. if (num7 > 0u)
  1038. {
  1039. AnimationCurve animationCurve = new AnimationCurve();
  1040. AnimationCurve animationCurve2 = new AnimationCurve();
  1041. AnimationCurve animationCurve3 = new AnimationCurve();
  1042. AnimationCurve animationCurve4 = new AnimationCurve();
  1043. for (uint num8 = 0u; num8 < num7; num8 += 1u)
  1044. {
  1045. IntPtr ptrQuatKey = AssimpInterop.aiNodeAnim_GetRotationKey(ptrNodeAnim, num8);
  1046. float time = AssimpInterop.aiQuatKey_GetTime(ptrQuatKey) / num3;
  1047. Quaternion quaternion = AssimpInterop.aiQuatKey_GetValue(ptrQuatKey);
  1048. animationCurve.AddKey(time, quaternion.x);
  1049. animationCurve2.AddKey(time, quaternion.y);
  1050. animationCurve3.AddKey(time, quaternion.z);
  1051. animationCurve4.AddKey(time, quaternion.w);
  1052. }
  1053. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localRotation.x", AssetLoader.FixCurve(animationLength, animationCurve));
  1054. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localRotation.y", AssetLoader.FixCurve(animationLength, animationCurve2));
  1055. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localRotation.z", AssetLoader.FixCurve(animationLength, animationCurve3));
  1056. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localRotation.w", AssetLoader.FixCurve(animationLength, animationCurve4));
  1057. }
  1058. uint num9 = AssimpInterop.aiNodeAnim_GetNumPositionKeys(ptrNodeAnim);
  1059. if (num9 > 0u)
  1060. {
  1061. AnimationCurve animationCurve5 = new AnimationCurve();
  1062. AnimationCurve animationCurve6 = new AnimationCurve();
  1063. AnimationCurve animationCurve7 = new AnimationCurve();
  1064. for (uint num10 = 0u; num10 < num9; num10 += 1u)
  1065. {
  1066. IntPtr ptrVectorKey = AssimpInterop.aiNodeAnim_GetPositionKey(ptrNodeAnim, num10);
  1067. float time2 = AssimpInterop.aiVectorKey_GetTime(ptrVectorKey) / num3;
  1068. Vector3 vector = AssimpInterop.aiVectorKey_GetValue(ptrVectorKey);
  1069. animationCurve5.AddKey(time2, vector.x);
  1070. animationCurve6.AddKey(time2, vector.y);
  1071. animationCurve7.AddKey(time2, vector.z);
  1072. }
  1073. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localPosition.x", AssetLoader.FixCurve(animationLength, animationCurve5));
  1074. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localPosition.y", AssetLoader.FixCurve(animationLength, animationCurve6));
  1075. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localPosition.z", AssetLoader.FixCurve(animationLength, animationCurve7));
  1076. }
  1077. uint num11 = AssimpInterop.aiNodeAnim_GetNumScalingKeys(ptrNodeAnim);
  1078. if (num11 > 0u)
  1079. {
  1080. AnimationCurve animationCurve8 = new AnimationCurve();
  1081. AnimationCurve animationCurve9 = new AnimationCurve();
  1082. AnimationCurve animationCurve10 = new AnimationCurve();
  1083. for (uint num12 = 0u; num12 < num11; num12 += 1u)
  1084. {
  1085. IntPtr ptrVectorKey2 = AssimpInterop.aiNodeAnim_GetScalingKey(ptrNodeAnim, num12);
  1086. float time3 = AssimpInterop.aiVectorKey_GetTime(ptrVectorKey2) / num3;
  1087. Vector3 vector2 = AssimpInterop.aiVectorKey_GetValue(ptrVectorKey2);
  1088. animationCurve8.AddKey(time3, vector2.x);
  1089. animationCurve9.AddKey(time3, vector2.y);
  1090. animationCurve10.AddKey(time3, vector2.z);
  1091. }
  1092. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localScale.x", AssetLoader.FixCurve(animationLength, animationCurve8));
  1093. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localScale.y", AssetLoader.FixCurve(animationLength, animationCurve9));
  1094. animationClip.SetCurve(nodeData.Path, typeof(Transform), "localScale.z", AssetLoader.FixCurve(animationLength, animationCurve10));
  1095. }
  1096. }
  1097. }
  1098. }
  1099. animationClip.EnsureQuaternionContinuity();
  1100. animationClip.wrapMode = options.AnimationWrapMode;
  1101. array[(int)((UIntPtr)num2)] = animationClip;
  1102. if (this.OnAnimationClipCreated != null)
  1103. {
  1104. this.OnAnimationClipCreated(num2, animationClip);
  1105. }
  1106. }
  1107. if (options.UseLegacyAnimations)
  1108. {
  1109. Animation animation = wrapperGameObject.GetComponent<Animation>();
  1110. if (animation == null)
  1111. {
  1112. animation = wrapperGameObject.AddComponent<Animation>();
  1113. }
  1114. AnimationClip clip = null;
  1115. for (int i = 0; i < array.Length; i++)
  1116. {
  1117. AnimationClip animationClip2 = array[i];
  1118. animation.AddClip(animationClip2, animationClip2.name);
  1119. if (i == 0)
  1120. {
  1121. clip = animationClip2;
  1122. }
  1123. }
  1124. animation.clip = clip;
  1125. if (options.AutoPlayAnimations)
  1126. {
  1127. animation.Play();
  1128. }
  1129. }
  1130. else
  1131. {
  1132. Animator animator = wrapperGameObject.GetComponent<Animator>();
  1133. if (animator == null)
  1134. {
  1135. animator = wrapperGameObject.AddComponent<Animator>();
  1136. }
  1137. if (options.AnimatorController != null)
  1138. {
  1139. animator.runtimeAnimatorController = options.AnimatorController;
  1140. }
  1141. if (options.Avatar != null)
  1142. {
  1143. animator.avatar = options.Avatar;
  1144. }
  1145. else
  1146. {
  1147. animator.avatar = AvatarBuilder.BuildGenericAvatar(wrapperGameObject, string.Empty);
  1148. }
  1149. }
  1150. }
  1151. private GameObject BuildWrapperObject(IntPtr scene, AssetLoaderOptions options, GameObject templateObject = null)
  1152. {
  1153. IntPtr intPtr = AssimpInterop.aiScene_GetRootNode(scene);
  1154. NodeData nodeData = new NodeData();
  1155. int id = this._nodeId++;
  1156. nodeData.Node = intPtr;
  1157. nodeData.Id = id;
  1158. string text = this.FixName(AssimpInterop.aiNode_GetName(intPtr), id);
  1159. nodeData.Name = text;
  1160. nodeData.Path = text;
  1161. GameObject gameObject = templateObject ?? new GameObject
  1162. {
  1163. name = string.Format("Wrapper_{0}", text)
  1164. };
  1165. GameObject gameObject2 = this.BuildObject(scene, nodeData, options);
  1166. this.LoadContextOptions(gameObject2, options);
  1167. gameObject2.transform.parent = gameObject.transform;
  1168. return gameObject;
  1169. }
  1170. private GameObject BuildObject(IntPtr scene, NodeData nodeData, AssetLoaderOptions options)
  1171. {
  1172. GameObject gameObject = new GameObject
  1173. {
  1174. name = nodeData.Name
  1175. };
  1176. IntPtr node = nodeData.Node;
  1177. uint num = AssimpInterop.aiNode_GetNumMeshes(node);
  1178. bool flag = AssimpInterop.aiScene_HasMeshes(scene);
  1179. if (num > 0u && flag)
  1180. {
  1181. for (uint num2 = 0u; num2 < num; num2 += 1u)
  1182. {
  1183. uint num3 = AssimpInterop.aiNode_GetMeshIndex(node, num2);
  1184. IntPtr ptrMesh = AssimpInterop.aiScene_GetMesh(scene, num3);
  1185. uint num4 = AssimpInterop.aiMesh_GetMatrialIndex(ptrMesh);
  1186. Material material = null;
  1187. if (this._materialData != null)
  1188. {
  1189. MaterialData materialData = this._materialData[(int)((UIntPtr)num4)];
  1190. if (materialData != null)
  1191. {
  1192. material = materialData.UnityMaterial;
  1193. }
  1194. }
  1195. if (material == null)
  1196. {
  1197. material = AssetLoader._standardBaseMaterial;
  1198. }
  1199. MeshData meshData = this._meshData[(int)((UIntPtr)num3)];
  1200. Mesh unityMesh = meshData.UnityMesh;
  1201. GameObject gameObject2 = new GameObject
  1202. {
  1203. name = string.Format("<{0}:Mesh:{1}>", gameObject.name, num2)
  1204. };
  1205. gameObject2.transform.parent = gameObject.transform;
  1206. MeshFilter meshFilter = gameObject2.AddComponent<MeshFilter>();
  1207. meshFilter.mesh = unityMesh;
  1208. if (AssimpInterop.aiMesh_HasBones(ptrMesh))
  1209. {
  1210. SkinnedMeshRenderer skinnedMeshRenderer = gameObject2.AddComponent<SkinnedMeshRenderer>();
  1211. skinnedMeshRenderer.sharedMesh = unityMesh;
  1212. skinnedMeshRenderer.quality = SkinQuality.Bone4;
  1213. skinnedMeshRenderer.sharedMaterial = material;
  1214. meshData.SkinnedMeshRenderer = skinnedMeshRenderer;
  1215. }
  1216. else
  1217. {
  1218. MeshRenderer meshRenderer = gameObject2.AddComponent<MeshRenderer>();
  1219. meshRenderer.sharedMaterial = material;
  1220. if (options.GenerateMeshColliders)
  1221. {
  1222. MeshCollider meshCollider = gameObject2.AddComponent<MeshCollider>();
  1223. meshCollider.sharedMesh = unityMesh;
  1224. meshCollider.convex = options.ConvexMeshColliders;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. if (nodeData.ParentNodeData != null)
  1230. {
  1231. gameObject.transform.parent = nodeData.ParentNodeData.GameObject.transform;
  1232. }
  1233. gameObject.transform.LoadMatrix(AssimpInterop.aiNode_GetTransformation(node), true);
  1234. nodeData.GameObject = gameObject;
  1235. this._nodeDataDictionary.Add(nodeData.Name, nodeData);
  1236. uint num5 = AssimpInterop.aiNode_GetNumChildren(node);
  1237. if (num5 > 0u)
  1238. {
  1239. for (uint num6 = 0u; num6 < num5; num6 += 1u)
  1240. {
  1241. IntPtr intPtr = AssimpInterop.aiNode_GetChildren(node, num6);
  1242. int id = this._nodeId++;
  1243. NodeData nodeData2 = new NodeData
  1244. {
  1245. ParentNodeData = nodeData,
  1246. Node = intPtr,
  1247. Id = id,
  1248. Name = this.FixName(AssimpInterop.aiNode_GetName(intPtr), id)
  1249. };
  1250. nodeData2.Path = string.Format("{0}/{1}", nodeData.Path, nodeData2.Name);
  1251. this.BuildObject(scene, nodeData2, options);
  1252. }
  1253. }
  1254. return gameObject;
  1255. }
  1256. private string FixName(string name, int id)
  1257. {
  1258. return (!string.IsNullOrEmpty(name) && !this._nodeDataDictionary.ContainsKey(name)) ? name : StringUtils.GenerateUniqueName(id);
  1259. }
  1260. private static AnimationCurve FixCurve(float animationLength, AnimationCurve curve)
  1261. {
  1262. if (Mathf.Approximately(animationLength, 0f))
  1263. {
  1264. animationLength = 1f;
  1265. }
  1266. if (curve.keys.Length == 1)
  1267. {
  1268. curve.AddKey(new Keyframe(animationLength, curve.keys[0].value));
  1269. }
  1270. return curve;
  1271. }
  1272. private MaterialData[] _materialData;
  1273. private MeshData[] _meshData;
  1274. private Dictionary<string, NodeData> _nodeDataDictionary;
  1275. private int _nodeId;
  1276. private static Material _standardBaseMaterial;
  1277. private static Material _standardSpecularMaterial;
  1278. private static Material _standardBaseAlphaMaterial;
  1279. private static Material _standardSpecularAlphaMaterial;
  1280. private static Material _standardBaseCutoutMaterial;
  1281. private static Material _standardSpecularCutoutMaterial;
  1282. private static Texture2D _notFoundTexture;
  1283. private static Texture2D _normalBaseTexture;
  1284. }
  1285. }