forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance3d.js
More file actions
233 lines (202 loc) · 8.55 KB
/
instance3d.js
File metadata and controls
233 lines (202 loc) · 8.55 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { math } from '../math/math.js';
import { Vec3 } from '../math/vec3.js';
import { DISTANCE_EXPONENTIAL, DISTANCE_INVERSE, DISTANCE_LINEAR } from '../audio/constants.js';
import { hasAudioContext } from '../audio/capabilities.js';
import { SoundInstance } from './instance.js';
// default maxDistance, same as Web Audio API
const MAX_DISTANCE = 10000;
/**
* @class
* @name SoundInstance3d
* @augments SoundInstance
* @classdesc A SoundInstance3d plays a {@link Sound} in 3D.
* @param {SoundManager} manager - The sound manager.
* @param {Sound} sound - The sound to play.
* @param {object} options - Options for the instance.
* @param {number} [options.volume=1] - The playback volume, between 0 and 1.
* @param {number} [options.pitch=1] - The relative pitch, default of 1, plays at normal pitch.
* @param {boolean} [options.loop=false] - Whether the sound should loop when it reaches the end or not.
* @param {number} [options.startTime=0] - The time from which the playback will start. Default is 0 to start at the beginning.
* @param {number} [options.duration=null] - The total time after the startTime when playback will stop or restart if loop is true.
* @param {Vec3} [options.position=null] - The position of the sound in 3D space.
* @param {Vec3} [options.velocity=null] - The velocity of the sound.
* @param {string} [options.distanceModel=DISTANCE_LINEAR] - Determines which algorithm to use to reduce the volume of the audio as it moves away from the listener. Can be:
*
* * {@link DISTANCE_LINEAR}
* * {@link DISTANCE_INVERSE}
* * {@link DISTANCE_EXPONENTIAL}
*
* Default is {@link DISTANCE_LINEAR}.
* @param {number} [options.refDistance=1] - The reference distance for reducing volume as the sound source moves further from the listener.
* @param {number} [options.maxDistance=10000] - The maximum distance from the listener at which audio falloff stops. Note the volume of the audio is not 0 after this distance, but just doesn't fall off anymore.
* @param {number} [options.rollOffFactor=1] - The factor used in the falloff equation.
* @property {Vec3} position The position of the sound in 3D space.
* @property {Vec3} velocity The velocity of the sound.
* @property {string} distanceModel Determines which algorithm to use to reduce the volume of the audio as it moves away from the listener. Can be:
*
* * {@link DISTANCE_LINEAR}
* * {@link DISTANCE_INVERSE}
* * {@link DISTANCE_EXPONENTIAL}
*
* Default is {@link DISTANCE_LINEAR}.
* @property {number} refDistance The reference distance for reducing volume as the sound source moves further from the listener.
* @property {number} maxDistance The maximum distance from the listener at which audio falloff stops. Note the volume of the audio is not 0 after this distance, but just doesn't fall off anymore.
* @property {number} rollOffFactor The factor used in the falloff equation.
*/
class SoundInstance3d extends SoundInstance {
constructor(manager, sound, options) {
super(manager, sound, options);
options = options || {};
this._position = new Vec3();
if (options.position)
this.position = options.position;
this._velocity = new Vec3();
if (options.velocity)
this.velocity = options.velocity;
this.maxDistance = options.maxDistance !== undefined ? Number(options.maxDistance) : MAX_DISTANCE;
this.refDistance = options.refDistance !== undefined ? Number(options.refDistance) : 1;
this.rollOffFactor = options.rollOffFactor !== undefined ? Number(options.rollOffFactor) : 1;
this.distanceModel = options.distanceModel !== undefined ? options.distanceModel : DISTANCE_LINEAR;
}
}
if (hasAudioContext()) {
Object.assign(SoundInstance3d.prototype, {
_initializeNodes: function () {
this.gain = this._manager.context.createGain();
this.panner = this._manager.context.createPanner();
this.panner.connect(this.gain);
this._inputNode = this.panner;
this._connectorNode = this.gain;
this._connectorNode.connect(this._manager.context.destination);
}
});
Object.defineProperty(SoundInstance3d.prototype, 'position', {
get: function () {
return this._position;
},
set: function (position) {
this._position.copy(position);
this.panner.setPosition(position.x, position.y, position.z);
}
});
Object.defineProperty(SoundInstance3d.prototype, 'velocity', {
get: function () {
return this._velocity;
},
set: function (velocity) {
this._velocity.copy(velocity);
this.panner.setVelocity(velocity.x, velocity.y, velocity.z);
}
});
Object.defineProperty(SoundInstance3d.prototype, 'maxDistance', {
get: function () {
return this.panner.maxDistance;
},
set: function (value) {
this.panner.maxDistance = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'refDistance', {
get: function () {
return this.panner.refDistance;
},
set: function (value) {
this.panner.refDistance = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'rollOffFactor', {
get: function () {
return this.panner.rolloffFactor;
},
set: function (value) {
this.panner.rolloffFactor = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'distanceModel', {
get: function () {
return this.panner.distanceModel;
},
set: function (value) {
this.panner.distanceModel = value;
}
});
} else {
// temp vector storage
let offset = new Vec3();
// Fall off function which should be the same as the one in the Web Audio API
// Taken from https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel
const fallOff = function (posOne, posTwo, refDistance, maxDistance, rollOffFactor, distanceModel) {
offset = offset.sub2(posOne, posTwo);
const distance = offset.length();
if (distance < refDistance) {
return 1;
} else if (distance > maxDistance) {
return 0;
}
let result = 0;
if (distanceModel === DISTANCE_LINEAR) {
result = 1 - rollOffFactor * (distance - refDistance) / (maxDistance - refDistance);
} else if (distanceModel === DISTANCE_INVERSE) {
result = refDistance / (refDistance + rollOffFactor * (distance - refDistance));
} else if (distanceModel === DISTANCE_EXPONENTIAL) {
result = Math.pow(distance / refDistance, -rollOffFactor);
}
return math.clamp(result, 0, 1);
};
Object.defineProperty(SoundInstance3d.prototype, 'position', {
get: function () {
return this._position;
},
set: function (position) {
this._position.copy(position);
if (this.source) {
const listener = this._manager.listener;
const lpos = listener.getPosition();
const factor = fallOff(lpos, this._position, this.refDistance, this.maxDistance, this.rollOffFactor, this.distanceModel);
const v = this.volume;
this.source.volume = v * factor * this._manager.volume;
}
}
});
Object.defineProperty(SoundInstance3d.prototype, 'velocity', {
get: function () {
return this._velocity;
},
set: function (velocity) {
this._velocity.copy(velocity);
}
});
Object.defineProperty(SoundInstance3d.prototype, 'maxDistance', {
get: function () {
return this._maxDistance;
},
set: function (value) {
this._maxDistance = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'refDistance', {
get: function () {
return this._refDistance;
},
set: function (value) {
this._refDistance = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'rollOffFactor', {
get: function () {
return this._rollOffFactor;
},
set: function (value) {
this._rollOffFactor = value;
}
});
Object.defineProperty(SoundInstance3d.prototype, 'distanceModel', {
get: function () {
return this._distanceModel;
},
set: function (value) {
this._distanceModel = value;
}
});
}
export { SoundInstance3d };