| 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 | |
| 8 | Licensed under the Oculus Audio SDK License Version 3.3 (the "License"); |
| 9 | you may not use the Oculus Audio SDK except in compliance with the License, |
| 10 | which is provided at the time of installation or download, or which |
| 11 | otherwise accompanies this software in either electronic or hard copy form. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | https://developer.oculus.com/licenses/audio-3.3/ |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, the Oculus Audio SDK |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | ************************************************************************************/ |
| 23 | using UnityEngine; |
| 24 | |
| 25 | public class OVRLipSyncContextTextureFlip : MonoBehaviour |
| 26 | { |
| 27 | // PUBLIC |
| 28 | |
| 29 | // Manually assign the material |
| 30 | public Material material = null; |
| 31 | |
| 32 | // Set the textures for each viseme. We should follow the viseme order as specified |
| 33 | // by the Phoneme list |
| 34 | [Tooltip("The texture used for each viseme.")] |
| 35 | [OVRNamedArray(new string[] { "sil", "PP", "FF", "TH", "DD", "kk", "CH", |
| 36 | "SS", "nn", "RR", "aa", "E", "ih", "oh", "ou" })] |
| 37 | public Texture[] Textures = new Texture[OVRLipSync.VisemeCount]; |
| 38 | |
| 39 | // smoothing amount |
| 40 | [Range(1, 100)] |
| 41 | [Tooltip("Smoothing of 1 will yield only the current predicted viseme," + |
| 42 | "100 will yield an extremely smooth viseme response.")] |
| 43 | public int smoothAmount = 70; |
| 44 | |
| 45 | // PRIVATE |
| 46 | |
| 47 | // Look for a Phoneme Context (should be set at the same level as this component) |
| 48 | private OVRLipSyncContextBase lipsyncContext = null; |
| 49 | |
| 50 | // Capture the old viseme frame (we will write back into this one) |
| 51 | private OVRLipSync.Frame oldFrame = new OVRLipSync.Frame(); |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Start this instance. |
| 55 | /// </summary> |
| 56 | void Start() |
| 57 | { |
| 58 | // make sure there is a phoneme context assigned to this object |
| 59 | lipsyncContext = GetComponent<OVRLipSyncContextBase>(); |
| 60 | if (lipsyncContext == null) |
| 61 | { |
| 62 | Debug.LogWarning("LipSyncContextTextureFlip.Start WARNING:" + |
| 63 | " No lip sync context component set to object"); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | // Send smoothing amount to context |
| 68 | lipsyncContext.Smoothing = smoothAmount; |
| 69 | } |
| 70 | |
| 71 | if (material == null) |
| 72 | { |
| 73 | Debug.LogWarning("LipSyncContextTextureFlip.Start WARNING:" + |
| 74 | " Lip sync context texture flip has no material target to control!"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Update this instance. |
| 80 | /// </summary> |
| 81 | void Update () |
| 82 | { |
| 83 | if((lipsyncContext != null) && (material != null)) |
| 84 | { |
| 85 | // trap inputs and send signals to phoneme engine for testing purposes |
| 86 | |
| 87 | // get the current viseme frame |
| 88 | OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame(); |
| 89 | if (frame != null) |
| 90 | { |
| 91 | // Perform smoothing here if on original provider |
| 92 | if (lipsyncContext.provider == OVRLipSync.ContextProviders.Original) |
| 93 | { |
| 94 | // Go through the current and old |
| 95 | for (int i = 0; i < frame.Visemes.Length; i++) |
| 96 | { |
| 97 | // Convert 1-100 to old * (0.00 - 0.99) |
| 98 | float smoothing = ((smoothAmount - 1) / 100.0f); |
| 99 | oldFrame.Visemes[i] = |
| 100 | oldFrame.Visemes[i] * smoothing + |
| 101 | frame.Visemes[i] * (1.0f - smoothing); |
| 102 | } |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | oldFrame.Visemes = frame.Visemes; |
| 107 | } |
| 108 | |
| 109 | SetVisemeToTexture(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Update smoothing value in context |
| 114 | if (smoothAmount != lipsyncContext.Smoothing) |
| 115 | { |
| 116 | lipsyncContext.Smoothing = smoothAmount; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Sets the viseme to texture. |
| 122 | /// </summary> |
| 123 | void SetVisemeToTexture() |
| 124 | { |
| 125 | // This setting will run through all the Visemes, find the |
| 126 | // one with the greatest amplitude and set it to max value. |
| 127 | // all other visemes will be set to zero. |
| 128 | int gV = -1; |
| 129 | float gA = 0.0f; |
| 130 | |
| 131 | for (int i = 0; i < oldFrame.Visemes.Length; i++) |
| 132 | { |
| 133 | if(oldFrame.Visemes[i] > gA) |
| 134 | { |
| 135 | gV = i; |
| 136 | gA = oldFrame.Visemes[i]; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if ((gV != -1) && (gV < Textures.Length)) |
| 141 | { |
| 142 | Texture t = Textures[gV]; |
| 143 | |
| 144 | if(t != null) |
| 145 | { |
| 146 | material.SetTexture("_MainTex", t); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |