Browse Source

fixed Spatialization volume

ncannasse 8 năm trước cách đây
mục cha
commit
e59f684f76
3 tập tin đã thay đổi với 15 bổ sung7 xóa
  1. 1 0
      hxd/snd/Channel.hx
  2. 12 0
      hxd/snd/Effect.hx
  3. 2 7
      hxd/snd/effect/Spatialization.hx

+ 1 - 0
hxd/snd/Channel.hx

@@ -74,6 +74,7 @@ class Channel extends ChannelBase {
 		super.updateCurrentVolume(now);
 		channelGroup.updateCurrentVolume(now);
 		currentVolume *= channelGroup.currentVolume * soundGroup.volume;
+		for( e in effects ) currentVolume *= e.getVolumeModifier();
 	}
 
 	public function calcAudibleGain( now : Float ) {

+ 12 - 0
hxd/snd/Effect.hx

@@ -1,6 +1,10 @@
 package hxd.snd;
 
 class Effect {
+
+	/**
+		Audible gain used to estimate wether the channel should be cutoff or not
+	**/
 	public var gain (get, set) : Float;
 
 	function get_gain() return 1.0;
@@ -11,6 +15,14 @@ class Effect {
 
 	function new() { }
 
+
+	/**
+		Actual volume change to be performed on channel.
+	**/
+	public function getVolumeModifier() : Float {
+		return 1;
+	}
+
 	function apply( channel : Channel, source : Driver.Source ) {
 		throw this+" is not supported on this platform";
 	}

+ 2 - 7
hxd/snd/effect/Spatialization.hx

@@ -25,7 +25,8 @@ class Spatialization extends Effect {
 		rollOffFactor =  1.0;
 	}
 
-	function getFadeGain() {
+	override function getVolumeModifier() {
+		if( fadeDistance == null ) return 1.;
 		var dist = Driver.get().listener.position.distance(position);
 		if (maxDistance != null) dist -= maxDistance;
 		else dist -= referenceDistance;
@@ -51,11 +52,6 @@ class Spatialization extends Effect {
 			var md : Float = maxDistance;
 			AL.sourcef(s.inst, AL.MAX_DISTANCE, md);
 		}
-
-		if (fadeDistance != null) {
-			var volume = channel.volume * channel.soundGroup.volume * channel.channelGroup.volume;
-			AL.sourcef(s.inst, AL.GAIN, getFadeGain() * volume);
-		}
 	}
 	#end
 
@@ -64,7 +60,6 @@ class Spatialization extends Effect {
 		dist = Math.max(dist, referenceDistance);
 		if (maxDistance != null) dist = Math.min(dist, maxDistance);
 		var gain = referenceDistance/(referenceDistance + rollOffFactor * (dist - referenceDistance));
-		if (fadeDistance != null) gain *= getFadeGain();
 		return gain;
 	}