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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Events;
 
namespace MoreMountains.NiceVibrations
{
    public class HapticCurve : MonoBehaviour
    {
        [Range(0f, 1f)]
        public float Intensity = 1f;
        [Range(0f, 1f)]
        public float Sharpness = 0f;
        public int PointsCount = 50;
        public float AmplitudeFactor = 3;
        public int Period = 4;
        public RectTransform StartPoint;
        public RectTransform EndPoint;
 
        [Header("Movement")]
        public bool Move = false;
        public float MovementSpeed = 1f;
 
        protected LineRenderer _targetLineRenderer;
        protected List<Vector3> Points;
 
        protected Canvas _canvas;
        protected Camera _camera;
 
        protected Vector3 _startPosition;
        protected Vector3 _endPosition;
        protected Vector3 _workPoint;
        
        protected virtual void Awake()
        {
            Initialization();
        }
 
        protected virtual void Initialization()
        {
            Points = new List<Vector3>();
            _canvas = this.gameObject.GetComponentInParent<Canvas>();
            _targetLineRenderer = this.gameObject.GetComponent<LineRenderer>();
            _camera = _canvas.worldCamera;
            DrawCurve();
        }
 
        protected virtual void DrawCurve()
        {
            _startPosition = StartPoint.transform.position;
            _startPosition.z -= 0.1f;
            _endPosition = EndPoint.transform.position;
            _endPosition.z -= 0.1f;
 
            Points.Clear();
            
            for (int i = 0; i < PointsCount; i++)
            {
                float t = NiceVibrationsDemoHelpers.Remap(i, 0, PointsCount, 0f, 1f);
                float sinValue = MMSignal.GetValue(t, MMSignal.SignalType.Sine, 1f, AmplitudeFactor, Period, 0f, false);
                float triangleValue = MMSignal.GetValue(t, MMSignal.SignalType.Triangle, 1f, AmplitudeFactor, Period, 0f, false);
 
                if (Move)
                {                                        
                    sinValue = MMSignal.GetValue(t + Time.time * MovementSpeed, MMSignal.SignalType.Sine, 1f, AmplitudeFactor, Period, 0f, false);
                    triangleValue = MMSignal.GetValue(t + Time.time * MovementSpeed, MMSignal.SignalType.Triangle, 1f, AmplitudeFactor, Period, 0f, false);
                }
 
                float finalValue = Mathf.Lerp(sinValue, triangleValue, Sharpness);
 
 
                _workPoint.x = Mathf.Lerp(_startPosition.x, _endPosition.x, t);
                _workPoint.y = finalValue * Intensity + _startPosition.y;
                _workPoint.z = _startPosition.z;
                Points.Add(_workPoint);
            }
 
            _targetLineRenderer.positionCount = PointsCount ;
            _targetLineRenderer.SetPositions(Points.ToArray());
        }
 
        protected virtual void Update()
        {
            UpdateCurve(Intensity, Sharpness);
        }
 
        public virtual void UpdateCurve(float intensity, float sharpness)
        {
            Intensity = intensity;
            Sharpness = sharpness;
            DrawCurve();
        }
    }
}