Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
ParametricHapticSource.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using UnityEngine;
7using System.Linq;
10using System;
11
12namespace Interhaptics.Utils
13{
14
15 [Serializable]
16 public class TimeValuePair
17 {
18 public double time;
19 [Range(0f, 1f)]
20 public double value; // For Amplitude or Pitch
21 }
22
23 [Serializable]
25 {
26 public double time;
27 [Range(0f, 1f)]
28 public double amplitude;
29 [Range(0f, 1f)]
30 public double frequency;
31 }
32
33 [AddComponentMenu("Interhaptics/ParametricHapticSource")]
34 public class ParametricHapticSource : Internal.HapticSource
35 {
36#region Public Fields
37 [Header("Amplitude Configuration")]
38 [Tooltip("Enable to add time-amplitude composition.")]
39 public bool useAmplitude;
40
41 [ConditionalHide("useAmplitude", true)]
42 [Tooltip("Pairs of time and amplitude values.")]
44
45 [Header("Pitch Configuration")]
46 [Tooltip("Enable to add time-pitch composition.")]
47 public bool usePitch;
48
49 [ConditionalHide("usePitch", true)]
50 [Tooltip("Pairs of time and pitch values.")]
52
53 [Header("Transient Configuration")]
54 [Tooltip("Enable to add transient composition.")]
55 public bool useTransients;
56
57 [ConditionalHide("useTransients", true)]
58 [Tooltip("Triplets of time, amplitude, and frequency values for transients.")]
60 //Resulting Amplitude Array
61 [HideInInspector]
62 private double[] amplitude;
63//Resulting Pitch Array
64 [HideInInspector]
65 private double[] pitch;
66 [Tooltip("Minimum value for the frequency range. Default 65")]
67 [SerializeField]
68 private double freqMin = 65.0;
69 [Tooltip("Maximum value for the frequency range. Default 300")]
70 [SerializeField]
71 private double freqMax = 300.0;
72//Resulting Transient Array
73 [HideInInspector]
74 [SerializeField]
75 private double[] transients;
77#endregion
78
79#region Lifecycle
80 protected override void Awake()
81 {
82 // Custom initialization for ParametricHapticSource. Bypasses adding a haptic effect
83#if UNITY_ANDROID
84 if (HapticManager.MonoScriptingBackend)
85 {
86 DebugMode(ERROR_MESSAGE_MONO + "Haptic Source");
87 return;
88 }
89 else
90 {
91 DebugMode("IL2CPP Haptic Source");
92 }
93#endif
94 }
95 protected override void Start()
96 {
97 // Initialize the parametric haptic effect with the vector data
99 base.Start();
100 }
101
102 private void OnDestroy()
103 {
104 // Optional: Cleanup the haptic effect
105 //HAR.ClearEvent(HapticMaterialId);
106 }
107 #endregion
108
110 {
111 bool initializationSuccess = true;
112
113 // Convert the custom class data to arrays suitable for AddParametricEffect
114 amplitude = useAmplitude ? timeAmplitudePairs.SelectMany(pair => new double[] { pair.time, pair.value }).ToArray() : null;
115 pitch = usePitch ? timePitchPairs.SelectMany(pair => new double[] { pair.time, pair.value }).ToArray() : null;
116 transients = useTransients ? timeAmplitudeFrequencyTriplets.SelectMany(triplet => new double[] { triplet.time, triplet.amplitude, triplet.frequency }).ToArray() : null;
117
118 // Now you can call AddParametricEffect with the converted arrays
119 HapticMaterialId = HAR.AddParametricEffect(
120 amplitude, amplitude?.Length ?? 0,
121 pitch, pitch?.Length ?? 0,
122 freqMin, freqMax,
123 transients, transients?.Length ?? 0,
125 );
126
127 // Check if the ID is valid
128 if (HapticMaterialId == -1)
129 {
130 DebugMode("Failed to create parametric haptic effect.");
131 initializationSuccess = false;
132 }
133 else
134 {
135 DebugMode("Parametric haptic effect created with ID: " + HapticMaterialId);
136 }
137 return initializationSuccess;
138 }
139
140 // Overrides the base class Play method to use the haptic effect created on the fly
141 public override void Play()
142 {
143 AddTarget(hapticBodyParts.Select(hapticBodyPart => new CommandData(Operator.Plus, hapticBodyPart.BodyPart, hapticBodyPart.Side)).ToList());
144 base.Play();
145 }
146 public override void Stop()
147 {
148 base.Stop();
149 }
150
151 public override void PlayEventVibration()
152 {
153 AddTarget(hapticBodyParts.Select(hapticBodyPart => new CommandData(Operator.Plus, hapticBodyPart.BodyPart, hapticBodyPart.Side)).ToList());
154 base.PlayEventVibration();
155 }
156
157 }
158}
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.
void AddTarget(List< HapticBodyMapping.CommandData > Target)
Call this method to add a target to the haptic effect.
override void Stop()
Call this method to stop the haptic effect.
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...
override void Awake()
Add the haptic effect file to the when the object is created. The haptic effect file can be in the St...
override void Start()
Initialize the haptic effect settings at the start of the game.
TimeAmplitudeFrequencyTriplet[] timeAmplitudeFrequencyTriplets
override void Play()
Call this method to play the haptic effect.
Operator
Enumeration for operator signs in haptic command data.
Structure for command data in haptic systems.