TestModelMaterial.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. // Use this Script in parent folder of all Humans
  4. public class TestModelMaterial : MonoBehaviour
  5. {
  6. [Multiline]
  7. public string info;
  8. [Header("Highlighting Materials:")]
  9. public Material highlightRed;
  10. public Material highlightGreen;
  11. public Material highlightYellow;
  12. [Header("Highlighting Transparent Materials:")]
  13. public Material highlightTransRed;
  14. public Material highlightTransGreen;
  15. public Material highlightTransYellow;
  16. [Header("Thresholds:")]
  17. [Tooltip("The maximum number of people (including the observed) that will be colored green.\n Threshold Green = 1 means that no further person is allowed to approach.")]
  18. public int thresholdGreen = 1;
  19. [Tooltip("The maximum number of people (including the observed) that will be colored yellow.\n Threshold Yellow = 2 means that only one other person may approach.")]
  20. public int thresholdYellow = 2;
  21. [Space]
  22. public float radius = 1f;
  23. private Vector3 center;
  24. private Collider[] hitHumans;
  25. private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
  26. // Start is called before the first frame update
  27. private void Start()
  28. {
  29. Component[] allTransform = GetComponentsInChildren<Transform>(); // all possible children of parent object
  30. foreach (Transform child in allTransform)
  31. {
  32. if (child.parent == allTransform[0])
  33. {
  34. allHumansTransform.Add(child);
  35. }
  36. }
  37. }
  38. private void OnDrawGizmos()
  39. {
  40. foreach (Component lst in allHumansTransform)
  41. {
  42. Gizmos.color = Color.blue;
  43. Gizmos.DrawWireSphere(lst.transform.position, radius);
  44. }
  45. }
  46. private void FixedUpdate()
  47. {
  48. foreach (Component lst in allHumansTransform)
  49. {
  50. center = lst.GetComponent<Transform>().position;
  51. // only collisions with Layer "Humans" are listed in "hitHumans"
  52. hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
  53. bool differentTag = false;
  54. foreach (Collider hitHuman in hitHumans)
  55. {
  56. if (lst.CompareTag(hitHuman.tag))
  57. differentTag = false;
  58. else
  59. {
  60. differentTag = true;
  61. break;
  62. }
  63. }
  64. int hitHumansLength = 0;
  65. foreach (Collider hitHuman in hitHumans)
  66. {
  67. if (lst.CompareTag(hitHuman.tag))
  68. hitHumansLength++;
  69. }
  70. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Green
  71. if (hitHumansLength <= thresholdGreen)
  72. {
  73. if (!differentTag)
  74. {
  75. Debug.Log(lst.gameObject.name + " set to Green");
  76. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightGreen;
  77. }
  78. else
  79. {
  80. Debug.Log(lst.gameObject.name + " set to Trans Green");
  81. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransGreen;
  82. }
  83. }
  84. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Yellow
  85. else if (hitHumansLength <= thresholdYellow)
  86. {
  87. if (!differentTag)
  88. {
  89. Debug.Log(lst.gameObject.name + " set to Yellow");
  90. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightYellow;
  91. }
  92. else
  93. {
  94. Debug.Log(lst.gameObject.name + " set to Trans Yellow");
  95. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransYellow;
  96. }
  97. }
  98. // everything else change to Red
  99. else
  100. {
  101. if (!differentTag)
  102. {
  103. Debug.Log(lst.gameObject.name + " set to Red");
  104. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
  105. }
  106. else
  107. {
  108. Debug.Log(lst.gameObject.name + " set to Trans Red");
  109. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransRed;
  110. }
  111. }
  112. }
  113. }
  114. }