Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.1 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Spawns a render model for the controller from SteamVR
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class SpawnRenderModel : MonoBehaviour
  13. {
  14. public Material[] materials;
  15. private SteamVR_RenderModel[] renderModels;
  16. private Hand hand;
  17. private List<MeshRenderer> renderers = new List<MeshRenderer>();
  18. private static List<SpawnRenderModel> spawnRenderModels = new List<SpawnRenderModel>();
  19. private static int lastFrameUpdated;
  20. private static int spawnRenderModelUpdateIndex;
  21. SteamVR_Events.Action renderModelLoadedAction;
  22. //-------------------------------------------------
  23. void Awake()
  24. {
  25. renderModels = new SteamVR_RenderModel[materials.Length];
  26. renderModelLoadedAction = SteamVR_Events.RenderModelLoadedAction( OnRenderModelLoaded );
  27. }
  28. //-------------------------------------------------
  29. void OnEnable()
  30. {
  31. ShowController();
  32. renderModelLoadedAction.enabled = true;
  33. spawnRenderModels.Add( this );
  34. }
  35. //-------------------------------------------------
  36. void OnDisable()
  37. {
  38. HideController();
  39. renderModelLoadedAction.enabled = false;
  40. spawnRenderModels.Remove( this );
  41. }
  42. //-------------------------------------------------
  43. private void OnAttachedToHand( Hand hand )
  44. {
  45. this.hand = hand;
  46. ShowController();
  47. }
  48. //-------------------------------------------------
  49. private void OnDetachedFromHand( Hand hand )
  50. {
  51. this.hand = null;
  52. HideController();
  53. }
  54. //-------------------------------------------------
  55. void Update()
  56. {
  57. // Only update one per frame
  58. if ( lastFrameUpdated == Time.renderedFrameCount )
  59. {
  60. return;
  61. }
  62. lastFrameUpdated = Time.renderedFrameCount;
  63. // SpawnRenderModel overflow
  64. if ( spawnRenderModelUpdateIndex >= spawnRenderModels.Count )
  65. {
  66. spawnRenderModelUpdateIndex = 0;
  67. }
  68. // Perform update
  69. if ( spawnRenderModelUpdateIndex < spawnRenderModels.Count )
  70. {
  71. SteamVR_RenderModel renderModel = spawnRenderModels[spawnRenderModelUpdateIndex].renderModels[0];
  72. if ( renderModel != null )
  73. {
  74. renderModel.UpdateComponents( OpenVR.RenderModels );
  75. }
  76. }
  77. spawnRenderModelUpdateIndex++;
  78. }
  79. //-------------------------------------------------
  80. private void ShowController()
  81. {
  82. if ( hand == null || hand.controller == null )
  83. {
  84. return;
  85. }
  86. for ( int i = 0; i < renderModels.Length; i++ )
  87. {
  88. if ( renderModels[i] == null )
  89. {
  90. renderModels[i] = new GameObject( "SteamVR_RenderModel" ).AddComponent<SteamVR_RenderModel>();
  91. renderModels[i].updateDynamically = false; // Update one per frame (see Update() method)
  92. renderModels[i].transform.parent = transform;
  93. Util.ResetTransform( renderModels[i].transform );
  94. }
  95. renderModels[i].gameObject.SetActive( true );
  96. renderModels[i].SetDeviceIndex( (int)hand.controller.index );
  97. }
  98. }
  99. //-------------------------------------------------
  100. private void HideController()
  101. {
  102. for ( int i = 0; i < renderModels.Length; i++ )
  103. {
  104. if ( renderModels[i] != null )
  105. {
  106. renderModels[i].gameObject.SetActive( false );
  107. }
  108. }
  109. }
  110. //-------------------------------------------------
  111. private void OnRenderModelLoaded( SteamVR_RenderModel renderModel, bool success )
  112. {
  113. for ( int i = 0; i < renderModels.Length; i++ )
  114. {
  115. if ( renderModel == renderModels[i] )
  116. {
  117. if ( materials[i] != null )
  118. {
  119. renderers.Clear();
  120. renderModels[i].GetComponentsInChildren<MeshRenderer>( renderers );
  121. for ( int j = 0; j < renderers.Count; j++ )
  122. {
  123. Texture mainTexture = renderers[j].material.mainTexture;
  124. renderers[j].sharedMaterial = materials[i];
  125. renderers[j].material.mainTexture = mainTexture;
  126. renderers[j].gameObject.layer = gameObject.layer;
  127. renderers[j].tag = gameObject.tag;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }