Interhaptics SDK for Unity
1.6
Loading...
Searching...
No Matches
Interhaptics
sdk_unity
Interhaptics
Runtime
Utils
ParametricHapticSource.cs
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2023 Go Touch VR SAS. All rights reserved.
3
*
4
*/
5
6
using
UnityEngine;
7
using
System.Linq;
8
using
Interhaptics.HapticBodyMapping
;
9
using
Interhaptics.Core
;
10
using
System;
11
12
namespace
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]
24
public
class
TimeAmplitudeFrequencyTriplet
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."
)]
43
public
TimeValuePair
[]
timeAmplitudePairs
;
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."
)]
51
public
TimeValuePair
[]
timePitchPairs
;
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."
)]
59
public
TimeAmplitudeFrequencyTriplet
[]
timeAmplitudeFrequencyTriplets
;
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;
76
public
HapticBodyPart
[]
hapticBodyParts
;
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
98
InitializeParametricHapticSource
();
99
base.Start();
100
}
101
102
private
void
OnDestroy()
103
{
104
// Optional: Cleanup the haptic effect
105
//HAR.ClearEvent(HapticMaterialId);
106
}
107
#endregion
108
109
public
bool
InitializeParametricHapticSource
()
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,
124
isLooping
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
}
Interhaptics.HapticBodyPart
Definition
HapticBodyPart.cs:15
Interhaptics.Internal.HapticSource.ERROR_MESSAGE_MONO
const string ERROR_MESSAGE_MONO
Definition
HapticSource.cs:64
Interhaptics.Internal.HapticSource.DebugMode
void DebugMode(string debugMessage)
Debug method to print messages in the console only when debugMode is enabled.
Definition
HapticSource.cs:202
Interhaptics.Internal.HapticSource.isLooping
bool isLooping
Definition
HapticSource.cs:41
Interhaptics.Internal.HapticSource.AddTarget
void AddTarget(List< HapticBodyMapping.CommandData > Target)
Call this method to add a target to the haptic effect.
Definition
HapticSource.cs:240
Interhaptics.Internal.HapticSource.HapticMaterialId
int HapticMaterialId
Definition
HapticSource.cs:66
Interhaptics.Utils.ParametricHapticSource
Definition
ParametricHapticSource.cs:35
Interhaptics.Utils.ParametricHapticSource.usePitch
bool usePitch
Definition
ParametricHapticSource.cs:47
Interhaptics.Utils.ParametricHapticSource.Stop
override void Stop()
Call this method to stop the haptic effect.
Definition
ParametricHapticSource.cs:146
Interhaptics.Utils.ParametricHapticSource.timePitchPairs
TimeValuePair[] timePitchPairs
Definition
ParametricHapticSource.cs:51
Interhaptics.Utils.ParametricHapticSource.InitializeParametricHapticSource
bool InitializeParametricHapticSource()
Definition
ParametricHapticSource.cs:109
Interhaptics.Utils.ParametricHapticSource.PlayEventVibration
override void PlayEventVibration()
Method to start the coroutine from outside (if necessary). Plays the haptic effect after the vibratio...
Definition
ParametricHapticSource.cs:151
Interhaptics.Utils.ParametricHapticSource.useTransients
bool useTransients
Definition
ParametricHapticSource.cs:55
Interhaptics.Utils.ParametricHapticSource.Awake
override void Awake()
Add the haptic effect file to the when the object is created. The haptic effect file can be in the St...
Definition
ParametricHapticSource.cs:80
Interhaptics.Utils.ParametricHapticSource.timeAmplitudePairs
TimeValuePair[] timeAmplitudePairs
Definition
ParametricHapticSource.cs:43
Interhaptics.Utils.ParametricHapticSource.Start
override void Start()
Initialize the haptic effect settings at the start of the game.
Definition
ParametricHapticSource.cs:95
Interhaptics.Utils.ParametricHapticSource.useAmplitude
bool useAmplitude
Definition
ParametricHapticSource.cs:39
Interhaptics.Utils.ParametricHapticSource.hapticBodyParts
HapticBodyPart[] hapticBodyParts
Definition
ParametricHapticSource.cs:76
Interhaptics.Utils.ParametricHapticSource.timeAmplitudeFrequencyTriplets
TimeAmplitudeFrequencyTriplet[] timeAmplitudeFrequencyTriplets
Definition
ParametricHapticSource.cs:59
Interhaptics.Utils.ParametricHapticSource.Play
override void Play()
Call this method to play the haptic effect.
Definition
ParametricHapticSource.cs:141
Interhaptics.Utils.TimeAmplitudeFrequencyTriplet
Definition
ParametricHapticSource.cs:25
Interhaptics.Utils.TimeAmplitudeFrequencyTriplet.frequency
double frequency
Definition
ParametricHapticSource.cs:30
Interhaptics.Utils.TimeAmplitudeFrequencyTriplet.amplitude
double amplitude
Definition
ParametricHapticSource.cs:28
Interhaptics.Utils.TimeAmplitudeFrequencyTriplet.time
double time
Definition
ParametricHapticSource.cs:26
Interhaptics.Utils.TimeValuePair
Definition
ParametricHapticSource.cs:17
Interhaptics.Utils.TimeValuePair.value
double value
Definition
ParametricHapticSource.cs:20
Interhaptics.Utils.TimeValuePair.time
double time
Definition
ParametricHapticSource.cs:18
Interhaptics.Core
Definition
MobileControl.cs:12
Interhaptics.HapticBodyMapping
Definition
BodyMapping.cs:7
Interhaptics.HapticBodyMapping.Operator
Operator
Enumeration for operator signs in haptic command data.
Definition
BodyMapping.cs:119
Interhaptics.Utils
Definition
ConditionalHideAttribute.cs:7
Interhaptics.HapticBodyMapping.CommandData
Structure for command data in haptic systems.
Definition
BodyMapping.cs:189
Generated by
1.11.0