Exemplo n.º 1
0
        /// <summary>
        /// Converts a AHAP string into a MMNRumbleWaveForm
        /// </summary>
        /// <param name="AHAPasString"></param>
        /// <returns></returns>
        public static MMNVRumbleWaveForm AHAPtoRumbleWaveForm(string AHAPasString, float intensityMultiplier, float sharpnessMultiplier)
        {
            MMNVAHAP ahap = JsonConvert.DeserializeObject <MMNVAHAP>(AHAPasString);

            List <long> patterns           = new List <long>();
            List <int>  lowFreqAmplitudes  = new List <int>();
            List <int>  highFreqAmplitudes = new List <int>();

            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);
        }
        /// <summary>
        /// On inspector GUI, we draw an extra button
        /// </summary>
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            MMNVRumbleWaveFormAsset waveformAsset = (MMNVRumbleWaveFormAsset)target;

            DrawDefaultInspector();

            if (waveformAsset.AHAPFile != null)
            {
                if (GUILayout.Button("Import from AHAP"))
                {
                    MMNVRumbleWaveForm tempWaveform = MMNVAHAP.AHAPtoRumbleWaveForm(waveformAsset.AHAPFile.text, waveformAsset.IntensityMultiplier, waveformAsset.SharpnessMultiplier);
                    waveformAsset.WaveForm.Pattern = tempWaveform.Pattern;
                    waveformAsset.WaveForm.LowFrequencyAmplitudes  = tempWaveform.LowFrequencyAmplitudes;
                    waveformAsset.WaveForm.HighFrequencyAmplitudes = tempWaveform.HighFrequencyAmplitudes;
                }
            }

            serializedObject.ApplyModifiedProperties();
        }