TemplateModel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Linq;
  8. using System.Reflection;
  9. using TriLibCore;
  10. using TriLibCore.Extensions;
  11. using TriLibCore.Mappers;
  12. using TriLibCore.Samples;
  13. using TriLibCore.Utils;
  14. using UnityEngine;
  15. using UnityEngine.Rendering;
  16. public class TemplateModel : BaseTemPlate
  17. {
  18. private GameObject m_ModelObj = null;
  19. protected override void OnEnable()
  20. {
  21. base.OnEnable();
  22. if (GameManager.Instance.IsRuning && m_ModelObj == null)
  23. {
  24. Debug.Log("ExtractZipFile===>1");
  25. if (IsAB())
  26. LoadABModel();
  27. else
  28. TriLibModel();
  29. }
  30. else
  31. {
  32. if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
  33. {
  34. Debug.Log("Animation===>1" + m_ModelObj.name);
  35. Animation _animation = m_ModelObj.GetComponent<Animation>();
  36. List<AnimationClip> _animations = _animation.GetAllAnimationClips();
  37. m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
  38. }
  39. }
  40. if (this.gameObject.GetComponent<AudioSource>())
  41. this.gameObject.GetComponent<AudioSource>().Play();
  42. }
  43. private IEnumerator LoadModel()
  44. {
  45. yield return new WaitForSeconds(0.01f);
  46. if (GameManager.Instance.IsRuning && m_ModelObj == null)
  47. {
  48. //Debug.Log(Data.name);
  49. Debug.Log("DGJ ==>> " + Data.localLoadPath);
  50. var ab = AssetBundle.LoadFromFileAsync(Data.localLoadPath);
  51. yield return ab;
  52. if (ab == null)
  53. {
  54. Debug.Log("Failed to load AssetBundle!");
  55. }
  56. else
  57. {
  58. var allab = ab.assetBundle.LoadAllAssetsAsync<GameObject>();
  59. yield return allab;
  60. var prefab = ab.assetBundle.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
  61. yield return prefab;
  62. switch (prefab.asset.name)
  63. {
  64. case "GHZ_OOBE_230721":
  65. var objload = Resources.LoadAsync<GameObject>("GongYe");
  66. yield return objload;
  67. GameObject obj = null;
  68. if (objload.asset is GameObject)
  69. {
  70. obj = objload.asset as GameObject;
  71. }
  72. if (obj == null)
  73. {
  74. Debug.LogError(" GHZ_OOBE_230721 物体加载失败,请查看是否导入相关Packages ");
  75. obj = new GameObject();
  76. }
  77. m_ModelObj = GameObject.Instantiate(obj, transform);
  78. m_ModelObj.name = "GongYe";
  79. if (GameObject.Find("AudioManager") == null)
  80. {
  81. GameObject audio = Resources.Load<GameObject>("AudioManager");
  82. if (audio == null)
  83. {
  84. Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失");
  85. audio = new GameObject();
  86. }
  87. GameObject Audio = GameObject.Instantiate(audio, transform);
  88. Audio.name = "AudioManager";
  89. }
  90. break;
  91. case "GHZ_XXXXXX_230721":
  92. break;
  93. default:
  94. m_ModelObj = Instantiate(prefab.asset as GameObject, transform);
  95. break;
  96. }
  97. //if (ab.LoadAllAssets<GameObject>()[0].name == "MRVideo")
  98. //m_ModelObj.AddComponent<MovieScreen>();
  99. gameObject.AddComponent<ManipulationHandler>();
  100. gameObject.AddComponent<BoundingBox>();
  101. gameObject.AddComponent<Patch_ModelController>();
  102. yield return new WaitForSeconds(0.3f);
  103. ab.assetBundle.Unload(false);
  104. HideCollider();
  105. }
  106. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  107. //if (obj != null)
  108. //{
  109. // m_ModelObj = Instantiate(obj, transform);
  110. // m_ModelObj.transform.localPosition = Vector3.zero;
  111. // m_ModelObj.gameObject.SetActive(true);
  112. //}
  113. }
  114. }
  115. public void LoadModel(AssetBundle ab)
  116. {
  117. GameManager.Instance.StartCoroutine(LoadABModel(ab));
  118. //LoadABModel(ab);
  119. }
  120. private IEnumerator LoadABModel(AssetBundle ab)
  121. {
  122. if (ab == null)
  123. {
  124. Debug.Log("Failed to load AssetBundle!");
  125. }
  126. else
  127. {
  128. var allab = ab.LoadAllAssetsAsync<GameObject>();
  129. yield return allab;
  130. var prefab = ab.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
  131. switch (prefab.asset.name)
  132. {
  133. case "GHZ_OOBE_230721":
  134. var objload = Resources.LoadAsync<GameObject>("GongYe");
  135. yield return objload;
  136. GameObject obj = null;
  137. if (objload.asset is GameObject)
  138. {
  139. obj = objload.asset as GameObject;
  140. }
  141. m_ModelObj = GameObject.Instantiate(obj, transform);
  142. m_ModelObj.name = "GongYe";
  143. if (GameObject.Find("AudioManager") == null)
  144. {
  145. GameObject audio = Resources.Load<GameObject>("AudioManager");
  146. if (audio == null)
  147. {
  148. Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失");
  149. audio = new GameObject();
  150. }
  151. GameObject Audio = GameObject.Instantiate(audio, null);
  152. Audio.name = "AudioManager";
  153. }
  154. break;
  155. case "GHZ_XXXXXX_230721":
  156. break;
  157. default:
  158. m_ModelObj = Instantiate(prefab.asset as GameObject, transform);
  159. break;
  160. }
  161. //if (ab.LoadAllAssets<GameObject>()[0].name == "MRVideo")
  162. //m_ModelObj.AddComponent<MovieScreen>();
  163. gameObject.AddComponent<ManipulationHandler>();
  164. gameObject.AddComponent<BoundingBox>();
  165. // yield return new WaitForSeconds(0.3f);
  166. ab.Unload(false);
  167. HideCollider();
  168. ModelItem.isLoad = false;
  169. }
  170. }
  171. protected override void OnAwake()
  172. {
  173. base.OnAwake();
  174. }
  175. public override void SetData(MaterialObjValue value, long updateTime)
  176. {
  177. base.SetData(value, updateTime);
  178. //if (gameObject.GetComponent<Collider>() != null)
  179. //{
  180. // Destroy(gameObject.GetComponent<Collider>());
  181. //}
  182. //下载列表中加入数据
  183. if (!GameManager.Instance.IsRuning || Data == null)
  184. {
  185. // DownloadManager.Instance.AddDownloadData(Data);
  186. DownLoadMaterial data = new DownLoadMaterial();
  187. this.Data = data;
  188. data.name = value.name;
  189. data.downLoadPath = value.DownloadPath;
  190. data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  191. data.updataTime = updateTime;//GameManager.Instance.m_SceneValue.updateTime;
  192. data.type = "3";
  193. MsgHandler.AddListener(value.DownloadPath, HandleMsg);
  194. DownloadResManager.Instance.DownLoad(data);
  195. Debug.Log(" SetData " + updateTime);
  196. }
  197. else
  198. {
  199. if (IsAB())
  200. LoadABModel();
  201. else
  202. TriLibModel();
  203. //AssetBundle ab = AssetBundle.LoadFromFile(Data.localLoadPath);
  204. //if (ab == null)
  205. //{
  206. // Debug.Log("Failed to load AssetBundle!");
  207. // return;
  208. //}
  209. //var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
  210. //m_ModelObj = Instantiate(prefab, transform);
  211. //ab.Unload(false);
  212. //gameObject.AddComponent<ManipulationHandler>();
  213. //gameObject.AddComponent<BoundingBox>();
  214. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  215. //if (obj != null)
  216. //{
  217. // m_ModelObj = Instantiate(obj, transform);
  218. // m_ModelObj.transform.localPosition = Vector3.zero;
  219. // m_ModelObj.gameObject.SetActive(true);
  220. //}
  221. }
  222. }
  223. private void HandleMsg(Msg msg)
  224. {
  225. Debug.Log("DGJ TemplateModel ");
  226. if (msg.Value != null)
  227. {
  228. if (IsAB())
  229. LoadABModel();
  230. else
  231. TriLibModel();
  232. }
  233. else
  234. {
  235. InstantiateCommand Command = new InstantiateCommand(
  236. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  237. InstantiateSystem.Instance.BlueObject.NetErrorText);
  238. CommandSystem.Instance.Send(Command);
  239. Debug.LogError(" 模型下载失败 ");
  240. }
  241. }
  242. private bool IsAB()
  243. {
  244. bool finish = false;
  245. string fileName = Path.GetFileName(Data.localLoadPath);
  246. Debug.Log("HJJ fileName===>" + fileName);
  247. if (fileName.Contains(".ghb") || !fileName.Contains("."))
  248. {
  249. finish = true;
  250. }
  251. return finish;
  252. }
  253. private void LoadABModel()
  254. {
  255. GameManager.Instance.StartCoroutine(LoadABModelNew());
  256. /**/
  257. }
  258. public static Dictionary<string, GameObject> loadPathstatic = new Dictionary<string, GameObject>();
  259. public static Dictionary<string, bool> loadPathstaticbool = new Dictionary<string, bool>();
  260. public static Dictionary<string, string> loadPathstaticstr = new Dictionary<string, string>();
  261. private IEnumerator LoadABModelNew()
  262. {
  263. /*
  264. AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync(Data.localLoadPath);
  265. yield return ab;
  266. if (ab.assetBundle == null)
  267. {
  268. Debug.Log("Failed to load AssetBundle!");
  269. yield return null;
  270. }
  271. else
  272. {
  273. AssetBundleRequest golist = ab.assetBundle.LoadAllAssetsAsync<GameObject>();
  274. yield return golist;
  275. AssetBundleRequest assetrequest = ab.assetBundle.LoadAssetAsync<GameObject>(golist.allAssets[0].name);
  276. yield return assetrequest;
  277. GameObject prefab = (GameObject)assetrequest.asset;
  278. m_ModelObj = Instantiate(prefab, transform);
  279. ab.assetBundle.Unload(false);
  280. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  281. manipulation.enabled = false;
  282. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  283. boundingBox.enabled = false;
  284. }*/
  285. Debug.Log("ghb加载===》" + Data.localLoadPath);
  286. if (loadPathstaticbool.ContainsKey(Data.localLoadPath))
  287. {
  288. bool isload = false;
  289. while (!isload)
  290. {
  291. var allab = AssetBundle.GetAllLoadedAssetBundles();
  292. yield return allab;
  293. foreach (var a in allab)
  294. {
  295. if (loadPathstatic.ContainsKey(a.name))
  296. {
  297. GameObject prefab = (GameObject)loadPathstatic[a.name];
  298. m_ModelObj = Instantiate(prefab, transform);
  299. m_ModelObj.SetActive(true);
  300. //Destroy(prefab);
  301. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  302. manipulation.enabled = false;
  303. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  304. boundingBox.enabled = false;
  305. isload = true;
  306. }
  307. }
  308. yield return null;
  309. }
  310. yield return null;
  311. }
  312. else
  313. {
  314. loadPathstaticbool.Add(Data.localLoadPath,true);
  315. AssetBundleCreateRequest ab=null;
  316. ab = AssetBundle.LoadFromFileAsync(Data.localLoadPath);
  317. yield return ab;
  318. if (ab.assetBundle == null)
  319. {
  320. bool isload = false;
  321. while (!isload)
  322. {
  323. var allab = AssetBundle.GetAllLoadedAssetBundles();
  324. yield return allab;
  325. foreach (var a in allab)
  326. {
  327. if (loadPathstatic.ContainsKey(a.name))
  328. {
  329. GameObject prefab = (GameObject)loadPathstatic[a.name];
  330. m_ModelObj = Instantiate(prefab, transform);
  331. m_ModelObj.SetActive(true);
  332. //Destroy(prefab);
  333. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  334. manipulation.enabled = false;
  335. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  336. boundingBox.enabled = false;
  337. isload = true;
  338. }
  339. }
  340. yield return null;
  341. }
  342. yield return null;
  343. }
  344. else
  345. {
  346. loadPathstaticstr.Add(Data.localLoadPath, ab.assetBundle.name);
  347. AssetBundleRequest golist = ab.assetBundle.LoadAllAssetsAsync<GameObject>();
  348. yield return golist;
  349. Debug.Log("ghb加载==1=》" + golist.allAssets[0].name);
  350. AssetBundleRequest assetrequest = ab.assetBundle.LoadAssetAsync<GameObject>(golist.allAssets[0].name);
  351. yield return assetrequest;
  352. Debug.Log("ghb加载===2》" + (assetrequest.asset.name));
  353. loadPathstatic.Add(ab.assetBundle.name, (GameObject)assetrequest.asset);
  354. GameObject prefab = (GameObject)assetrequest.asset;
  355. m_ModelObj = Instantiate(prefab, transform);
  356. GameObject prefabasset = (GameObject)assetrequest.asset;
  357. prefabasset.SetActive(false);
  358. loadPathstatic.Add(ab.assetBundle.name, prefabasset);
  359. // ab.assetBundle.Unload(true);
  360. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  361. manipulation.enabled = false;
  362. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  363. boundingBox.enabled = false;
  364. }
  365. }
  366. }
  367. /// <summary>
  368. /// 解压
  369. /// </summary>
  370. /// <param name="zipFilePath">压缩文件路径</param>
  371. /// <param name="extractPath">解压路径</param>
  372. public bool ExtractZipFile(string zipFilePath)
  373. {
  374. if(!zipFilePath.Contains(".zip"))
  375. {
  376. return false;
  377. }
  378. Debug.Log("ExtractZipFile===>1");
  379. using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
  380. {
  381. foreach (ZipArchiveEntry entry in archive.Entries)
  382. {
  383. Debug.Log("ExtractZipFile==FullName=>2" + entry.FullName);
  384. if (entry.FullName.Contains("GHZPointCloud"))
  385. {
  386. return true;
  387. }
  388. }
  389. }
  390. return false;
  391. }
  392. public static float[] bytesToFloat(byte[] byteArray)//byte[]数组转化为AudioClip可读取的float[]类型
  393. {
  394. float[] sounddata = new float[byteArray.Length / 2];
  395. for (int i = 0; i < sounddata.Length; i++)
  396. {
  397. sounddata[i] = bytesToFloat(byteArray[i * 2], byteArray[i * 2 + 1]);
  398. }
  399. return sounddata;
  400. }
  401. static float bytesToFloat(byte firstByte, byte secondByte)
  402. {
  403. // convert two bytes to one short (little endian)
  404. //小端和大端顺序要调整
  405. short s;
  406. if (BitConverter.IsLittleEndian)
  407. s = (short)((secondByte << 8) | firstByte);
  408. else
  409. s = (short)((firstByte << 8) | secondByte);
  410. // convert to range from -1 to (just below) 1
  411. return s / 32768.0F;
  412. }
  413. public byte[] readbytes(Stream stream)
  414. {
  415. using (Stream fs = stream)
  416. {
  417. byte[] bytes = new byte[fs.Length];
  418. fs.Read(bytes, 0, bytes.Length);
  419. fs.Close();
  420. return bytes;
  421. }
  422. }
  423. public void TriLibModel()
  424. {
  425. if(m_ModelObj)
  426. {
  427. return;
  428. }
  429. Debug.Log("模型加载完成TriLibModel" + Data.downLoadPath);
  430. if (false)//
  431. {
  432. }
  433. else
  434. {
  435. AssetLoaderOptions AssetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  436. if (ExtractZipFile(Data.localLoadPath))
  437. {
  438. AssetLoaderOptions.LoadPointClouds = true;
  439. }
  440. else
  441. {
  442. AssetLoaderOptions.LoadPointClouds = false;
  443. }
  444. AssetLoaderOptions.TextureMappers = new TextureMapper[] { ScriptableObject.CreateInstance<FilePickerTextureMapper>() };
  445. if (!(AssetLoaderOptions.ExternalDataMapper is FilePickerExternalDataMapper))
  446. {
  447. AssetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance<FilePickerExternalDataMapper>();
  448. }
  449. if (AssetLoaderOptions.LoadPointClouds)
  450. {
  451. Debug.Log("模型加载完成TriLibModel" + AssetLoaderOptions.LoadPointClouds);
  452. AssetLoader.LoadModelFromFile(Data.localLoadPath, (AssetLoaderContext ac) => {
  453. Debug.Log("模型加载完成");
  454. }, (AssetLoaderContext ac) => {
  455. Debug.Log("载材质加完成");
  456. HandlePointClouds(ac);
  457. // ac.RootGameObject.transform.position = Camera.main.transform.forward;
  458. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  459. manipulation.enabled = false;
  460. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  461. boundingBox.enabled = false;
  462. ac.RootGameObject.SetActive(false);
  463. if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
  464. {
  465. Debug.Log("Animation===>1" + m_ModelObj.name);
  466. Animation _animation = m_ModelObj.GetComponent<Animation>();
  467. List<AnimationClip> _animations = _animation.GetAllAnimationClips();
  468. m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
  469. }
  470. if (this.gameObject.GetComponent<BoxCollider>())
  471. {
  472. this.gameObject.GetComponent<BoxCollider>().size = Vector3.one * 0.4f;
  473. }
  474. }, (AssetLoaderContext ac, float f) => {
  475. Debug.Log("加载中==》" + f);
  476. }, (IContextualizedError error) => {
  477. Debug.Log("加载失败" + error);
  478. }, null, AssetLoaderOptions);
  479. }
  480. else
  481. {
  482. try
  483. {
  484. TriLibLocalLoad.Load(Data.localLoadPath, (AssetLoaderContext ac) => {
  485. }, (AssetLoaderContext ac) => {
  486. Debug.Log("载材质加完成");
  487. Bounds bounds = GetAllBounds.GetRendererBounds(ac.RootGameObject);
  488. GameObject obj = new GameObject("Test");
  489. obj.transform.position = bounds.center;
  490. obj.transform.SetParent(ac.RootGameObject.transform);
  491. float bizhi = 0.4f / bounds.size.x;
  492. if(bizhi==float.NaN || bizhi == float.PositiveInfinity || bizhi == float.NegativeInfinity)
  493. { bizhi = 1; }
  494. ac.RootGameObject.transform.localScale = Vector3.one * bizhi;
  495. ac.RootGameObject.transform.SetParent(this.transform.parent);
  496. Vector3 v3local = (Vector3.zero - obj.transform.position);
  497. ac.RootGameObject.transform.SetParent(this.transform);
  498. ac.RootGameObject.transform.localPosition = v3local;
  499. ac.RootGameObject.transform.localScale = ac.RootGameObject.transform.localScale * this.transform.localScale.x;
  500. ac.RootGameObject.transform.localEulerAngles = Vector3.zero;
  501. ac.RootGameObject.SetActive(true);
  502. // ac.RootGameObject.transform.position = Camera.main.transform.forward;
  503. m_ModelObj = Instantiate(ac.RootGameObject, transform);
  504. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  505. manipulation.enabled = false;
  506. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  507. boundingBox.enabled = false;
  508. ac.RootGameObject.SetActive(false);
  509. if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
  510. {
  511. Debug.Log("Animation===>1" + m_ModelObj.name);
  512. Animation _animation = m_ModelObj.GetComponent<Animation>();
  513. List<AnimationClip> _animations = _animation.GetAllAnimationClips();
  514. m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
  515. }
  516. if (this.gameObject.GetComponent<BoxCollider>())
  517. {
  518. this.gameObject.GetComponent<BoxCollider>().size = Vector3.one * 0.4f;
  519. }
  520. }, (AssetLoaderContext ac, float f) => {
  521. Debug.Log("加载中==》" + f);
  522. }, (IContextualizedError error) => {
  523. Debug.Log("加载失败" + error);
  524. },null, AssetLoaderOptions);
  525. }
  526. catch
  527. {
  528. Debug.Log("加载失败" + Data.localLoadPath);
  529. }
  530. }
  531. }
  532. }
  533. /// <summary>Handles Point Clouds rendering.</summary>
  534. /// <param name="assetLoaderContext">The Asset Loader Context reference. Asset Loader Context contains the Model loading data.</param>
  535. private void HandlePointClouds(AssetLoaderContext assetLoaderContext)
  536. {
  537. Material material = null;
  538. foreach (var gameObject in assetLoaderContext.GameObjects.Values)
  539. {
  540. m_ModelObj = Instantiate(gameObject, transform);
  541. if (m_ModelObj.TryGetComponent<MeshFilter>(out var meshFilter))
  542. {
  543. var points = meshFilter.sharedMesh.vertices;
  544. var colors = meshFilter.sharedMesh.colors32;
  545. if (colors == null || colors.Length != points.Length)
  546. {
  547. colors = new Color32[points.Length];
  548. for (var i = 0; i < colors.Length; i++)
  549. {
  550. colors[i] = Color.white;
  551. }
  552. }
  553. if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D11 && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D12)
  554. {
  555. UnityEngine.Debug.Log("graphicsDeviceTypegraphicsDeviceTypegraphicsDeviceType" + SystemInfo.graphicsDeviceType);
  556. var mesh = new Mesh();
  557. mesh.indexFormat = IndexFormat.UInt32;
  558. mesh.SetVertices(points);
  559. mesh.SetColors(colors);
  560. mesh.SetIndices(
  561. Enumerable.Range(0, points.Length).ToArray(),
  562. MeshTopology.Points, 0
  563. );
  564. mesh.UploadMeshData(!assetLoaderContext.Options.ReadEnabled);
  565. meshFilter.sharedMesh = mesh;
  566. var meshRenderer = m_ModelObj.AddComponent<MeshRenderer>();
  567. var materials = new Material[meshFilter.sharedMesh.subMeshCount];
  568. if (material == null)
  569. {
  570. material = new Material(Shader.Find("Hidden/PointCloud_GL"));
  571. }
  572. for (var i = 0; i < materials.Length; i++)
  573. {
  574. materials[i] = material;
  575. }
  576. meshRenderer.materials = materials;
  577. }
  578. else
  579. {
  580. var pointCloudRenderer = m_ModelObj.AddComponent<PointCloudRenderer>();
  581. var data = ScriptableObject.CreateInstance<PointCloudData>();
  582. data.Initialize(points, colors);
  583. pointCloudRenderer.sourceData = data;
  584. pointCloudRenderer.destroyData = true;
  585. pointCloudRenderer.pointSize = 0.01f;
  586. assetLoaderContext.Allocations.Add(data);
  587. }
  588. }
  589. }
  590. }
  591. public override void HideCollider()
  592. {
  593. base.HideCollider();
  594. }
  595. }