From b12cd00b74f6d6cbd43cf4622c2a3244553f070c Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Sat, 3 Feb 2024 20:16:45 -0600 Subject: [PATCH] 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 --- Engine/source/T3D/assets/SoundAsset.cpp | 7 +- Engine/source/T3D/assets/SoundAsset.h | 2 +- Engine/source/T3D/gameBase/gameConnection.cpp | 6 +- .../T3D/gameBase/gameConnectionEvents.cpp | 98 +++++++++++-------- .../T3D/gameBase/gameConnectionEvents.h | 2 +- 5 files changed, 69 insertions(+), 46 deletions(-) diff --git a/Engine/source/T3D/assets/SoundAsset.cpp b/Engine/source/T3D/assets/SoundAsset.cpp index d9aeb7f4a..e182bf9bd 100644 --- a/Engine/source/T3D/assets/SoundAsset.cpp +++ b/Engine/source/T3D/assets/SoundAsset.cpp @@ -550,7 +550,12 @@ DefineEngineMethod(SoundAsset, playSound, S32, (Point3F position), (Point3F::Zer { MatrixF transform; 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) return source->getId(); else diff --git a/Engine/source/T3D/assets/SoundAsset.h b/Engine/source/T3D/assets/SoundAsset.h index 9aaf51224..5e2ecb3e7 100644 --- a/Engine/source/T3D/assets/SoundAsset.h +++ b/Engine/source/T3D/assets/SoundAsset.h @@ -161,7 +161,7 @@ public: inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; }; SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; } SFXPlayList* getSfxPlaylist() { return &mPlaylist; } - SFXTrack* getSFXTrack() { return mIsPlaylist ? dynamic_cast(&mPlaylist) : dynamic_cast(&mSFXProfile[0]); } + SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast(&mPlaylist) : dynamic_cast(&mSFXProfile[0]); } SFXDescription* getSfxDescription() { return &mProfileDesc; } bool isPlaylist(){ return mIsPlaylist; } diff --git a/Engine/source/T3D/gameBase/gameConnection.cpp b/Engine/source/T3D/gameBase/gameConnection.cpp index 6b19b40b5..c0ef3ffb7 100644 --- a/Engine/source/T3D/gameBase/gameConnection.cpp +++ b/Engine/source/T3D/gameBase/gameConnection.cpp @@ -1566,7 +1566,11 @@ void GameConnection::play2D(StringTableEntry assetId) { if (AssetDatabase.isDeclaredAsset(assetId)) { - postNetEvent(new SimSoundAssetEvent(assetId)); + AssetPtr tempSoundAsset = assetId; + if (tempSoundAsset && tempSoundAsset->is3D()) + { + postNetEvent(new SimSoundAssetEvent(assetId, SFX->getListener(0).getTransform())); + } } } diff --git a/Engine/source/T3D/gameBase/gameConnectionEvents.cpp b/Engine/source/T3D/gameBase/gameConnectionEvents.cpp index a24a80c87..0aa21ea5d 100644 --- a/Engine/source/T3D/gameBase/gameConnectionEvents.cpp +++ b/Engine/source/T3D/gameBase/gameConnectionEvents.cpp @@ -301,9 +301,12 @@ SimSoundAssetEvent::SimSoundAssetEvent(StringTableEntry assetId, const MatrixF& { // cant get here unless the asset is declared. mAsset = assetId; - + mIs3D = false; if (mat) + { mTransform = mat; + mIs3D = true; + } } void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream) @@ -312,28 +315,32 @@ void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream) con->packNetStringHandleU(stream, assetIdStr); // only stream if this is a 3d sound asset. - if (mAsset->is3D()) + stream->writeFlag(mIs3D); + if (mIs3D) { - SFXDescription* ad = mAsset->getSfxDescription(); - if (stream->writeFlag(ad->mConeInsideAngle || ad->mConeOutsideAngle)) + if (mAsset->is3D()) { - QuatF q(mTransform); - q.normalize(); + SFXDescription* ad = mAsset->getSfxDescription(); + 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 - // 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), - "QuatF::normalize() is broken in Sim3DAudioEvent"); + // 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- + 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"); - stream->writeSignedFloat(q.x, SoundRotBits); - stream->writeSignedFloat(q.y, SoundRotBits); - stream->writeSignedFloat(q.z, SoundRotBits); - stream->writeFlag(q.w < 0.0); + stream->writeSignedFloat(q.x, SoundRotBits); + stream->writeSignedFloat(q.y, SoundRotBits); + stream->writeSignedFloat(q.z, SoundRotBits); + 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; } - if (mAsset->is3D()) + if (stream->readFlag()) { - if (stream->readFlag()) { - QuatF q; - q.x = stream->readSignedFloat(SoundRotBits); - q.y = stream->readSignedFloat(SoundRotBits); - q.z = stream->readSignedFloat(SoundRotBits); - F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z)); - // #ifdef __linux - // Hmm, this should never happen, but it does... - if (value > 1.f) - value = 1.f; - // #endif - q.w = mSqrt(1.f - value); - if (stream->readFlag()) - q.w = -q.w; - q.setMatrix(&mTransform); - } - else - mTransform.identity(); + mIs3D = true; + if (mAsset->is3D()) + { + if (stream->readFlag()) { + QuatF q; + q.x = stream->readSignedFloat(SoundRotBits); + q.y = stream->readSignedFloat(SoundRotBits); + q.z = stream->readSignedFloat(SoundRotBits); + F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z)); + // #ifdef __linux + // Hmm, this should never happen, but it does... + if (value > 1.f) + value = 1.f; + // #endif + q.w = mSqrt(1.f - value); + if (stream->readFlag()) + q.w = -q.w; + q.setMatrix(&mTransform); + } + else + mTransform.identity(); - Point3F pos; - stream->readCompressedPoint(&pos, SoundPosAccuracy); - mTransform.setColumn(3, pos); + Point3F pos; + stream->readCompressedPoint(&pos, SoundPosAccuracy); + mTransform.setColumn(3, pos); + } } } void SimSoundAssetEvent::process(NetConnection* con) { - if (mAsset->is3D()) - SFX->playOnce(mAsset->getSFXTrack(), &mTransform); + if (mIs3D) + { + if (mAsset->is3D()) + SFX->playOnce(mAsset->getSFXTrack(), &mTransform); + } else SFX->playOnce(mAsset->getSFXTrack()); diff --git a/Engine/source/T3D/gameBase/gameConnectionEvents.h b/Engine/source/T3D/gameBase/gameConnectionEvents.h index 755e02957..6bf9b8601 100644 --- a/Engine/source/T3D/gameBase/gameConnectionEvents.h +++ b/Engine/source/T3D/gameBase/gameConnectionEvents.h @@ -110,7 +110,7 @@ class SimSoundAssetEvent : public NetEvent private: AssetPtr mAsset; MatrixF mTransform; - + bool mIs3D; public: typedef NetEvent Parent;