LipSyncDemo_SetCurrentTarget.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /************************************************************************************
  2. Filename : LipSyncDemo_SetCurrentTarget.cs
  3. Content : Update LipSync Demo current target
  4. Created : July 11, 2018
  5. Copyright : Copyright Facebook Technologies, LLC and its affiliates.
  6. All rights reserved.
  7. Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
  8. you may not use the Oculus Audio SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. https://developer.oculus.com/licenses/audio-3.3/
  13. Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. using UnityEngine;
  20. #if UNITY_2019_1_OR_NEWER
  21. using UnityEngine.XR;
  22. #endif
  23. using System.Collections;
  24. using System.Collections.Generic;
  25. public class LipSyncDemo_SetCurrentTarget : MonoBehaviour
  26. {
  27. public EnableSwitch[] SwitchTargets;
  28. private int targetSet = 0;
  29. private int maxTarget = 6;
  30. private bool XRButtonBeingPressed = false;
  31. // Use this for initialization
  32. void Start ()
  33. {
  34. // Add a listener to the OVRTouchpad for touch events
  35. OVRTouchpad.AddListener(LocalTouchEventCallback);
  36. // Initialize the proper target set
  37. targetSet = 0;
  38. SwitchTargets[0].SetActive<OVRLipSyncContextMorphTarget>(0);
  39. SwitchTargets[1].SetActive<OVRLipSyncContextMorphTarget>(0);
  40. }
  41. // Update is called once per frame
  42. // Logic for LipSync_Demo
  43. void Update ()
  44. {
  45. if (Input.GetKeyDown(KeyCode.Alpha1))
  46. {
  47. targetSet = 0;
  48. SetCurrentTarget();
  49. }
  50. else if (Input.GetKeyDown(KeyCode.Alpha2))
  51. {
  52. targetSet = 1;
  53. SetCurrentTarget();
  54. }
  55. else if (Input.GetKeyDown(KeyCode.Alpha3))
  56. {
  57. targetSet = 2;
  58. SetCurrentTarget();
  59. }
  60. else if (Input.GetKeyDown(KeyCode.Alpha4))
  61. {
  62. targetSet = 3;
  63. SetCurrentTarget();
  64. }
  65. else if (Input.GetKeyDown(KeyCode.Alpha5))
  66. {
  67. targetSet = 4;
  68. SetCurrentTarget();
  69. }
  70. else if (Input.GetKeyDown(KeyCode.Alpha6))
  71. {
  72. targetSet = 5;
  73. SetCurrentTarget();
  74. }
  75. // Close app
  76. if (Input.GetKeyDown (KeyCode.Escape))
  77. {
  78. Application.Quit();
  79. }
  80. // VR controllers: primary buttons scrolls forward, secondary backwards
  81. #if UNITY_2019_1_OR_NEWER
  82. var inputDevices = new List<InputDevice>();
  83. #if UNITY_2019_3_OR_NEWER
  84. InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeldInHand, inputDevices);
  85. #else
  86. InputDevices.GetDevicesWithRole(InputDeviceRole.RightHanded, inputDevices);
  87. #endif
  88. var primaryButtonPressed = false;
  89. var secondaryButtonPressed = false;
  90. foreach (var device in inputDevices)
  91. {
  92. bool boolValue;
  93. if (device.TryGetFeatureValue(CommonUsages.primaryButton, out boolValue) && boolValue)
  94. {
  95. primaryButtonPressed = true;
  96. }
  97. if (device.TryGetFeatureValue(CommonUsages.secondaryButton, out boolValue) && boolValue)
  98. {
  99. secondaryButtonPressed = true;
  100. }
  101. }
  102. if (primaryButtonPressed && !XRButtonBeingPressed)
  103. {
  104. targetSet++;
  105. if (targetSet >= maxTarget)
  106. {
  107. targetSet = 0;
  108. }
  109. SetCurrentTarget();
  110. }
  111. if (secondaryButtonPressed && !XRButtonBeingPressed)
  112. {
  113. targetSet--;
  114. if (targetSet < 0)
  115. {
  116. targetSet = maxTarget - 1;
  117. }
  118. SetCurrentTarget();
  119. }
  120. XRButtonBeingPressed = primaryButtonPressed || secondaryButtonPressed;
  121. #endif
  122. }
  123. /// <summary>
  124. /// Sets the current target.
  125. /// </summary>
  126. void SetCurrentTarget()
  127. {
  128. switch(targetSet)
  129. {
  130. case(0):
  131. SwitchTargets[0].SetActive<OVRLipSyncContextMorphTarget>(0);
  132. SwitchTargets[1].SetActive<OVRLipSyncContextMorphTarget>(0);
  133. break;
  134. case(1):
  135. SwitchTargets[0].SetActive<OVRLipSyncContextTextureFlip>(0);
  136. SwitchTargets[1].SetActive<OVRLipSyncContextTextureFlip>(1);
  137. break;
  138. case(2):
  139. SwitchTargets[0].SetActive<OVRLipSyncContextMorphTarget>(1);
  140. SwitchTargets[1].SetActive<OVRLipSyncContextMorphTarget>(2);
  141. break;
  142. case(3):
  143. SwitchTargets[0].SetActive<OVRLipSyncContextTextureFlip>(1);
  144. SwitchTargets[1].SetActive<OVRLipSyncContextTextureFlip>(3);
  145. break;
  146. case(4):
  147. SwitchTargets[0].SetActive<OVRLipSyncContextMorphTarget>(2);
  148. SwitchTargets[1].SetActive<OVRLipSyncContextMorphTarget>(4);
  149. break;
  150. case(5):
  151. SwitchTargets[0].SetActive<OVRLipSyncContextTextureFlip>(2);
  152. SwitchTargets[1].SetActive<OVRLipSyncContextTextureFlip>(5);
  153. break;
  154. }
  155. OVRLipSyncDebugConsole.Clear();
  156. }
  157. /// <summary>
  158. /// Local touch event callback.
  159. /// </summary>
  160. /// <param name="touchEvent">Touch event.</param>
  161. void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent)
  162. {
  163. switch(touchEvent)
  164. {
  165. case(OVRTouchpad.TouchEvent.Left):
  166. targetSet--;
  167. if(targetSet < 0)
  168. targetSet = maxTarget - 1;
  169. SetCurrentTarget();
  170. break;
  171. case(OVRTouchpad.TouchEvent.Right):
  172. targetSet++;
  173. if(targetSet >= maxTarget)
  174. targetSet = 0;
  175. SetCurrentTarget();
  176. break;
  177. }
  178. }
  179. }