| 1 | using UnityEngine; |
| 2 | using System.Collections; |
| 3 | |
| 4 | public class VoiceModDemo_Logic : MonoBehaviour |
| 5 | { |
| 6 | public OVRVoiceModContext[] contexts; |
| 7 | public Material material; |
| 8 | public Transform[] xfrms; |
| 9 | public VoiceModEnableSwitch SwitchTarget; |
| 10 | |
| 11 | private int targetSet = 0; |
| 12 | |
| 13 | // Use this as the init value for the model scale |
| 14 | private Vector3 scale = new Vector3(3.0f, 3.0f, 3.0f); |
| 15 | private float scaleMax = 10.0f; |
| 16 | |
| 17 | private int currentPreset = 0; |
| 18 | |
| 19 | // Use this for initialization |
| 20 | void Start () |
| 21 | { |
| 22 | // Add a listener to the OVRMessenger for touch events |
| 23 | OVRMessenger.AddListener<OVRTouchpad.TouchEvent>("Touchpad", LocalTouchEventCallback); |
| 24 | |
| 25 | // Initialize the proper target set |
| 26 | targetSet = 0; |
| 27 | SwitchTarget.SetActive(0); |
| 28 | |
| 29 | // Set initial color on models |
| 30 | if(material != null) |
| 31 | material.SetColor("_Color", Color.grey); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | // Update is called once per frame |
| 36 | // Logic for LipSync_Demo |
| 37 | void Update () |
| 38 | { |
| 39 | // Change preset |
| 40 | int preset = -1; |
| 41 | |
| 42 | if(Input.GetKeyDown(KeyCode.Alpha1)) |
| 43 | preset = 0; |
| 44 | else |
| 45 | if(Input.GetKeyDown(KeyCode.Alpha2)) |
| 46 | preset = 1; |
| 47 | else |
| 48 | if(Input.GetKeyDown(KeyCode.Alpha3)) |
| 49 | preset = 2; |
| 50 | else |
| 51 | if(Input.GetKeyDown(KeyCode.Alpha4)) |
| 52 | preset = 3; |
| 53 | else |
| 54 | if(Input.GetKeyDown(KeyCode.Alpha5)) |
| 55 | preset = 4; |
| 56 | else |
| 57 | if(Input.GetKeyDown(KeyCode.Alpha6)) |
| 58 | preset = 5; |
| 59 | else |
| 60 | if(Input.GetKeyDown(KeyCode.Alpha7)) |
| 61 | preset = 6; |
| 62 | else |
| 63 | if(Input.GetKeyDown(KeyCode.Alpha8)) |
| 64 | preset = 7; |
| 65 | else |
| 66 | if(Input.GetKeyDown(KeyCode.Alpha9)) |
| 67 | preset = 8; |
| 68 | else |
| 69 | if(Input.GetKeyDown(KeyCode.Alpha0)) |
| 70 | preset = 9; |
| 71 | |
| 72 | if(preset != -1) |
| 73 | { |
| 74 | Color c = Color.black; |
| 75 | |
| 76 | for (int i = 0; i < contexts.Length; i++) |
| 77 | { |
| 78 | if(contexts[i].SetPreset(preset) == true) |
| 79 | { |
| 80 | // query color from preset and set material color |
| 81 | // of sphere |
| 82 | c = contexts[i].GetPresetColor(preset); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Set the material(s) note: each context is sharing a single |
| 87 | // material in this demo :) |
| 88 | if(material != null) |
| 89 | material.SetColor("_Color", c); |
| 90 | } |
| 91 | |
| 92 | // Update transforms with context average volume |
| 93 | UpdateModelScale(); |
| 94 | |
| 95 | // Change visible target context |
| 96 | if (Input.GetKeyDown(KeyCode.Z)) |
| 97 | { |
| 98 | targetSet = 0; |
| 99 | SetCurrentTarget(); |
| 100 | } |
| 101 | else |
| 102 | if (Input.GetKeyDown(KeyCode.X)) |
| 103 | { |
| 104 | targetSet = 1; |
| 105 | SetCurrentTarget(); |
| 106 | } |
| 107 | |
| 108 | // Close app |
| 109 | if(Input.GetKeyDown (KeyCode.Escape)) |
| 110 | Application.Quit(); |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Sets the current target. |
| 115 | /// </summary> |
| 116 | void SetCurrentTarget() |
| 117 | { |
| 118 | switch(targetSet) |
| 119 | { |
| 120 | case(0): |
| 121 | SwitchTarget.SetActive(0); |
| 122 | OVRDebugConsole.Clear(); |
| 123 | OVRDebugConsole.Log("MICROPHONE INPUT"); |
| 124 | OVRDebugConsole.ClearTimeout(1.5f); |
| 125 | |
| 126 | break; |
| 127 | case(1): |
| 128 | SwitchTarget.SetActive(1); |
| 129 | OVRDebugConsole.Clear(); |
| 130 | OVRDebugConsole.Log("SAMPLE INPUT"); |
| 131 | OVRDebugConsole.ClearTimeout(1.5f); |
| 132 | |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /// <summary> |
| 138 | /// Local touch event callback. |
| 139 | /// </summary> |
| 140 | /// <param name="touchEvent">Touch event.</param> |
| 141 | void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent) |
| 142 | { |
| 143 | switch(touchEvent) |
| 144 | { |
| 145 | case(OVRTouchpad.TouchEvent.Left): |
| 146 | |
| 147 | targetSet--; |
| 148 | if(targetSet < 0) |
| 149 | targetSet = 1; |
| 150 | |
| 151 | SetCurrentTarget(); |
| 152 | |
| 153 | break; |
| 154 | |
| 155 | case(OVRTouchpad.TouchEvent.Right): |
| 156 | |
| 157 | targetSet++; |
| 158 | if(targetSet > 1) |
| 159 | targetSet = 0; |
| 160 | |
| 161 | SetCurrentTarget(); |
| 162 | |
| 163 | break; |
| 164 | |
| 165 | case(OVRTouchpad.TouchEvent.Up): |
| 166 | |
| 167 | if(contexts.Length != 0) |
| 168 | { |
| 169 | if(contexts[0].GetNumPresets() == 0) |
| 170 | { |
| 171 | OVRDebugConsole.Clear(); |
| 172 | OVRDebugConsole.Log("NO PRESETS!"); |
| 173 | OVRDebugConsole.ClearTimeout(1.5f); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | currentPreset++; |
| 178 | if(currentPreset >= contexts[0].GetNumPresets()) |
| 179 | currentPreset = 0; |
| 180 | |
| 181 | Color c = Color.black; |
| 182 | |
| 183 | for (int i = 0; i < contexts.Length; i++) |
| 184 | { |
| 185 | if(contexts[i].SetPreset(currentPreset) == true) |
| 186 | { |
| 187 | // query color from preset and set material color |
| 188 | // of sphere |
| 189 | c = contexts[i].GetPresetColor(currentPreset); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Set the material(s) note: each context is sharing a single |
| 194 | // material in this demo :) |
| 195 | if(material != null) |
| 196 | material.SetColor("_Color", c); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | break; |
| 201 | |
| 202 | case(OVRTouchpad.TouchEvent.Down): |
| 203 | if(contexts.Length != 0) |
| 204 | { |
| 205 | if(contexts[0].GetNumPresets() == 0) |
| 206 | { |
| 207 | OVRDebugConsole.Clear(); |
| 208 | OVRDebugConsole.Log("NO PRESETS!"); |
| 209 | OVRDebugConsole.ClearTimeout(1.5f); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | currentPreset--; |
| 214 | if(currentPreset < 0) |
| 215 | currentPreset = contexts[0].GetNumPresets() - 1; |
| 216 | |
| 217 | Color c = Color.black; |
| 218 | |
| 219 | for (int i = 0; i < contexts.Length; i++) |
| 220 | { |
| 221 | if(contexts[i].SetPreset(currentPreset) == true) |
| 222 | { |
| 223 | // query color from preset and set material color |
| 224 | // of sphere |
| 225 | c = contexts[i].GetPresetColor(currentPreset); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Set the material(s) note: each context is sharing a single |
| 230 | // material in this demo :) |
| 231 | if(material != null) |
| 232 | material.SetColor("_Color", c); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | break; |
| 237 | |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// <summary> |
| 242 | /// Updates the model scale. |
| 243 | /// </summary> |
| 244 | void UpdateModelScale() |
| 245 | { |
| 246 | for (int i = 0; i < xfrms.Length; i++) |
| 247 | { |
| 248 | if(i < contexts.Length) |
| 249 | { |
| 250 | xfrms[i].localScale = scale * (1.0f + (contexts[i].GetAverageAbsVolume() * scaleMax)); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |