From c72d1f41062546c153fa21d38b67c08558a9ec49 Mon Sep 17 00:00:00 2001 From: ohlidalp Date: Thu, 26 Dec 2024 02:01:03 +0100 Subject: [PATCH] :ocean: Fixed HydraX caustics not running on sim. time --- source/main/gfx/hydrax/Hydrax.cpp | 1 + source/main/gfx/hydrax/MaterialManager.cpp | 47 ++++++++++++++++++---- source/main/gfx/hydrax/MaterialManager.h | 9 +++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/source/main/gfx/hydrax/Hydrax.cpp b/source/main/gfx/hydrax/Hydrax.cpp index 3374793439..cc2b0a09f7 100644 --- a/source/main/gfx/hydrax/Hydrax.cpp +++ b/source/main/gfx/hydrax/Hydrax.cpp @@ -292,6 +292,7 @@ namespace Hydrax { if (mCreated && mModule && mVisible) { + mMaterialManager->updateAnimatedTextures(timeSinceLastFrame); mModule->update(timeSinceLastFrame); mDecalsManager->update(); _checkUnderwater(timeSinceLastFrame); diff --git a/source/main/gfx/hydrax/MaterialManager.cpp b/source/main/gfx/hydrax/MaterialManager.cpp index 5998cd9c7d..2135c22bd1 100644 --- a/source/main/gfx/hydrax/MaterialManager.cpp +++ b/source/main/gfx/hydrax/MaterialManager.cpp @@ -1528,9 +1528,11 @@ namespace Hydrax { FP_Parameters->setNamedConstant("uCaustics", 0); } - Ogre::TextureUnitState *TUS_Caustics = DM_Technique0_Pass0->createTextureUnitState("Caustics.bmp"); - TUS_Caustics->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP); - TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32, 1.5); + Ogre::TextureUnitState* TUS_Caustics = DM_Technique0_Pass0->createTextureUnitState("Caustics.bmp"); + TUS_Caustics->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP); + // To account for variable sim. time, we must update animation manually. + TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32); + mCausticsAnimTexVec.push_back(TUS_Caustics); } DepthMaterial->setReceiveShadows(false); @@ -1539,6 +1541,31 @@ namespace Hydrax return true; } + const float CAUSTICS_FRAME_DURATION = 1.5f / 32.f; // 1.5sec is the original hardcoded total duration. + const unsigned int CAUSTICS_NUM_FRAMES = 32; + + void MaterialManager::updateAnimatedTextures(float dt) + { + mCausticsAnimCurrentFrameElapsedTime += dt; + while (mCausticsAnimCurrentFrameElapsedTime > CAUSTICS_FRAME_DURATION) + { + // advance anim frame + mCausticsAnimCurrentFrame++; + if (mCausticsAnimCurrentFrame >= CAUSTICS_NUM_FRAMES) + { + mCausticsAnimCurrentFrame = 0; + } + // update time + mCausticsAnimCurrentFrameElapsedTime -= CAUSTICS_FRAME_DURATION; + } + + // Update frame on registered anims + for (Ogre::TextureUnitState* tus : mCausticsAnimTexVec) + { + tus->setCurrentFrame(mCausticsAnimCurrentFrame); + } + } + bool MaterialManager::_createDepthTextureGPUPrograms(const HydraxComponent &Components, const Options &Options, const Ogre::String& AlphaChannel) { const bool cCaustics = _isComponent(Components, HYDRAX_COMPONENT_CAUSTICS); @@ -3409,10 +3436,12 @@ namespace Hydrax { FP_Parameters->setNamedConstant("uCaustics", 0); } - Ogre::TextureUnitState *TUS_Caustics = DM_Technique_Pass0->createTextureUnitState("Caustics.bmp"); - TUS_Caustics->setName("Caustics"); - TUS_Caustics->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP); - TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32, 1.5); + Ogre::TextureUnitState* TUS_Caustics = DM_Technique_Pass0->createTextureUnitState("Caustics.bmp"); + TUS_Caustics->setName("Caustics"); + TUS_Caustics->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP); + // To account for variable sim. time, we must update animation manually. + TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32); + mCausticsAnimTexVec.push_back(TUS_Caustics); } if (AutoUpdate) @@ -3472,7 +3501,9 @@ namespace Hydrax Ogre::TextureUnitState *TUS_Caustics = DM_Technique_Pass0->createTextureUnitState("Caustics.bmp"); TUS_Caustics->setName("Caustics"); TUS_Caustics->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP); - TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32, 1.5); + // To account for variable sim. time, we must update animation manually. + TUS_Caustics->setAnimatedTextureName("Caustics.bmp", 32); + mCausticsAnimTexVec.push_back(TUS_Caustics); } if(mOptions.SM == SM_GLSL) diff --git a/source/main/gfx/hydrax/MaterialManager.h b/source/main/gfx/hydrax/MaterialManager.h index 1c272ecbdf..d747a469ef 100644 --- a/source/main/gfx/hydrax/MaterialManager.h +++ b/source/main/gfx/hydrax/MaterialManager.h @@ -311,6 +311,10 @@ namespace Hydrax */ void setGpuProgramParameter(const GpuProgram &GpuP, const MaterialType &MType, const Ogre::String &Name, const Ogre::Vector3 &Value); + /** Animated textures must be updated manually to account for variable simulation time. + */ + void updateAnimatedTextures(float dt); + private: /** Is component in the given list? @param List Components list @@ -373,6 +377,11 @@ namespace Hydrax UnderwaterCompositorListener mUnderwaterCompositorListener; /// Hydrax main pointer Hydrax *mHydrax; + /// Caustics animated texture, for manual updating. + std::vector mCausticsAnimTexVec; + unsigned int mCausticsAnimCurrentFrame = 0; + /// Time spent on current animation frame, cumulative. + float mCausticsAnimCurrentFrameElapsedTime = 0.f; }; };