-
Notifications
You must be signed in to change notification settings - Fork 335
/
Spatialization.hx
41 lines (35 loc) · 1.25 KB
/
Spatialization.hx
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
package hxd.snd.effect;
class Spatialization extends hxd.snd.Effect {
public var position : h3d.Vector;
public var velocity : h3d.Vector;
public var direction : h3d.Vector;
public var referenceDistance : Float;
public var maxDistance : Null<Float>;
public var fadeDistance : Null<Float>;
public var rollOffFactor : Float;
public function new() {
super("spatialization");
position = new h3d.Vector();
velocity = new h3d.Vector();
direction = new h3d.Vector();
referenceDistance = 1.0;
rollOffFactor = 1.0;
}
override function getVolumeModifier() {
if( fadeDistance == null ) return 1.;
var dist = Manager.get().listener.position.distance(position);
if (maxDistance != null) dist -= maxDistance;
else dist -= referenceDistance;
var volume = 1 - dist / fadeDistance;
if (volume > 1) volume = 1;
if (volume < 0) volume = 0;
return volume;
}
override function applyAudibleVolumeModifier(v : Float) {
var dist = Manager.get().listener.position.distance(position);
dist = Math.max(dist, referenceDistance);
if (maxDistance != null) dist = Math.min(dist, maxDistance);
var volume = referenceDistance/(referenceDistance + rollOffFactor * (dist - referenceDistance));
return v * volume;
}
}