using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using Newtonsoft.Json; #endif namespace MoreMountains.NiceVibrations { /// /// A class to store AHAP metadata /// [System.Serializable] public class MMNVAHAPMetadata { public string Project; public string Created; } /// /// A class to store AHAP event parameters /// [System.Serializable] public class MMNVAHAPEventParameter { public string ParameterID; public int ParameterValue; } /// /// A class to store AHAP events /// [System.Serializable] public class MMNVAHAPEvent { public int Time; public string EventType; public double EventDuration; public List EventParameters; } /// /// A class to store AHAP parameter curve control points /// [System.Serializable] public class MMNVAHAPParameterCurveControlPoint { public double Time; public double ParameterValue; } /// /// A class to store AHAP parameter curves /// [System.Serializable] public class MMNVAHAPParameterCurve { public string ParameterID; public double Time; public List ParameterCurveControlPoints; } /// /// A class to store AHAP patterns /// [System.Serializable] public class MMNVAHAPPattern { public MMNVAHAPEvent Event; public MMNVAHAPParameterCurve ParameterCurve; } /// /// A class used to store and manipulate AHAP json contents /// [System.Serializable] public class MMNVAHAP { public float Version; public MMNVAHAPMetadata Metadata; public List Pattern; #if UNITY_EDITOR /// /// Converts a AHAP string into a MMNVAndroidWaveForm /// /// /// public static MMNVAndroidWaveForm AHAPtoAndroidWaveForm(string AHAPasString, float intensityMultiplier, float sharpnessMultiplier) { MMNVAHAP ahap = JsonConvert.DeserializeObject(AHAPasString); List patterns = new List(); List amplitudes = new List(); double totalTimeStamp = 0f; double previousTimeStamp = 0f; foreach (MMNVAHAPPattern pattern in ahap.Pattern) { if (pattern.ParameterCurve != null) { if (pattern.ParameterCurve.ParameterID == "HapticIntensityControl") { foreach (MMNVAHAPParameterCurveControlPoint point in pattern.ParameterCurve.ParameterCurveControlPoints) { double timeStamp = point.Time - previousTimeStamp; totalTimeStamp += timeStamp; patterns.Add((long)(timeStamp * 1000)); float originalIntensity = (float)point.ParameterValue * intensityMultiplier; int intensity = (int)Remap(originalIntensity, 0f, 1f, 0f, 255f); amplitudes.Add(intensity); previousTimeStamp = point.Time; } } } } MMNVAndroidWaveForm returnWaveForm = new MMNVAndroidWaveForm(); returnWaveForm.Amplitudes = amplitudes.ToArray(); returnWaveForm.Pattern = patterns.ToArray(); return returnWaveForm; } /// /// Converts a AHAP string into a MMNRumbleWaveForm /// /// /// public static MMNVRumbleWaveForm AHAPtoRumbleWaveForm(string AHAPasString, float intensityMultiplier, float sharpnessMultiplier) { MMNVAHAP ahap = JsonConvert.DeserializeObject(AHAPasString); List patterns = new List(); List lowFreqAmplitudes = new List(); List highFreqAmplitudes = new List(); double totalTimeStamp = 0f; double previousTimeStamp = 0f; foreach (MMNVAHAPPattern pattern in ahap.Pattern) { if (pattern.ParameterCurve != null) { if (pattern.ParameterCurve.ParameterID == "HapticIntensityControl") { foreach (MMNVAHAPParameterCurveControlPoint point in pattern.ParameterCurve.ParameterCurveControlPoints) { double timeStamp = point.Time - previousTimeStamp; totalTimeStamp += timeStamp; patterns.Add((long)(timeStamp * 1000)); float originalIntensity = (float)point.ParameterValue * intensityMultiplier; int intensity = (int)Remap(originalIntensity, 0f, 1f, 0f, 255f); lowFreqAmplitudes.Add(intensity); previousTimeStamp = point.Time; } } if (pattern.ParameterCurve.ParameterID == "HapticSharpnessControl") { foreach (MMNVAHAPParameterCurveControlPoint point in pattern.ParameterCurve.ParameterCurveControlPoints) { double timeStamp = point.Time - previousTimeStamp; totalTimeStamp += timeStamp; patterns.Add((long)(timeStamp * 1000)); float originalIntensity = (float)point.ParameterValue * sharpnessMultiplier; int intensity = (int)Remap(originalIntensity, 0f, 1f, 0f, 255f); highFreqAmplitudes.Add(intensity); previousTimeStamp = point.Time; } } } } MMNVRumbleWaveForm returnWaveForm = new MMNVRumbleWaveForm(); returnWaveForm.LowFrequencyAmplitudes = lowFreqAmplitudes.ToArray(); returnWaveForm.HighFrequencyAmplitudes = highFreqAmplitudes.ToArray(); returnWaveForm.Pattern = patterns.ToArray(); return returnWaveForm; } #endif /// /// Remaps value x between AB to CD /// /// /// /// /// /// /// public static float Remap(float x, float A, float B, float C, float D) { float remappedValue = C + (x - A) / (B - A) * (D - C); return remappedValue; } } }