SteamVR_RenderModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. using System;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Threading;
  6. using UnityEngine;
  7. using Valve.VR;
  8. [ExecuteInEditMode]
  9. public class SteamVR_RenderModel : MonoBehaviour
  10. {
  11. public string renderModelName { get; private set; }
  12. private void OnModelSkinSettingsHaveChanged(params object[] args)
  13. {
  14. if (!string.IsNullOrEmpty(this.renderModelName))
  15. {
  16. this.renderModelName = string.Empty;
  17. this.UpdateModel();
  18. }
  19. }
  20. private void OnHideRenderModels(params object[] args)
  21. {
  22. bool flag = (bool)args[0];
  23. MeshRenderer component = base.GetComponent<MeshRenderer>();
  24. if (component != null)
  25. {
  26. component.enabled = !flag;
  27. }
  28. foreach (MeshRenderer meshRenderer in base.transform.GetComponentsInChildren<MeshRenderer>())
  29. {
  30. meshRenderer.enabled = !flag;
  31. }
  32. }
  33. private void OnDeviceConnected(params object[] args)
  34. {
  35. int num = (int)args[0];
  36. if (num != (int)this.index)
  37. {
  38. return;
  39. }
  40. bool flag = (bool)args[1];
  41. if (flag)
  42. {
  43. this.UpdateModel();
  44. }
  45. }
  46. public void UpdateModel()
  47. {
  48. CVRSystem system = OpenVR.System;
  49. if (system == null)
  50. {
  51. return;
  52. }
  53. ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
  54. uint stringTrackedDeviceProperty = system.GetStringTrackedDeviceProperty((uint)this.index, ETrackedDeviceProperty.Prop_RenderModelName_String, null, 0u, ref etrackedPropertyError);
  55. if (stringTrackedDeviceProperty <= 1u)
  56. {
  57. Debug.LogError("Failed to get render model name for tracked object " + this.index);
  58. return;
  59. }
  60. StringBuilder stringBuilder = new StringBuilder((int)stringTrackedDeviceProperty);
  61. system.GetStringTrackedDeviceProperty((uint)this.index, ETrackedDeviceProperty.Prop_RenderModelName_String, stringBuilder, stringTrackedDeviceProperty, ref etrackedPropertyError);
  62. string text = stringBuilder.ToString();
  63. if (this.renderModelName != text)
  64. {
  65. this.renderModelName = text;
  66. base.StartCoroutine(this.SetModelAsync(text));
  67. }
  68. }
  69. private IEnumerator SetModelAsync(string renderModelName)
  70. {
  71. if (string.IsNullOrEmpty(renderModelName))
  72. {
  73. yield break;
  74. }
  75. using (SteamVR_RenderModel.RenderModelInterfaceHolder holder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
  76. {
  77. CVRRenderModels renderModels = holder.instance;
  78. if (renderModels == null)
  79. {
  80. yield break;
  81. }
  82. uint count = renderModels.GetComponentCount(renderModelName);
  83. string[] renderModelNames;
  84. if (count > 0u)
  85. {
  86. renderModelNames = new string[count];
  87. int num = 0;
  88. while ((long)num < (long)((ulong)count))
  89. {
  90. uint num2 = renderModels.GetComponentName(renderModelName, (uint)num, null, 0u);
  91. if (num2 != 0u)
  92. {
  93. StringBuilder stringBuilder = new StringBuilder((int)num2);
  94. if (renderModels.GetComponentName(renderModelName, (uint)num, stringBuilder, num2) != 0u)
  95. {
  96. num2 = renderModels.GetComponentRenderModelName(renderModelName, stringBuilder.ToString(), null, 0u);
  97. if (num2 != 0u)
  98. {
  99. StringBuilder stringBuilder2 = new StringBuilder((int)num2);
  100. if (renderModels.GetComponentRenderModelName(renderModelName, stringBuilder.ToString(), stringBuilder2, num2) != 0u)
  101. {
  102. string text = stringBuilder2.ToString();
  103. SteamVR_RenderModel.RenderModel renderModel = SteamVR_RenderModel.models[text] as SteamVR_RenderModel.RenderModel;
  104. if (renderModel == null || renderModel.mesh == null)
  105. {
  106. renderModelNames[num] = text;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. num++;
  113. }
  114. }
  115. else
  116. {
  117. SteamVR_RenderModel.RenderModel renderModel2 = SteamVR_RenderModel.models[renderModelName] as SteamVR_RenderModel.RenderModel;
  118. if (renderModel2 == null || renderModel2.mesh == null)
  119. {
  120. renderModelNames = new string[]
  121. {
  122. renderModelName
  123. };
  124. }
  125. else
  126. {
  127. renderModelNames = new string[0];
  128. }
  129. }
  130. for (;;)
  131. {
  132. bool loading = false;
  133. foreach (string text2 in renderModelNames)
  134. {
  135. if (!string.IsNullOrEmpty(text2))
  136. {
  137. IntPtr zero = IntPtr.Zero;
  138. EVRRenderModelError evrrenderModelError = renderModels.LoadRenderModel_Async(text2, ref zero);
  139. if (evrrenderModelError == EVRRenderModelError.Loading)
  140. {
  141. loading = true;
  142. }
  143. else if (evrrenderModelError == EVRRenderModelError.None)
  144. {
  145. RenderModel_t renderModel_t = (RenderModel_t)Marshal.PtrToStructure(zero, typeof(RenderModel_t));
  146. Material material = SteamVR_RenderModel.materials[renderModel_t.diffuseTextureId] as Material;
  147. if (material == null || material.mainTexture == null)
  148. {
  149. IntPtr zero2 = IntPtr.Zero;
  150. evrrenderModelError = renderModels.LoadTexture_Async(renderModel_t.diffuseTextureId, ref zero2);
  151. if (evrrenderModelError == EVRRenderModelError.Loading)
  152. {
  153. loading = true;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. if (!loading)
  160. {
  161. break;
  162. }
  163. yield return new WaitForSeconds(0.1f);
  164. }
  165. }
  166. bool success = this.SetModel(renderModelName);
  167. SteamVR_Utils.Event.Send("render_model_loaded", new object[]
  168. {
  169. this,
  170. success
  171. });
  172. yield break;
  173. }
  174. private bool SetModel(string renderModelName)
  175. {
  176. this.StripMesh(base.gameObject);
  177. using (SteamVR_RenderModel.RenderModelInterfaceHolder renderModelInterfaceHolder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
  178. {
  179. if (this.createComponents)
  180. {
  181. if (this.LoadComponents(renderModelInterfaceHolder, renderModelName))
  182. {
  183. this.UpdateComponents();
  184. return true;
  185. }
  186. Debug.Log("[" + base.gameObject.name + "] Render model does not support components, falling back to single mesh.");
  187. }
  188. if (!string.IsNullOrEmpty(renderModelName))
  189. {
  190. SteamVR_RenderModel.RenderModel renderModel = SteamVR_RenderModel.models[renderModelName] as SteamVR_RenderModel.RenderModel;
  191. if (renderModel == null || renderModel.mesh == null)
  192. {
  193. CVRRenderModels instance = renderModelInterfaceHolder.instance;
  194. if (instance == null)
  195. {
  196. return false;
  197. }
  198. if (this.verbose)
  199. {
  200. Debug.Log("Loading render model " + renderModelName);
  201. }
  202. renderModel = this.LoadRenderModel(instance, renderModelName, renderModelName);
  203. if (renderModel == null)
  204. {
  205. return false;
  206. }
  207. SteamVR_RenderModel.models[renderModelName] = renderModel;
  208. }
  209. base.gameObject.AddComponent<MeshFilter>().mesh = renderModel.mesh;
  210. base.gameObject.AddComponent<MeshRenderer>().sharedMaterial = renderModel.material;
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. private SteamVR_RenderModel.RenderModel LoadRenderModel(CVRRenderModels renderModels, string renderModelName, string baseName)
  217. {
  218. IntPtr zero = IntPtr.Zero;
  219. EVRRenderModelError evrrenderModelError;
  220. for (;;)
  221. {
  222. evrrenderModelError = renderModels.LoadRenderModel_Async(renderModelName, ref zero);
  223. if (evrrenderModelError != EVRRenderModelError.Loading)
  224. {
  225. break;
  226. }
  227. Thread.Sleep(1);
  228. }
  229. if (evrrenderModelError != EVRRenderModelError.None)
  230. {
  231. Debug.LogError(string.Format("Failed to load render model {0} - {1}", renderModelName, evrrenderModelError.ToString()));
  232. return null;
  233. }
  234. RenderModel_t renderModel_t = (RenderModel_t)Marshal.PtrToStructure(zero, typeof(RenderModel_t));
  235. Vector3[] array = new Vector3[renderModel_t.unVertexCount];
  236. Vector3[] array2 = new Vector3[renderModel_t.unVertexCount];
  237. Vector2[] array3 = new Vector2[renderModel_t.unVertexCount];
  238. Type typeFromHandle = typeof(RenderModel_Vertex_t);
  239. int num = 0;
  240. while ((long)num < (long)((ulong)renderModel_t.unVertexCount))
  241. {
  242. IntPtr ptr = new IntPtr(renderModel_t.rVertexData.ToInt64() + (long)(num * Marshal.SizeOf(typeFromHandle)));
  243. RenderModel_Vertex_t renderModel_Vertex_t = (RenderModel_Vertex_t)Marshal.PtrToStructure(ptr, typeFromHandle);
  244. array[num] = new Vector3(renderModel_Vertex_t.vPosition.v0, renderModel_Vertex_t.vPosition.v1, -renderModel_Vertex_t.vPosition.v2);
  245. array2[num] = new Vector3(renderModel_Vertex_t.vNormal.v0, renderModel_Vertex_t.vNormal.v1, -renderModel_Vertex_t.vNormal.v2);
  246. array3[num] = new Vector2(renderModel_Vertex_t.rfTextureCoord0, renderModel_Vertex_t.rfTextureCoord1);
  247. num++;
  248. }
  249. int num2 = (int)(renderModel_t.unTriangleCount * 3u);
  250. short[] array4 = new short[num2];
  251. Marshal.Copy(renderModel_t.rIndexData, array4, 0, array4.Length);
  252. int[] array5 = new int[num2];
  253. int num3 = 0;
  254. while ((long)num3 < (long)((ulong)renderModel_t.unTriangleCount))
  255. {
  256. array5[num3 * 3] = (int)array4[num3 * 3 + 2];
  257. array5[num3 * 3 + 1] = (int)array4[num3 * 3 + 1];
  258. array5[num3 * 3 + 2] = (int)array4[num3 * 3];
  259. num3++;
  260. }
  261. Mesh mesh = new Mesh();
  262. mesh.vertices = array;
  263. mesh.normals = array2;
  264. mesh.uv = array3;
  265. mesh.triangles = array5;
  266. Material material = SteamVR_RenderModel.materials[renderModel_t.diffuseTextureId] as Material;
  267. if (material == null || material.mainTexture == null)
  268. {
  269. IntPtr zero2 = IntPtr.Zero;
  270. for (;;)
  271. {
  272. evrrenderModelError = renderModels.LoadTexture_Async(renderModel_t.diffuseTextureId, ref zero2);
  273. if (evrrenderModelError != EVRRenderModelError.Loading)
  274. {
  275. break;
  276. }
  277. Thread.Sleep(1);
  278. }
  279. if (evrrenderModelError == EVRRenderModelError.None)
  280. {
  281. RenderModel_TextureMap_t renderModel_TextureMap_t = (RenderModel_TextureMap_t)Marshal.PtrToStructure(zero2, typeof(RenderModel_TextureMap_t));
  282. Texture2D texture2D = new Texture2D((int)renderModel_TextureMap_t.unWidth, (int)renderModel_TextureMap_t.unHeight, TextureFormat.ARGB32, false);
  283. if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
  284. {
  285. byte[] array6 = new byte[(int)(renderModel_TextureMap_t.unWidth * renderModel_TextureMap_t.unHeight * '\u0004')];
  286. Marshal.Copy(renderModel_TextureMap_t.rubTextureMapData, array6, 0, array6.Length);
  287. Color32[] array7 = new Color32[(int)(renderModel_TextureMap_t.unWidth * renderModel_TextureMap_t.unHeight)];
  288. int num4 = 0;
  289. for (int i = 0; i < (int)renderModel_TextureMap_t.unHeight; i++)
  290. {
  291. for (int j = 0; j < (int)renderModel_TextureMap_t.unWidth; j++)
  292. {
  293. byte r = array6[num4++];
  294. byte g = array6[num4++];
  295. byte b = array6[num4++];
  296. byte a = array6[num4++];
  297. array7[i * (int)renderModel_TextureMap_t.unWidth + j] = new Color32(r, g, b, a);
  298. }
  299. }
  300. texture2D.SetPixels32(array7);
  301. texture2D.Apply();
  302. }
  303. else
  304. {
  305. texture2D.Apply();
  306. for (;;)
  307. {
  308. evrrenderModelError = renderModels.LoadIntoTextureD3D11_Async(renderModel_t.diffuseTextureId, texture2D.GetNativeTexturePtr());
  309. if (evrrenderModelError != EVRRenderModelError.Loading)
  310. {
  311. break;
  312. }
  313. Thread.Sleep(1);
  314. }
  315. }
  316. material = new Material((!(this.shader != null)) ? Shader.Find("Standard") : this.shader);
  317. material.mainTexture = texture2D;
  318. SteamVR_RenderModel.materials[renderModel_t.diffuseTextureId] = material;
  319. renderModels.FreeTexture(zero2);
  320. }
  321. else
  322. {
  323. Debug.Log("Failed to load render model texture for render model " + renderModelName);
  324. }
  325. }
  326. base.StartCoroutine(this.FreeRenderModel(zero));
  327. return new SteamVR_RenderModel.RenderModel(mesh, material);
  328. }
  329. private IEnumerator FreeRenderModel(IntPtr pRenderModel)
  330. {
  331. yield return new WaitForSeconds(1f);
  332. using (SteamVR_RenderModel.RenderModelInterfaceHolder renderModelInterfaceHolder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
  333. {
  334. CVRRenderModels instance = renderModelInterfaceHolder.instance;
  335. instance.FreeRenderModel(pRenderModel);
  336. }
  337. yield break;
  338. }
  339. public Transform FindComponent(string componentName)
  340. {
  341. Transform transform = base.transform;
  342. for (int i = 0; i < transform.childCount; i++)
  343. {
  344. Transform child = transform.GetChild(i);
  345. if (child.name == componentName)
  346. {
  347. return child;
  348. }
  349. }
  350. return null;
  351. }
  352. private void StripMesh(GameObject go)
  353. {
  354. MeshRenderer component = go.GetComponent<MeshRenderer>();
  355. if (component != null)
  356. {
  357. UnityEngine.Object.DestroyImmediate(component);
  358. }
  359. MeshFilter component2 = go.GetComponent<MeshFilter>();
  360. if (component2 != null)
  361. {
  362. UnityEngine.Object.DestroyImmediate(component2);
  363. }
  364. }
  365. private bool LoadComponents(SteamVR_RenderModel.RenderModelInterfaceHolder holder, string renderModelName)
  366. {
  367. Transform transform = base.transform;
  368. for (int i = 0; i < transform.childCount; i++)
  369. {
  370. Transform child = transform.GetChild(i);
  371. child.gameObject.SetActive(false);
  372. this.StripMesh(child.gameObject);
  373. }
  374. if (string.IsNullOrEmpty(renderModelName))
  375. {
  376. return true;
  377. }
  378. CVRRenderModels instance = holder.instance;
  379. if (instance == null)
  380. {
  381. return false;
  382. }
  383. uint componentCount = instance.GetComponentCount(renderModelName);
  384. if (componentCount == 0u)
  385. {
  386. return false;
  387. }
  388. int num = 0;
  389. while ((long)num < (long)((ulong)componentCount))
  390. {
  391. uint num2 = instance.GetComponentName(renderModelName, (uint)num, null, 0u);
  392. if (num2 != 0u)
  393. {
  394. StringBuilder stringBuilder = new StringBuilder((int)num2);
  395. if (instance.GetComponentName(renderModelName, (uint)num, stringBuilder, num2) != 0u)
  396. {
  397. transform = this.FindComponent(stringBuilder.ToString());
  398. if (transform != null)
  399. {
  400. transform.gameObject.SetActive(true);
  401. }
  402. else
  403. {
  404. transform = new GameObject(stringBuilder.ToString()).transform;
  405. transform.parent = base.transform;
  406. transform.gameObject.layer = base.gameObject.layer;
  407. Transform transform2 = new GameObject("attach").transform;
  408. transform2.parent = transform;
  409. transform2.localPosition = Vector3.zero;
  410. transform2.localRotation = Quaternion.identity;
  411. transform2.localScale = Vector3.one;
  412. transform2.gameObject.layer = base.gameObject.layer;
  413. }
  414. transform.localPosition = Vector3.zero;
  415. transform.localRotation = Quaternion.identity;
  416. transform.localScale = Vector3.one;
  417. num2 = instance.GetComponentRenderModelName(renderModelName, stringBuilder.ToString(), null, 0u);
  418. if (num2 != 0u)
  419. {
  420. StringBuilder stringBuilder2 = new StringBuilder((int)num2);
  421. if (instance.GetComponentRenderModelName(renderModelName, stringBuilder.ToString(), stringBuilder2, num2) != 0u)
  422. {
  423. SteamVR_RenderModel.RenderModel renderModel = SteamVR_RenderModel.models[stringBuilder2] as SteamVR_RenderModel.RenderModel;
  424. if (renderModel == null || renderModel.mesh == null)
  425. {
  426. if (this.verbose)
  427. {
  428. Debug.Log("Loading render model " + stringBuilder2);
  429. }
  430. renderModel = this.LoadRenderModel(instance, stringBuilder2.ToString(), renderModelName);
  431. if (renderModel == null)
  432. {
  433. goto IL_265;
  434. }
  435. SteamVR_RenderModel.models[stringBuilder2] = renderModel;
  436. }
  437. transform.gameObject.AddComponent<MeshFilter>().mesh = renderModel.mesh;
  438. transform.gameObject.AddComponent<MeshRenderer>().sharedMaterial = renderModel.material;
  439. }
  440. }
  441. }
  442. }
  443. IL_265:
  444. num++;
  445. }
  446. return true;
  447. }
  448. private void OnEnable()
  449. {
  450. if (!string.IsNullOrEmpty(this.modelOverride))
  451. {
  452. Debug.Log("Model override is really only meant to be used in the scene view for lining things up; using it at runtime is discouraged. Use tracked device index instead to ensure the correct model is displayed for all users.");
  453. base.enabled = false;
  454. return;
  455. }
  456. CVRSystem system = OpenVR.System;
  457. if (system != null && system.IsTrackedDeviceConnected((uint)this.index))
  458. {
  459. this.UpdateModel();
  460. }
  461. SteamVR_Utils.Event.Listen("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  462. SteamVR_Utils.Event.Listen("hide_render_models", new SteamVR_Utils.Event.Handler(this.OnHideRenderModels));
  463. SteamVR_Utils.Event.Listen("ModelSkinSettingsHaveChanged", new SteamVR_Utils.Event.Handler(this.OnModelSkinSettingsHaveChanged));
  464. }
  465. private void OnDisable()
  466. {
  467. SteamVR_Utils.Event.Remove("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  468. SteamVR_Utils.Event.Remove("hide_render_models", new SteamVR_Utils.Event.Handler(this.OnHideRenderModels));
  469. SteamVR_Utils.Event.Remove("ModelSkinSettingsHaveChanged", new SteamVR_Utils.Event.Handler(this.OnModelSkinSettingsHaveChanged));
  470. }
  471. private void Update()
  472. {
  473. if (this.updateDynamically)
  474. {
  475. this.UpdateComponents();
  476. }
  477. }
  478. public void UpdateComponents()
  479. {
  480. Transform transform = base.transform;
  481. if (transform.childCount == 0)
  482. {
  483. return;
  484. }
  485. using (SteamVR_RenderModel.RenderModelInterfaceHolder renderModelInterfaceHolder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
  486. {
  487. VRControllerState_t vrcontrollerState_t = (this.index == SteamVR_TrackedObject.EIndex.None) ? default(VRControllerState_t) : SteamVR_Controller.Input((int)this.index).GetState();
  488. for (int i = 0; i < transform.childCount; i++)
  489. {
  490. Transform child = transform.GetChild(i);
  491. CVRRenderModels instance = renderModelInterfaceHolder.instance;
  492. if (instance == null)
  493. {
  494. break;
  495. }
  496. RenderModel_ComponentState_t renderModel_ComponentState_t = default(RenderModel_ComponentState_t);
  497. if (instance.GetComponentState(this.renderModelName, child.name, ref vrcontrollerState_t, ref this.controllerModeState, ref renderModel_ComponentState_t))
  498. {
  499. SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(renderModel_ComponentState_t.mTrackingToComponentRenderModel);
  500. child.localPosition = rigidTransform.pos;
  501. child.localRotation = rigidTransform.rot;
  502. Transform transform2 = child.Find("attach");
  503. if (transform2 != null)
  504. {
  505. SteamVR_Utils.RigidTransform rigidTransform2 = new SteamVR_Utils.RigidTransform(renderModel_ComponentState_t.mTrackingToComponentLocal);
  506. transform2.position = transform.TransformPoint(rigidTransform2.pos);
  507. transform2.rotation = transform.rotation * rigidTransform2.rot;
  508. }
  509. bool flag = (renderModel_ComponentState_t.uProperties & 2u) != 0u;
  510. if (flag != child.gameObject.activeSelf)
  511. {
  512. child.gameObject.SetActive(flag);
  513. }
  514. }
  515. }
  516. }
  517. }
  518. public void SetDeviceIndex(int index)
  519. {
  520. this.index = (SteamVR_TrackedObject.EIndex)index;
  521. this.modelOverride = string.Empty;
  522. if (base.enabled)
  523. {
  524. this.UpdateModel();
  525. }
  526. }
  527. public SteamVR_TrackedObject.EIndex index = SteamVR_TrackedObject.EIndex.None;
  528. public string modelOverride;
  529. public Shader shader;
  530. public bool verbose;
  531. public bool createComponents = true;
  532. public bool updateDynamically = true;
  533. public RenderModel_ControllerMode_State_t controllerModeState;
  534. public const string k_localTransformName = "attach";
  535. public static Hashtable models = new Hashtable();
  536. public static Hashtable materials = new Hashtable();
  537. public class RenderModel
  538. {
  539. public RenderModel(Mesh mesh, Material material)
  540. {
  541. this.mesh = mesh;
  542. this.material = material;
  543. }
  544. public Mesh mesh { get; private set; }
  545. public Material material { get; private set; }
  546. }
  547. public sealed class RenderModelInterfaceHolder : IDisposable
  548. {
  549. public CVRRenderModels instance
  550. {
  551. get
  552. {
  553. if (this._instance == null && !this.failedLoadInterface)
  554. {
  555. if (!SteamVR.active && !SteamVR.usingNativeSupport)
  556. {
  557. EVRInitError evrinitError = EVRInitError.None;
  558. OpenVR.Init(ref evrinitError, EVRApplicationType.VRApplication_Other);
  559. this.needsShutdown = true;
  560. }
  561. this._instance = OpenVR.RenderModels;
  562. if (this._instance == null)
  563. {
  564. Debug.LogError("Failed to load IVRRenderModels interface version IVRRenderModels_005");
  565. this.failedLoadInterface = true;
  566. }
  567. }
  568. return this._instance;
  569. }
  570. }
  571. public void Dispose()
  572. {
  573. if (this.needsShutdown)
  574. {
  575. OpenVR.Shutdown();
  576. }
  577. }
  578. private bool needsShutdown;
  579. private bool failedLoadInterface;
  580. private CVRRenderModels _instance;
  581. }
  582. }