AssetLoader.cs 45 KB

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