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.

150 lines
5.1 KiB

  1. /************************************************************************************
  2. Filename : OVRLipSyncContextTextureFlip.cs
  3. Content : This bridges the phoneme/viseme output to texture flip 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. public class OVRLipSyncContextTextureFlip : MonoBehaviour
  21. {
  22. // PUBLIC
  23. // Manually assign the material
  24. public Material material = null;
  25. // Set the textures for each viseme. We should follow the viseme order as specified
  26. // by the Phoneme list
  27. [Tooltip("The texture used for each viseme.")]
  28. [OVRNamedArray(new string[] { "sil", "PP", "FF", "TH", "DD", "kk", "CH",
  29. "SS", "nn", "RR", "aa", "E", "ih", "oh", "ou" })]
  30. public Texture[] Textures = new Texture[OVRLipSync.VisemeCount];
  31. // smoothing amount
  32. [Range(1, 100)]
  33. [Tooltip("Smoothing of 1 will yield only the current predicted viseme," +
  34. "100 will yield an extremely smooth viseme response.")]
  35. public int smoothAmount = 70;
  36. // PRIVATE
  37. // Look for a Phoneme Context (should be set at the same level as this component)
  38. private OVRLipSyncContextBase lipsyncContext = null;
  39. // Capture the old viseme frame (we will write back into this one)
  40. private OVRLipSync.Frame oldFrame = new OVRLipSync.Frame();
  41. /// <summary>
  42. /// Start this instance.
  43. /// </summary>
  44. void Start()
  45. {
  46. // make sure there is a phoneme context assigned to this object
  47. lipsyncContext = GetComponent<OVRLipSyncContextBase>();
  48. if (lipsyncContext == null)
  49. {
  50. Debug.LogWarning("LipSyncContextTextureFlip.Start WARNING:" +
  51. " No lip sync context component set to object");
  52. }
  53. else
  54. {
  55. // Send smoothing amount to context
  56. lipsyncContext.Smoothing = smoothAmount;
  57. }
  58. if (material == null)
  59. {
  60. Debug.LogWarning("LipSyncContextTextureFlip.Start WARNING:" +
  61. " Lip sync context texture flip has no material target to control!");
  62. }
  63. }
  64. /// <summary>
  65. /// Update this instance.
  66. /// </summary>
  67. void Update ()
  68. {
  69. if((lipsyncContext != null) && (material != null))
  70. {
  71. // trap inputs and send signals to phoneme engine for testing purposes
  72. // get the current viseme frame
  73. OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
  74. if (frame != null)
  75. {
  76. // Perform smoothing here if on original provider
  77. if (lipsyncContext.provider == OVRLipSync.ContextProviders.Original)
  78. {
  79. // Go through the current and old
  80. for (int i = 0; i < frame.Visemes.Length; i++)
  81. {
  82. // Convert 1-100 to old * (0.00 - 0.99)
  83. float smoothing = ((smoothAmount - 1) / 100.0f);
  84. oldFrame.Visemes[i] =
  85. oldFrame.Visemes[i] * smoothing +
  86. frame.Visemes[i] * (1.0f - smoothing);
  87. }
  88. }
  89. else
  90. {
  91. oldFrame.Visemes = frame.Visemes;
  92. }
  93. SetVisemeToTexture();
  94. }
  95. }
  96. // Update smoothing value in context
  97. if (smoothAmount != lipsyncContext.Smoothing)
  98. {
  99. lipsyncContext.Smoothing = smoothAmount;
  100. }
  101. }
  102. /// <summary>
  103. /// Sets the viseme to texture.
  104. /// </summary>
  105. void SetVisemeToTexture()
  106. {
  107. // This setting will run through all the Visemes, find the
  108. // one with the greatest amplitude and set it to max value.
  109. // all other visemes will be set to zero.
  110. int gV = -1;
  111. float gA = 0.0f;
  112. for (int i = 0; i < oldFrame.Visemes.Length; i++)
  113. {
  114. if(oldFrame.Visemes[i] > gA)
  115. {
  116. gV = i;
  117. gA = oldFrame.Visemes[i];
  118. }
  119. }
  120. if ((gV != -1) && (gV < Textures.Length))
  121. {
  122. Texture t = Textures[gV];
  123. if(t != null)
  124. {
  125. material.SetTexture("_MainTex", t);
  126. }
  127. }
  128. }
  129. }