Procedural Thermometer
2023-04-21
I couldn't find any tutorials on this, so this is how I made
a simple
programmatically
controlled thermometer in Unreal Engine 5.
Demo video
1. Material Parameter Collection
First make a new
Material Parameter Collection asset
Add a new scalar parameter named "Temp" to this new asset.
2. Material
Then make a new material with the following nodes:
The `Temp` parameter is a Material Parameter Collection, pointing to the MPC we created in the previous
step.
This allows us to dynamically change this value by changing the `Temp` variable in the MPC at runtime.
As `Temp` fluctuates from [0.0, 1.0] the bar will change from 0 - 100% full
// Demo changing thermometer at runtime
void AThermometer::Tick(float DeltaTime)
{
float lerpDuration = 2.0f;
if (lerpTimeElapsed < lerpDuration)
{
// pci is the MPC we made
if (pci)
{
// Convert the [0, 100] temp to [0.0 1.0]:
float matTemp = CurrentTemp / 100.0f;
pci->SetScalarParameterValue(FName("Temp"), FMath::Lerp(startLerpTemp, matTemp, lerpTimeElapsed / lerpDuration));
lerpTimeElapsed += DeltaTime;
}
}
}
NOTE: You could create a Dynamic Material Instance instead of using a Material Parameter
Collection here but that is more work.
Finally, use this Material in your static mesh thermometer asset's thermometer tube material slot.