2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
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.

321 lines
11 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using HTC.UnityPlugin.VRModuleManagement;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using UnityEngine;
  9. namespace HTC.UnityPlugin.Vive
  10. {
  11. // This script creates and handles SteamVR_RenderModel using viveRole property or device index
  12. [DisallowMultipleComponent]
  13. [AddComponentMenu("VIU/Hooks/Render Model Hook", 10)]
  14. public class RenderModelHook : MonoBehaviour, IViveRoleComponent
  15. {
  16. [AttributeUsage(AttributeTargets.Class)]
  17. public class CreatorPriorityAttirbute : Attribute
  18. {
  19. public int priority { get; set; }
  20. public CreatorPriorityAttirbute(int priority = 0) { this.priority = priority; }
  21. }
  22. public abstract class RenderModelCreator
  23. {
  24. public abstract bool shouldActive { get; }
  25. protected RenderModelHook hook { get; private set; }
  26. public void Initialize(RenderModelHook hook) { this.hook = hook; }
  27. public abstract void UpdateRenderModel();
  28. public abstract void CleanUpRenderModel();
  29. }
  30. [CreatorPriorityAttirbute(1)]
  31. public class DefaultRenderModelCreator : RenderModelCreator
  32. {
  33. private VRModuleDeviceModel m_loadedModelEnum = (VRModuleDeviceModel)(-1);
  34. protected GameObject m_model;
  35. public override bool shouldActive { get { return true; } }
  36. public override void UpdateRenderModel()
  37. {
  38. var index = hook.GetModelDeviceIndex();
  39. if (!VRModule.IsValidDeviceIndex(index))
  40. {
  41. if (m_model != null)
  42. {
  43. m_model.SetActive(false);
  44. }
  45. }
  46. else
  47. {
  48. var loadModelEnum = VRModuleDeviceModel.Unknown;
  49. if (hook.m_overrideModel != OverrideModelEnum.DontOverride)
  50. {
  51. loadModelEnum = (VRModuleDeviceModel)hook.m_overrideModel;
  52. }
  53. else
  54. {
  55. loadModelEnum = VRModule.GetCurrentDeviceState(index).deviceModel;
  56. }
  57. if (ChangeProp.Set(ref m_loadedModelEnum, loadModelEnum))
  58. {
  59. CleanUpRenderModel();
  60. var prefab = Resources.Load<GameObject>("Models/VIUModel" + m_loadedModelEnum.ToString());
  61. if (prefab != null)
  62. {
  63. m_model = Instantiate(prefab);
  64. m_model.transform.SetParent(hook.transform, false);
  65. m_model.gameObject.name = "VIUModel" + m_loadedModelEnum.ToString();
  66. if (hook.m_overrideShader != null)
  67. {
  68. var renderer = m_model.GetComponentInChildren<Renderer>();
  69. if (renderer != null)
  70. {
  71. renderer.material.shader = hook.m_overrideShader;
  72. }
  73. }
  74. }
  75. }
  76. if (m_model != null)
  77. {
  78. m_model.SetActive(true);
  79. }
  80. }
  81. }
  82. public override void CleanUpRenderModel()
  83. {
  84. if (m_model != null)
  85. {
  86. Destroy(m_model);
  87. m_model = null;
  88. }
  89. }
  90. }
  91. public enum Mode
  92. {
  93. Disable,
  94. ViveRole,
  95. DeivceIndex,
  96. }
  97. public enum Index
  98. {
  99. None = -1,
  100. Hmd,
  101. Device1,
  102. Device2,
  103. Device3,
  104. Device4,
  105. Device5,
  106. Device6,
  107. Device7,
  108. Device8,
  109. Device9,
  110. Device10,
  111. Device11,
  112. Device12,
  113. Device13,
  114. Device14,
  115. Device15,
  116. }
  117. public enum OverrideModelEnum
  118. {
  119. DontOverride = VRModuleDeviceModel.Unknown,
  120. ViveController = VRModuleDeviceModel.ViveController,
  121. ViveTracker = VRModuleDeviceModel.ViveTracker,
  122. ViveBaseStation = VRModuleDeviceModel.ViveBaseStation,
  123. OculusTouchLeft = VRModuleDeviceModel.OculusTouchLeft,
  124. OculusTouchRight = VRModuleDeviceModel.OculusTouchRight,
  125. OculusSensor = VRModuleDeviceModel.OculusSensor,
  126. KnucklesLeft = VRModuleDeviceModel.KnucklesLeft,
  127. KnucklesRight = VRModuleDeviceModel.KnucklesRight,
  128. DaydreamController = VRModuleDeviceModel.DaydreamController,
  129. ViveFocusFinch = VRModuleDeviceModel.ViveFocusFinch,
  130. OculusGoController = VRModuleDeviceModel.OculusGoController,
  131. OculusGearVrController = VRModuleDeviceModel.OculusGearVrController,
  132. WMRControllerLeft = VRModuleDeviceModel.WMRControllerLeft,
  133. WMRControllerRight = VRModuleDeviceModel.WMRControllerRight,
  134. ViveCosmosControllerLeft = VRModuleDeviceModel.ViveCosmosControllerLeft,
  135. ViveCosmosControllerRight = VRModuleDeviceModel.ViveCosmosControllerRight,
  136. OculusQuestControllerLeft = VRModuleDeviceModel.OculusQuestControllerLeft,
  137. OculusQuestControllerRight = VRModuleDeviceModel.OculusQuestControllerRight,
  138. IndexHMD = VRModuleDeviceModel.IndexHMD,
  139. IndexControllerLeft = VRModuleDeviceModel.IndexControllerLeft,
  140. IndexControllerRight = VRModuleDeviceModel.IndexControllerRight,
  141. }
  142. [SerializeField]
  143. private Mode m_mode = Mode.ViveRole;
  144. [SerializeField]
  145. private ViveRoleProperty m_viveRole = ViveRoleProperty.New(HandRole.RightHand);
  146. [SerializeField]
  147. private Transform m_origin;
  148. [SerializeField]
  149. private Index m_deviceIndex = Index.Hmd;
  150. [SerializeField]
  151. private OverrideModelEnum m_overrideModel = OverrideModelEnum.DontOverride;
  152. [SerializeField]
  153. private Shader m_overrideShader = null;
  154. private static readonly Type[] s_creatorTypes;
  155. private RenderModelCreator[] m_creators;
  156. private int m_activeCreatorIndex = -1;
  157. private int m_defaultCreatorIndex = -1;
  158. private bool m_isQuiting;
  159. public ViveRoleProperty viveRole { get { return m_viveRole; } }
  160. public Transform origin { get { return m_origin; } set { m_origin = value; } }
  161. public bool applyTracking { get; set; }
  162. public OverrideModelEnum overrideModel { get { return m_overrideModel; } set { m_overrideModel = value; } }
  163. public Shader overrideShader { get { return m_overrideShader; } set { m_overrideShader = value; } }
  164. static RenderModelHook()
  165. {
  166. try
  167. {
  168. var creatorTypes = new List<Type>();
  169. foreach (var type in Assembly.GetAssembly(typeof(RenderModelCreator)).GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(RenderModelCreator))))
  170. {
  171. creatorTypes.Add(type);
  172. }
  173. s_creatorTypes = creatorTypes.OrderBy(t =>
  174. {
  175. foreach (var at in t.GetCustomAttributes(typeof(CreatorPriorityAttirbute), true))
  176. {
  177. return ((CreatorPriorityAttirbute)at).priority;
  178. }
  179. return 0;
  180. }).ToArray();
  181. }
  182. catch (Exception e)
  183. {
  184. s_creatorTypes = new Type[0];
  185. Debug.LogError(e);
  186. }
  187. }
  188. #if UNITY_EDITOR
  189. private void OnValidate()
  190. {
  191. if (isActiveAndEnabled && Application.isPlaying && VRModule.Active)
  192. {
  193. UpdateModel();
  194. }
  195. }
  196. #endif
  197. private void Awake()
  198. {
  199. m_creators = new RenderModelCreator[s_creatorTypes.Length];
  200. for (int i = s_creatorTypes.Length - 1; i >= 0; --i)
  201. {
  202. m_creators[i] = (RenderModelCreator)Activator.CreateInstance(s_creatorTypes[i]);
  203. m_creators[i].Initialize(this);
  204. if (s_creatorTypes[i] == typeof(DefaultRenderModelCreator))
  205. {
  206. m_defaultCreatorIndex = i;
  207. }
  208. }
  209. }
  210. protected virtual void OnEnable()
  211. {
  212. VRModule.onActiveModuleChanged += OnActiveModuleChanged;
  213. m_viveRole.onDeviceIndexChanged += OnDeviceIndexChanged;
  214. m_viveRole.onRoleChanged += UpdateModel;
  215. UpdateModel();
  216. }
  217. protected virtual void OnDisable()
  218. {
  219. VRModule.onActiveModuleChanged -= OnActiveModuleChanged;
  220. m_viveRole.onDeviceIndexChanged -= OnDeviceIndexChanged;
  221. m_viveRole.onRoleChanged -= UpdateModel;
  222. UpdateModel();
  223. }
  224. private void OnDeviceIndexChanged(uint deviceIndex) { UpdateModel(); }
  225. private void OnActiveModuleChanged(VRModuleActiveEnum module) { UpdateModel(); }
  226. private void OnApplicationQuit() { m_isQuiting = true; }
  227. public uint GetModelDeviceIndex()
  228. {
  229. if (!enabled) { return VRModule.INVALID_DEVICE_INDEX; }
  230. uint result;
  231. switch (m_mode)
  232. {
  233. case Mode.ViveRole:
  234. result = m_viveRole.GetDeviceIndex();
  235. break;
  236. case Mode.DeivceIndex:
  237. result = (uint)m_deviceIndex;
  238. break;
  239. case Mode.Disable:
  240. default:
  241. return VRModule.INVALID_DEVICE_INDEX;
  242. }
  243. return result;
  244. }
  245. private void UpdateModel()
  246. {
  247. if (m_isQuiting) { return; }
  248. var activeCreatorIndex = -1;
  249. if (enabled)
  250. {
  251. if (m_overrideModel == OverrideModelEnum.DontOverride)
  252. {
  253. for (int i = 0, imax = m_creators.Length; i < imax; ++i)
  254. {
  255. if (m_creators[i].shouldActive)
  256. {
  257. activeCreatorIndex = i;
  258. break;
  259. }
  260. }
  261. }
  262. else
  263. {
  264. activeCreatorIndex = m_defaultCreatorIndex;
  265. }
  266. }
  267. if (m_activeCreatorIndex != activeCreatorIndex)
  268. {
  269. // clean up model created from previous active creator
  270. if (m_activeCreatorIndex >= 0)
  271. {
  272. m_creators[m_activeCreatorIndex].CleanUpRenderModel();
  273. }
  274. m_activeCreatorIndex = activeCreatorIndex;
  275. }
  276. if (m_activeCreatorIndex >= 0)
  277. {
  278. m_creators[m_activeCreatorIndex].UpdateRenderModel();
  279. }
  280. }
  281. }
  282. }