SeeThru.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }