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.

227 lines
7.3 KiB

  1. /************************************************************************************
  2. Filename : OVRLipSyncContextMorphTarget.cs
  3. Content : This bridges the viseme output to the morph targets
  4. Created : August 7th, 2015
  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. using System.Linq;
  21. public class OVRLipSyncContextMorphTarget : MonoBehaviour
  22. {
  23. // PUBLIC
  24. // Manually assign the skinned mesh renderer to this script
  25. [Tooltip("Skinned Mesh Rendered target to be driven by Oculus Lipsync")]
  26. public SkinnedMeshRenderer skinnedMeshRenderer = null;
  27. // Set the blendshape index to go to (-1 means there is not one assigned)
  28. [Tooltip("Blendshape index to trigger for each viseme.")]
  29. public int [] visemeToBlendTargets = Enumerable.Range(0, OVRLipSync.VisemeCount).ToArray();
  30. // enable/disable sending signals to viseme engine
  31. [Tooltip("Enable using the test keys defined below to manually trigger each viseme.")]
  32. public bool enableVisemeTestKeys = false;
  33. [Tooltip("Test keys used to manually trigger an individual viseme - by " +
  34. "default the QWERTY row of a US keyboard.")]
  35. public KeyCode[] visemeTestKeys =
  36. {
  37. KeyCode.BackQuote,
  38. KeyCode.Tab,
  39. KeyCode.Q,
  40. KeyCode.W,
  41. KeyCode.E,
  42. KeyCode.R,
  43. KeyCode.T,
  44. KeyCode.Y,
  45. KeyCode.U,
  46. KeyCode.I,
  47. KeyCode.O,
  48. KeyCode.P,
  49. KeyCode.LeftBracket,
  50. KeyCode.RightBracket,
  51. KeyCode.Backslash,
  52. };
  53. [Tooltip("Test key used to manually trigger laughter and visualise the results")]
  54. public KeyCode laughterKey = KeyCode.CapsLock;
  55. [Tooltip("Blendshape index to trigger for laughter")]
  56. public int laughterBlendTarget = OVRLipSync.VisemeCount;
  57. [Range(0.0f, 1.0f)]
  58. [Tooltip("Laughter probability threshold above which the laughter blendshape will be activated")]
  59. public float laughterThreshold = 0.5f;
  60. [Range(0.0f, 3.0f)]
  61. [Tooltip("Laughter animation linear multiplier, the final output will be clamped to 1.0")]
  62. public float laughterMultiplier = 1.5f;
  63. // smoothing amount
  64. [Range(1, 100)]
  65. [Tooltip("Smoothing of 1 will yield only the current predicted viseme, 100 will yield an extremely smooth viseme response.")]
  66. public int smoothAmount = 70;
  67. // PRIVATE
  68. // Look for a lip-sync Context (should be set at the same level as this component)
  69. private OVRLipSyncContextBase lipsyncContext = null;
  70. /// <summary>
  71. /// Start this instance.
  72. /// </summary>
  73. void Start ()
  74. {
  75. // morph target needs to be set manually; possibly other components will need the same
  76. if(skinnedMeshRenderer == null)
  77. {
  78. Debug.LogError("LipSyncContextMorphTarget.Start Error: " +
  79. "Please set the target Skinned Mesh Renderer to be controlled!");
  80. return;
  81. }
  82. // make sure there is a phoneme context assigned to this object
  83. lipsyncContext = GetComponent<OVRLipSyncContextBase>();
  84. if(lipsyncContext == null)
  85. {
  86. Debug.LogError("LipSyncContextMorphTarget.Start Error: " +
  87. "No OVRLipSyncContext component on this object!");
  88. }
  89. else
  90. {
  91. // Send smoothing amount to context
  92. lipsyncContext.Smoothing = smoothAmount;
  93. }
  94. }
  95. /// <summary>
  96. /// Update this instance.
  97. /// </summary>
  98. void Update ()
  99. {
  100. if((lipsyncContext != null) && (skinnedMeshRenderer != null))
  101. {
  102. // get the current viseme frame
  103. OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
  104. if (frame != null)
  105. {
  106. SetVisemeToMorphTarget(frame);
  107. SetLaughterToMorphTarget(frame);
  108. }
  109. // TEST visemes by capturing key inputs and sending a signal
  110. CheckForKeys();
  111. // Update smoothing value
  112. if (smoothAmount != lipsyncContext.Smoothing)
  113. {
  114. lipsyncContext.Smoothing = smoothAmount;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Sends the signals.
  120. /// </summary>
  121. void CheckForKeys()
  122. {
  123. if (enableVisemeTestKeys)
  124. {
  125. for (int i = 0; i < OVRLipSync.VisemeCount; ++i)
  126. {
  127. CheckVisemeKey(visemeTestKeys[i], i, 100);
  128. }
  129. }
  130. CheckLaughterKey();
  131. }
  132. /// <summary>
  133. /// Sets the viseme to morph target.
  134. /// </summary>
  135. void SetVisemeToMorphTarget(OVRLipSync.Frame frame)
  136. {
  137. for (int i = 0; i < visemeToBlendTargets.Length; i++)
  138. {
  139. if (visemeToBlendTargets[i] != -1)
  140. {
  141. // Viseme blend weights are in range of 0->1.0, we need to make range 100
  142. skinnedMeshRenderer.SetBlendShapeWeight(
  143. visemeToBlendTargets[i],
  144. frame.Visemes[i] * 100.0f);
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// Sets the laughter to morph target.
  150. /// </summary>
  151. void SetLaughterToMorphTarget(OVRLipSync.Frame frame)
  152. {
  153. if (laughterBlendTarget != -1)
  154. {
  155. // Laughter score will be raw classifier output in [0,1]
  156. float laughterScore = frame.laughterScore;
  157. // Threshold then re-map to [0,1]
  158. laughterScore = laughterScore < laughterThreshold ? 0.0f : laughterScore - laughterThreshold;
  159. laughterScore = Mathf.Min(laughterScore * laughterMultiplier, 1.0f);
  160. laughterScore *= 1.0f / laughterThreshold;
  161. skinnedMeshRenderer.SetBlendShapeWeight(
  162. laughterBlendTarget,
  163. laughterScore * 100.0f);
  164. }
  165. }
  166. /// <summary>
  167. /// Sends the viseme signal.
  168. /// </summary>
  169. /// <param name="key">Key.</param>
  170. /// <param name="viseme">Viseme.</param>
  171. /// <param name="arg1">Arg1.</param>
  172. void CheckVisemeKey(KeyCode key, int viseme, int amount)
  173. {
  174. if (Input.GetKeyDown(key))
  175. {
  176. lipsyncContext.SetVisemeBlend(visemeToBlendTargets[viseme], amount);
  177. }
  178. if (Input.GetKeyUp(key))
  179. {
  180. lipsyncContext.SetVisemeBlend(visemeToBlendTargets[viseme], 0);
  181. }
  182. }
  183. /// <summary>
  184. /// Sends the laughter signal.
  185. /// </summary>
  186. void CheckLaughterKey()
  187. {
  188. if (Input.GetKeyDown(laughterKey))
  189. {
  190. lipsyncContext.SetLaughterBlend(100);
  191. }
  192. if (Input.GetKeyUp(laughterKey))
  193. {
  194. lipsyncContext.SetLaughterBlend(0);
  195. }
  196. }
  197. }