BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / LipSync / Scripts / OVRLipSyncContextTextureFlip.cs

OVRLipSyncContextTextureFlip.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRLipSyncContextTextureFlip.cs 5.1 KiB Download Raw
1/************************************************************************************
2Filename : OVRLipSyncContextTextureFlip.cs
3Content : This bridges the phoneme/viseme output to texture flip targets
4Created : August 7th, 2015
5Copyright : Copyright Facebook Technologies, LLC and its affiliates.
6 All rights reserved.
7
8Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
9you may not use the Oculus Audio SDK except in compliance with the License,
10which is provided at the time of installation or download, or which
11otherwise accompanies this software in either electronic or hard copy form.
12
13You may obtain a copy of the License at
14
15https://developer.oculus.com/licenses/audio-3.3/
16
17Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
18distributed under the License is distributed on an "AS IS" BASIS,
19WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20See the License for the specific language governing permissions and
21limitations under the License.
22************************************************************************************/
23using UnityEngine;
24
25public 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}