Skip to content

Commit

Permalink
🌊 Fixed HydraX caustics not running on sim. time
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Dec 26, 2024
1 parent 6174628 commit f5aa749
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions source/main/gfx/hydrax/Hydrax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ namespace Hydrax
{
if (mCreated && mModule && mVisible)
{
mMaterialManager->updateAnimatedTextures(timeSinceLastFrame);
mModule->update(timeSinceLastFrame);
mDecalsManager->update();
_checkUnderwater(timeSinceLastFrame);
Expand Down
47 changes: 39 additions & 8 deletions source/main/gfx/hydrax/MaterialManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions source/main/gfx/hydrax/MaterialManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -373,6 +377,11 @@ namespace Hydrax
UnderwaterCompositorListener mUnderwaterCompositorListener;
/// Hydrax main pointer
Hydrax *mHydrax;
/// Caustics animated texture, for manual updating.
std::vector<Ogre::TextureUnitState*> mCausticsAnimTexVec;
unsigned int mCausticsAnimCurrentFrame = 0;
/// Time spent on current animation frame, cumulative.
float mCausticsAnimCurrentFrameElapsedTime = 0.f;
};
};

Expand Down

0 comments on commit f5aa749

Please sign in to comment.