从XNA迁移至MonoGame后2D游戏3D音频失效求助
移植XNA 4.0到MonoGame 3.7.1 DesktopGL后3D音频空间定位失效(所有声音来自中心)
将2D游戏从XNA 4.0移植到MonoGame 3.7.1(DesktopGL平台,基于.NET Framework 4.8)后,出现3D音频空间定位失效问题:XNA版本中可正常体现声音的空间位置,但MonoGame版本里所有声音均似乎来自中心位置。
相关代码在两个版本中完全一致:
public void setSound(SoundEffectInstance sound, object owner, AudioListener listener, AudioEmitter emitter, double startTime, bool looping = false, float volumeModifier = 1) { currentSound = sound; soundOwner = owner; startTimeOfCurrentSound = startTime; initialVolume = MathHelper.Clamp(AudioSettings.SFX_VOLUME * volumeModifier, 0, 1); targetVolume = initialVolume; transitionStartVolume = initialVolume; currentSound.Volume = targetVolume; currentSound.IsLooped = looping; if (listener != null && emitter != null) { if (Vector3.Distance(listener.Position, emitter.Position) > MIN_DISTANCE_SCREEN_UNITS) { currentSound.Apply3D(listener, emitter); //Console.WriteLine("over threshold! listener = " + listener.Position + ", emitter = " + emitter.Position + ", distance = " + Vector3.Distance(listener.Position, emitter.Position)); } else { Vector3 savedPosition = emitter.Position; emitter.Position = new Vector3(listener.Position.X, listener.Position.Y, ConvertUnits.ToDisplayUnits(5)); currentSound.Apply3D(listener, emitter); //Console.WriteLine("under threshold! listener = " + listener.Position + ", emitter = " + emitter.Position + ", distance = " + Vector3.Distance(listener.Position, emitter.Position)); emitter.Position = savedPosition; } } currentSound.Play(); }
验证信息
- 代码中的if分支逻辑正常,仅在符合条件时进入else分支(最小距离阈值内扁平化声音)
- 通过调试器和控制台输出验证了传入的listener、emitter位置及距离数值均符合预期,示例输出如下:
SFX to be played: audio/sfx/environment/sfx_enemy_alert over threshold! listener = {X:19 Y:182,8 Z:0}, emitter = {X:115,5864 Y:147,08 Z:0}, distance = 102,9799 SFX to be played: audio/sfx/wpn/sfx_gun_shot_001 under threshold! listener = {X:19 Y:148,08 Z:0}, emitter = {X:19 Y:148,08 Z:80}, distance = 80 SFX to be played: audio/sfx/characters/generic/sfx_bullethit over threshold! listener = {X:19 Y:160,3001 Z:0}, emitter = {X:115,5864 Y:147,08 Z:0}, distance = 97,48696 SFX to be played: audio/sfx/wpn/sfx_gun_shot_002 over threshold! listener = {X:19 Y:195,0798 Z:0}, emitter = {X:104,25 Y:140 Z:0}, distance = 101,4956
已尝试的解决措施
- 设置
SoundEffect.DistanceScale为100,同时测试了0.00001到100000之间的多个值,均无改善 - 确认音效文件为单声道(1通道48kHz的.ogg格式),符合MonoGame对3D音频的要求(XNA无此限制)
- 网传距离超过100单位可能存在缩放问题,尝试缩小所有位置数值,无变化
恳请各位提供解决思路或方案,感谢!
内容的提问来源于stack exchange,提问作者MrJ
相关产品推荐
相关产品推荐

