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.

151 lines
4.2 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Uses the see thru renderer while attached to hand
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class SeeThru : MonoBehaviour
  13. {
  14. public Material seeThruMaterial;
  15. private GameObject seeThru;
  16. private Interactable interactable;
  17. private Renderer sourceRenderer;
  18. private Renderer destRenderer;
  19. //-------------------------------------------------
  20. void Awake()
  21. {
  22. interactable = GetComponentInParent<Interactable>();
  23. //
  24. // Create child game object for see thru renderer
  25. //
  26. seeThru = new GameObject( "_see_thru" );
  27. seeThru.transform.parent = transform;
  28. seeThru.transform.localPosition = Vector3.zero;
  29. seeThru.transform.localRotation = Quaternion.identity;
  30. seeThru.transform.localScale = Vector3.one;
  31. //
  32. // Copy mesh filter
  33. //
  34. MeshFilter sourceMeshFilter = GetComponent<MeshFilter>();
  35. if ( sourceMeshFilter != null )
  36. {
  37. MeshFilter destMeshFilter = seeThru.AddComponent<MeshFilter>();
  38. destMeshFilter.sharedMesh = sourceMeshFilter.sharedMesh;
  39. }
  40. //
  41. // Copy mesh renderer
  42. //
  43. MeshRenderer sourceMeshRenderer = GetComponent<MeshRenderer>();
  44. if ( sourceMeshRenderer != null )
  45. {
  46. sourceRenderer = sourceMeshRenderer;
  47. destRenderer = seeThru.AddComponent<MeshRenderer>();
  48. }
  49. //
  50. // Copy skinned mesh renderer
  51. //
  52. SkinnedMeshRenderer sourceSkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
  53. if ( sourceSkinnedMeshRenderer != null )
  54. {
  55. SkinnedMeshRenderer destSkinnedMeshRenderer = seeThru.AddComponent<SkinnedMeshRenderer>();
  56. sourceRenderer = sourceSkinnedMeshRenderer;
  57. destRenderer = destSkinnedMeshRenderer;
  58. destSkinnedMeshRenderer.sharedMesh = sourceSkinnedMeshRenderer.sharedMesh;
  59. destSkinnedMeshRenderer.rootBone = sourceSkinnedMeshRenderer.rootBone;
  60. destSkinnedMeshRenderer.bones = sourceSkinnedMeshRenderer.bones;
  61. destSkinnedMeshRenderer.quality = sourceSkinnedMeshRenderer.quality;
  62. destSkinnedMeshRenderer.updateWhenOffscreen = sourceSkinnedMeshRenderer.updateWhenOffscreen;
  63. }
  64. //
  65. // Create see thru materials
  66. //
  67. if ( sourceRenderer != null && destRenderer != null )
  68. {
  69. int materialCount = sourceRenderer.sharedMaterials.Length;
  70. Material[] destRendererMaterials = new Material[materialCount];
  71. for ( int i = 0; i < materialCount; i++ )
  72. {
  73. destRendererMaterials[i] = seeThruMaterial;
  74. }
  75. destRenderer.sharedMaterials = destRendererMaterials;
  76. for ( int i = 0; i < destRenderer.materials.Length; i++ )
  77. {
  78. destRenderer.materials[i].renderQueue = 2001; // Rendered after geometry
  79. }
  80. for ( int i = 0; i < sourceRenderer.materials.Length; i++ )
  81. {
  82. if ( sourceRenderer.materials[i].renderQueue == 2000 )
  83. {
  84. sourceRenderer.materials[i].renderQueue = 2002;
  85. }
  86. }
  87. }
  88. seeThru.gameObject.SetActive( false );
  89. }
  90. //-------------------------------------------------
  91. void OnEnable()
  92. {
  93. interactable.onAttachedToHand += AttachedToHand;
  94. interactable.onDetachedFromHand += DetachedFromHand;
  95. }
  96. //-------------------------------------------------
  97. void OnDisable()
  98. {
  99. interactable.onAttachedToHand -= AttachedToHand;
  100. interactable.onDetachedFromHand -= DetachedFromHand;
  101. }
  102. //-------------------------------------------------
  103. private void AttachedToHand( Hand hand )
  104. {
  105. seeThru.SetActive( true );
  106. }
  107. //-------------------------------------------------
  108. private void DetachedFromHand( Hand hand )
  109. {
  110. seeThru.SetActive( false );
  111. }
  112. //-------------------------------------------------
  113. void Update()
  114. {
  115. if ( seeThru.activeInHierarchy )
  116. {
  117. int materialCount = Mathf.Min( sourceRenderer.materials.Length, destRenderer.materials.Length );
  118. for ( int i = 0; i < materialCount; i++ )
  119. {
  120. destRenderer.materials[i].mainTexture = sourceRenderer.materials[i].mainTexture;
  121. destRenderer.materials[i].color = destRenderer.materials[i].color * sourceRenderer.materials[i].color;
  122. }
  123. }
  124. }
  125. }
  126. }