wangguan
2020-12-29 452c75675679c44cc39b04bdb7d330d7c5c14d5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace MoreMountains.NiceVibrations
{
    /// <summary>
    /// This class lets you output the value corresponding to one of the basic signal types it contains. Useful to draw basic signal curves.
    /// </summary>
    public class MMSignal : MonoBehaviour
    {
        public enum SignalType
        {
            DigitalNoise,
            Pulse,
            Sawtooth,
            Sine,
            Square,
            Triangle,
            WhiteNoise
        }
                
        /// <summary>
        /// Returns the corresponding value based on the selected SignalType for a given time value
        /// </summary>
        /// <param name="time"></param>
        /// <param name="signalType"></param>
        /// <param name="phase"></param>
        /// <param name="amplitude"></param>
        /// <param name="frequency"></param>
        /// <param name="offset"></param>
        /// <param name="Invert"></param>
        /// <returns></returns>
        public static float GetValue(float time, SignalType signalType, float phase, float amplitude, float frequency, float offset, bool Invert = false)
        {
            float value = 0f;
            float invert = Invert ? -1 : 1;
            float t = frequency * time + phase;
 
            switch (signalType)
            { 
                case SignalType.Sine: 
                    value = (float)Mathf.Sin(2f * Mathf.PI * t);
                    break;
                case SignalType.Square:
                    value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t));
                    break;
                case SignalType.Triangle:
                    value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f));
                    break;
                case SignalType.Sawtooth:
                    value = 2f * (t - (float)Mathf.Floor(t + 0.5f));
                    break;
                case SignalType.Pulse: 
                    value = (Mathf.Abs(Mathf.Sin(2 * Mathf.PI * t)) < 1.0 - 10E-3) ? (0) : (1);
                    break;
                case SignalType.WhiteNoise:
                    value = 2f * Random.Range(0,int.MaxValue) / int.MaxValue - 1f;
                    break;
                case SignalType.DigitalNoise:
                    value = Random.Range(0,2);
                    break;
            }
 
            return (invert * amplitude * value + offset);
        }
    }      
}