Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

XRLayoutBuilder.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
  2. #if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) && !UNITY_FORCE_INPUTSYSTEM_XR_OFF
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine.InputSystem.LowLevel;
  6. using UnityEngine.InputSystem.Utilities;
  7. using System.Text;
  8. using UnityEngine.InputSystem.Layouts;
  9. using UnityEngine.XR;
  10. namespace UnityEngine.InputSystem.XR
  11. {
  12. internal class XRLayoutBuilder
  13. {
  14. private string parentLayout;
  15. private string interfaceName;
  16. private XRDeviceDescriptor descriptor;
  17. private static uint GetSizeOfFeature(XRFeatureDescriptor featureDescriptor)
  18. {
  19. switch (featureDescriptor.featureType)
  20. {
  21. case FeatureType.Binary:
  22. return sizeof(byte);
  23. case FeatureType.DiscreteStates:
  24. return sizeof(int);
  25. case FeatureType.Axis1D:
  26. return sizeof(float);
  27. case FeatureType.Axis2D:
  28. return sizeof(float) * 2;
  29. case FeatureType.Axis3D:
  30. return sizeof(float) * 3;
  31. case FeatureType.Rotation:
  32. return sizeof(float) * 4;
  33. case FeatureType.Hand:
  34. return sizeof(uint) * 26;
  35. case FeatureType.Bone:
  36. return sizeof(uint) + (sizeof(float) * 3) + (sizeof(float) * 4);
  37. case FeatureType.Eyes:
  38. return (sizeof(float) * 3) * 3 + ((sizeof(float) * 4) * 2) + (sizeof(float) * 2);
  39. case FeatureType.Custom:
  40. return featureDescriptor.customSize;
  41. }
  42. return 0;
  43. }
  44. private static string SanitizeString(string original, bool allowPaths = false)
  45. {
  46. var stringLength = original.Length;
  47. var sanitizedName = new StringBuilder(stringLength);
  48. for (var i = 0; i < stringLength; i++)
  49. {
  50. var letter = original[i];
  51. if (char.IsUpper(letter) || char.IsLower(letter) || char.IsDigit(letter) || letter == '_' || (allowPaths && (letter == '/')))
  52. {
  53. sanitizedName.Append(letter);
  54. }
  55. }
  56. return sanitizedName.ToString();
  57. }
  58. internal static string OnFindLayoutForDevice(ref InputDeviceDescription description, string matchedLayout,
  59. InputDeviceExecuteCommandDelegate executeCommandDelegate)
  60. {
  61. // If the device isn't a XRInput, we're not interested.
  62. if (description.interfaceName != XRUtilities.InterfaceCurrent && description.interfaceName != XRUtilities.InterfaceV1)
  63. {
  64. return null;
  65. }
  66. // If the description doesn't come with a XR SDK descriptor, we're not
  67. // interested either.
  68. if (string.IsNullOrEmpty(description.capabilities))
  69. {
  70. return null;
  71. }
  72. // Try to parse the XR descriptor.
  73. XRDeviceDescriptor deviceDescriptor;
  74. try
  75. {
  76. deviceDescriptor = XRDeviceDescriptor.FromJson(description.capabilities);
  77. }
  78. catch (Exception)
  79. {
  80. return null;
  81. }
  82. if (deviceDescriptor == null)
  83. {
  84. return null;
  85. }
  86. if (string.IsNullOrEmpty(matchedLayout))
  87. {
  88. const InputDeviceCharacteristics controllerCharacteristics = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Controller;
  89. if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.HeadMounted) != 0)
  90. matchedLayout = "XRHMD";
  91. else if ((deviceDescriptor.characteristics & controllerCharacteristics) == controllerCharacteristics)
  92. matchedLayout = "XRController";
  93. }
  94. string layoutName;
  95. if (string.IsNullOrEmpty(description.manufacturer))
  96. {
  97. layoutName = $"{SanitizeString(description.interfaceName)}::{SanitizeString(description.product)}";
  98. }
  99. else
  100. {
  101. layoutName =
  102. $"{SanitizeString(description.interfaceName)}::{SanitizeString(description.manufacturer)}::{SanitizeString(description.product)}";
  103. }
  104. var layout = new XRLayoutBuilder { descriptor = deviceDescriptor, parentLayout = matchedLayout, interfaceName = description.interfaceName };
  105. InputSystem.RegisterLayoutBuilder(() => layout.Build(), layoutName, matchedLayout);
  106. return layoutName;
  107. }
  108. private static string ConvertPotentialAliasToName(InputControlLayout layout, string nameOrAlias)
  109. {
  110. var internedNameOrAlias = new InternedString(nameOrAlias);
  111. var controls = layout.controls;
  112. for (var i = 0; i < controls.Count; i++)
  113. {
  114. var controlItem = controls[i];
  115. if (controlItem.name == internedNameOrAlias)
  116. return nameOrAlias;
  117. var aliases = controlItem.aliases;
  118. for (var j = 0; j < aliases.Count; j++)
  119. {
  120. if (aliases[j] == nameOrAlias)
  121. return controlItem.name.ToString();
  122. }
  123. }
  124. return nameOrAlias;
  125. }
  126. private bool IsSubControl(string name)
  127. {
  128. return name.Contains('/');
  129. }
  130. private string GetParentControlName(string name)
  131. {
  132. int idx = name.IndexOf('/');
  133. return name.Substring(0, idx);
  134. }
  135. static readonly string[] poseSubControlNames =
  136. {
  137. "/isTracked",
  138. "/trackingState",
  139. "/position",
  140. "/rotation",
  141. "/velocity",
  142. "/angularVelocity"
  143. };
  144. static readonly FeatureType[] poseSubControlTypes =
  145. {
  146. FeatureType.Binary,
  147. FeatureType.DiscreteStates,
  148. FeatureType.Axis3D,
  149. FeatureType.Rotation,
  150. FeatureType.Axis3D,
  151. FeatureType.Axis3D
  152. };
  153. // A PoseControl consists of 6 subcontrols with specific names and types
  154. private bool IsPoseControl(List<XRFeatureDescriptor> features, int startIndex)
  155. {
  156. for (var i = 0; i < 6; i++)
  157. {
  158. if (!features[startIndex + i].name.EndsWith(poseSubControlNames[i]) ||
  159. features[startIndex + i].featureType != poseSubControlTypes[i])
  160. return false;
  161. }
  162. return true;
  163. }
  164. private InputControlLayout Build()
  165. {
  166. var builder = new InputControlLayout.Builder
  167. {
  168. stateFormat = new FourCC('X', 'R', 'S', '0'),
  169. extendsLayout = parentLayout,
  170. updateBeforeRender = true
  171. };
  172. var inheritedLayout = !string.IsNullOrEmpty(parentLayout)
  173. ? InputSystem.LoadLayout(parentLayout)
  174. : null;
  175. var parentControls = new List<string>();
  176. var currentUsages = new List<string>();
  177. uint currentOffset = 0;
  178. for (var i = 0; i < descriptor.inputFeatures.Count; i++)
  179. {
  180. var feature = descriptor.inputFeatures[i];
  181. currentUsages.Clear();
  182. if (feature.usageHints != null)
  183. {
  184. foreach (var usageHint in feature.usageHints)
  185. {
  186. if (!string.IsNullOrEmpty(usageHint.content))
  187. currentUsages.Add(usageHint.content);
  188. }
  189. }
  190. var featureName = feature.name;
  191. featureName = SanitizeString(featureName, true);
  192. if (inheritedLayout != null)
  193. featureName = ConvertPotentialAliasToName(inheritedLayout, featureName);
  194. featureName = featureName.ToLower();
  195. if (IsSubControl(featureName))
  196. {
  197. string parentControl = GetParentControlName(featureName);
  198. if (!parentControls.Contains(parentControl))
  199. {
  200. if (IsPoseControl(descriptor.inputFeatures, i))
  201. {
  202. builder.AddControl(parentControl)
  203. .WithLayout("Pose")
  204. .WithByteOffset(0);
  205. parentControls.Add(parentControl);
  206. }
  207. }
  208. }
  209. uint nextOffset = GetSizeOfFeature(feature);
  210. if (interfaceName == XRUtilities.InterfaceV1)
  211. {
  212. #if UNITY_ANDROID
  213. if (nextOffset < 4)
  214. nextOffset = 4;
  215. #endif
  216. }
  217. else
  218. {
  219. if (nextOffset >= 4 && (currentOffset % 4 != 0))
  220. currentOffset += (4 - (currentOffset % 4));
  221. }
  222. switch (feature.featureType)
  223. {
  224. case FeatureType.Binary:
  225. {
  226. builder.AddControl(featureName)
  227. .WithLayout("Button")
  228. .WithByteOffset(currentOffset)
  229. .WithFormat(InputStateBlock.FormatBit)
  230. .WithUsages(currentUsages);
  231. break;
  232. }
  233. case FeatureType.DiscreteStates:
  234. {
  235. builder.AddControl(featureName)
  236. .WithLayout("Integer")
  237. .WithByteOffset(currentOffset)
  238. .WithFormat(InputStateBlock.FormatInt)
  239. .WithUsages(currentUsages);
  240. break;
  241. }
  242. case FeatureType.Axis1D:
  243. {
  244. builder.AddControl(featureName)
  245. .WithLayout("Analog")
  246. .WithRange(-1, 1)
  247. .WithByteOffset(currentOffset)
  248. .WithFormat(InputStateBlock.FormatFloat)
  249. .WithUsages(currentUsages);
  250. break;
  251. }
  252. case FeatureType.Axis2D:
  253. {
  254. builder.AddControl(featureName)
  255. .WithLayout("Stick")
  256. .WithByteOffset(currentOffset)
  257. .WithFormat(InputStateBlock.FormatVector2)
  258. .WithUsages(currentUsages);
  259. builder.AddControl(featureName + "/x")
  260. .WithLayout("Analog")
  261. .WithRange(-1, 1);
  262. builder.AddControl(featureName + "/y")
  263. .WithLayout("Analog")
  264. .WithRange(-1, 1);
  265. break;
  266. }
  267. case FeatureType.Axis3D:
  268. {
  269. builder.AddControl(featureName)
  270. .WithLayout("Vector3")
  271. .WithByteOffset(currentOffset)
  272. .WithFormat(InputStateBlock.FormatVector3)
  273. .WithUsages(currentUsages);
  274. break;
  275. }
  276. case FeatureType.Rotation:
  277. {
  278. builder.AddControl(featureName)
  279. .WithLayout("Quaternion")
  280. .WithByteOffset(currentOffset)
  281. .WithFormat(InputStateBlock.FormatQuaternion)
  282. .WithUsages(currentUsages);
  283. break;
  284. }
  285. case FeatureType.Hand:
  286. {
  287. break;
  288. }
  289. case FeatureType.Bone:
  290. {
  291. builder.AddControl(featureName)
  292. .WithLayout("Bone")
  293. .WithByteOffset(currentOffset)
  294. .WithUsages(currentUsages);
  295. break;
  296. }
  297. case FeatureType.Eyes:
  298. {
  299. builder.AddControl(featureName)
  300. .WithLayout("Eyes")
  301. .WithByteOffset(currentOffset)
  302. .WithUsages(currentUsages);
  303. break;
  304. }
  305. }
  306. currentOffset += nextOffset;
  307. }
  308. return builder.Build();
  309. }
  310. }
  311. }
  312. #endif