What is the expected behavior when I detach sound from a mesh?

I see that attaching a sound to a mesh automatically makes it spatial. But once that’s happened, how can I make it unspatialized? I’ve tried detaching the mesh and also updating the sound options, like in this playground, but the audio is still spatialized and it audio.spatialSound logs as true. Is this the expected behavior, and is there any other way to make an audio turn from spatialized to NOT spatialized after it has been attached to a mesh? Might be one for @docEdub

PGDemo | Babylon.js Playground (babylonjs.com)

music.spatialSound = false should do it, as there is a setter for the spatialSound property.

This looks like a bug to me.

Setting the spatialSound property to false updates the underlying flag, but it does not un-spatialize the sound. It remains connected to the private panner node, and I do not see a way to disconnect it without accessing private members.

I’ll fix it.

3 Likes

Thanks for reporting this!

It should be fixed with PR 14048.

2 Likes

Thanks @docEdub ! And what would happen if I turned off spatialSound while the sound was attached to a mesh?

The mesh stays attached when spatialSound is turned off, but you won’t hear it moving with the mesh anymore because the sound panner node is disconnected and set to null, so when the attached mesh fires the _onRegisterAfterWorldMatrixUpdate callback, the logic that sets the sound panner node’s position and orientation gets skipped.

There’s is still some CPU used in the callback, though, until detachFromMesh is called to unregister it.

We could make it so the mesh is detached automatically when spatialSound is set to false. Would you expect the previously attached mesh to be re-attached when setting spatialSound back to true?

That’s a good question @docEdub , not entirely sure. I think the way you’ve done it (mesh stays attached, but it’s sound isn’t spatial) probably covers all the base. And rather than attach/reattach the sound, we’ll just use spatialSound property. Am I understanding that right?

1 Like

Yes, the way it is now, you do not need to detach and reattach the mesh when toggling the spatialSound property. An attached mesh will stay attached regardless and should work correctly if spatialSound is set to false and then reset to true.

1 Like