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.

96 lines
2.5 KiB

  1. /************************************************************************************
  2. Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. See SampleFramework license.txt for license terms. Unless required by applicable law
  4. or agreed to in writing, the sample code is provided ?AS IS? WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  6. language governing permissions and limitations under the license.
  7. ************************************************************************************/
  8. using UnityEngine;
  9. namespace OculusSampleFramework
  10. {
  11. public class SelectionCylinder : MonoBehaviour
  12. {
  13. public enum SelectionState
  14. {
  15. Off = 0,
  16. Selected,
  17. Highlighted
  18. }
  19. [SerializeField] private MeshRenderer _selectionMeshRenderer = null;
  20. private static int _colorId = Shader.PropertyToID("_Color");
  21. private Material[] _selectionMaterials;
  22. private Color[] _defaultSelectionColors = null, _highlightColors = null;
  23. private SelectionState _currSelectionState = SelectionState.Off;
  24. public SelectionState CurrSelectionState
  25. {
  26. get { return _currSelectionState; }
  27. set
  28. {
  29. var oldState = _currSelectionState;
  30. _currSelectionState = value;
  31. if (oldState != _currSelectionState)
  32. {
  33. if (_currSelectionState > SelectionState.Off)
  34. {
  35. _selectionMeshRenderer.enabled = true;
  36. AffectSelectionColor(_currSelectionState == SelectionState.Selected
  37. ? _defaultSelectionColors
  38. : _highlightColors);
  39. }
  40. else
  41. {
  42. _selectionMeshRenderer.enabled = false;
  43. }
  44. }
  45. }
  46. }
  47. private void Awake()
  48. {
  49. _selectionMaterials = _selectionMeshRenderer.materials;
  50. int numColors = _selectionMaterials.Length;
  51. _defaultSelectionColors = new Color[numColors];
  52. _highlightColors = new Color[numColors];
  53. for (int i = 0; i < numColors; i++)
  54. {
  55. _defaultSelectionColors[i] = _selectionMaterials[i].GetColor(_colorId);
  56. _highlightColors[i] = new Color(1.0f, 1.0f, 1.0f, _defaultSelectionColors[i].a);
  57. }
  58. CurrSelectionState = SelectionState.Off;
  59. }
  60. private void OnDestroy()
  61. {
  62. if (_selectionMaterials != null)
  63. {
  64. foreach (var selectionMaterial in _selectionMaterials)
  65. {
  66. if (selectionMaterial != null)
  67. {
  68. Destroy(selectionMaterial);
  69. }
  70. }
  71. }
  72. }
  73. private void AffectSelectionColor(Color[] newColors)
  74. {
  75. int numColors = newColors.Length;
  76. for (int i = 0; i < numColors; i++)
  77. {
  78. _selectionMaterials[i].SetColor(_colorId, newColors[i]);
  79. }
  80. }
  81. }
  82. }