SFX and soundasset safeties and fixe

adress several insatnaces of things like ServerPlay2D et al either not emitting sound at all, or doing so only at scene origin
This commit is contained in:
AzaezelX 2024-02-03 20:16:45 -06:00
parent 66766006d3
commit b12cd00b74
5 changed files with 69 additions and 46 deletions

View file

@ -550,7 +550,12 @@ DefineEngineMethod(SoundAsset, playSound, S32, (Point3F position), (Point3F::Zer
{ {
MatrixF transform; MatrixF transform;
transform.setPosition(position); transform.setPosition(position);
SFXSource* source = SFX->playOnce(object->getSFXTrack(), &transform, NULL, -1); SFXSource* source;
if (position == Point3F::Zero || !object->is3D())
source = SFX->playOnce(object->getSFXTrack());
else
source = SFX->playOnce(object->getSFXTrack(), &transform, NULL, -1);
if(source) if(source)
return source->getId(); return source->getId();
else else

View file

@ -161,7 +161,7 @@ public:
inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; }; inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; };
SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; } SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; }
SFXPlayList* getSfxPlaylist() { return &mPlaylist; } SFXPlayList* getSfxPlaylist() { return &mPlaylist; }
SFXTrack* getSFXTrack() { return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(&mSFXProfile[0]); } SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(&mSFXProfile[0]); }
SFXDescription* getSfxDescription() { return &mProfileDesc; } SFXDescription* getSfxDescription() { return &mProfileDesc; }
bool isPlaylist(){ return mIsPlaylist; } bool isPlaylist(){ return mIsPlaylist; }

View file

@ -1566,7 +1566,11 @@ void GameConnection::play2D(StringTableEntry assetId)
{ {
if (AssetDatabase.isDeclaredAsset(assetId)) if (AssetDatabase.isDeclaredAsset(assetId))
{ {
postNetEvent(new SimSoundAssetEvent(assetId)); AssetPtr<SoundAsset> tempSoundAsset = assetId;
if (tempSoundAsset && tempSoundAsset->is3D())
{
postNetEvent(new SimSoundAssetEvent(assetId, SFX->getListener(0).getTransform()));
}
} }
} }

View file

@ -301,9 +301,12 @@ SimSoundAssetEvent::SimSoundAssetEvent(StringTableEntry assetId, const MatrixF&
{ {
// cant get here unless the asset is declared. // cant get here unless the asset is declared.
mAsset = assetId; mAsset = assetId;
mIs3D = false;
if (mat) if (mat)
{
mTransform = mat; mTransform = mat;
mIs3D = true;
}
} }
void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream) void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream)
@ -312,28 +315,32 @@ void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream)
con->packNetStringHandleU(stream, assetIdStr); con->packNetStringHandleU(stream, assetIdStr);
// only stream if this is a 3d sound asset. // only stream if this is a 3d sound asset.
if (mAsset->is3D()) stream->writeFlag(mIs3D);
if (mIs3D)
{ {
SFXDescription* ad = mAsset->getSfxDescription(); if (mAsset->is3D())
if (stream->writeFlag(ad->mConeInsideAngle || ad->mConeOutsideAngle))
{ {
QuatF q(mTransform); SFXDescription* ad = mAsset->getSfxDescription();
q.normalize(); if (stream->writeFlag(ad->mConeInsideAngle || ad->mConeOutsideAngle))
{
QuatF q(mTransform);
q.normalize();
// LH - we can get a valid quat that's very slightly over 1 in and so // LH - we can get a valid quat that's very slightly over 1 in and so
// this fails (barely) check against zero. So use some error- // this fails (barely) check against zero. So use some error-
AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001), AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
"QuatF::normalize() is broken in Sim3DAudioEvent"); "QuatF::normalize() is broken in Sim3DAudioEvent");
stream->writeSignedFloat(q.x, SoundRotBits); stream->writeSignedFloat(q.x, SoundRotBits);
stream->writeSignedFloat(q.y, SoundRotBits); stream->writeSignedFloat(q.y, SoundRotBits);
stream->writeSignedFloat(q.z, SoundRotBits); stream->writeSignedFloat(q.z, SoundRotBits);
stream->writeFlag(q.w < 0.0); stream->writeFlag(q.w < 0.0);
}
Point3F pos;
mTransform.getColumn(3, &pos);
stream->writeCompressedPoint(pos, SoundPosAccuracy);
} }
Point3F pos;
mTransform.getColumn(3, &pos);
stream->writeCompressedPoint(pos, SoundPosAccuracy);
} }
} }
@ -356,38 +363,45 @@ void SimSoundAssetEvent::unpack(NetConnection* con, BitStream* stream)
mAsset = temp; mAsset = temp;
} }
if (mAsset->is3D()) if (stream->readFlag())
{ {
if (stream->readFlag()) { mIs3D = true;
QuatF q; if (mAsset->is3D())
q.x = stream->readSignedFloat(SoundRotBits); {
q.y = stream->readSignedFloat(SoundRotBits); if (stream->readFlag()) {
q.z = stream->readSignedFloat(SoundRotBits); QuatF q;
F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z)); q.x = stream->readSignedFloat(SoundRotBits);
// #ifdef __linux q.y = stream->readSignedFloat(SoundRotBits);
// Hmm, this should never happen, but it does... q.z = stream->readSignedFloat(SoundRotBits);
if (value > 1.f) F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
value = 1.f; // #ifdef __linux
// #endif // Hmm, this should never happen, but it does...
q.w = mSqrt(1.f - value); if (value > 1.f)
if (stream->readFlag()) value = 1.f;
q.w = -q.w; // #endif
q.setMatrix(&mTransform); q.w = mSqrt(1.f - value);
} if (stream->readFlag())
else q.w = -q.w;
mTransform.identity(); q.setMatrix(&mTransform);
}
else
mTransform.identity();
Point3F pos; Point3F pos;
stream->readCompressedPoint(&pos, SoundPosAccuracy); stream->readCompressedPoint(&pos, SoundPosAccuracy);
mTransform.setColumn(3, pos); mTransform.setColumn(3, pos);
}
} }
} }
void SimSoundAssetEvent::process(NetConnection* con) void SimSoundAssetEvent::process(NetConnection* con)
{ {
if (mAsset->is3D()) if (mIs3D)
SFX->playOnce(mAsset->getSFXTrack(), &mTransform); {
if (mAsset->is3D())
SFX->playOnce(mAsset->getSFXTrack(), &mTransform);
}
else else
SFX->playOnce(mAsset->getSFXTrack()); SFX->playOnce(mAsset->getSFXTrack());

View file

@ -110,7 +110,7 @@ class SimSoundAssetEvent : public NetEvent
private: private:
AssetPtr<SoundAsset> mAsset; AssetPtr<SoundAsset> mAsset;
MatrixF mTransform; MatrixF mTransform;
bool mIs3D;
public: public:
typedef NetEvent Parent; typedef NetEvent Parent;