Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion docs/weather-system-docs/WeatherVariableRegistration.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,91 @@ public:
};
```

### Array and Vector Types

The weather system supports `std::array` and `std::vector` for complex data structures:

#### Using ArrayVariable for Fixed-Size Arrays

```cpp
void RegisterWeatherVariables() override
{
auto* registry = WeatherVariables::GlobalWeatherRegistry::GetSingleton()
->GetOrCreateFeatureRegistry(GetShortName());

// Array of primitive types (floats)
registry->RegisterVariable(std::make_shared<WeatherVariables::ArrayVariable<float, 8>>(
"weights",
"Weight Values",
"Array of weight coefficients",
&settings.weights,
std::array<float, 8>{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }
// elementLerpFunc optional for float types - uses default std::lerp
));

// Array of complex types with custom interpolation
registry->RegisterVariable(std::make_shared<WeatherVariables::ArrayVariable<ColorProfile, 8>>(
"profiles",
"Color Profiles",
"Array of color profile configurations",
&settings.profiles,
defaultProfiles,
[](const ColorProfile& from, const ColorProfile& to, float factor) {
ColorProfile result;
result.hue = std::lerp(from.hue, to.hue, factor);
result.saturation = std::lerp(from.saturation, to.saturation, factor);
result.brightness = std::lerp(from.brightness, to.brightness, factor);
// interpolate other fields...
return result;
}
));
}
```

#### Using VectorVariable for Dynamic Arrays

```cpp
void RegisterWeatherVariables() override
{
auto* registry = WeatherVariables::GlobalWeatherRegistry::GetSingleton()
->GetOrCreateFeatureRegistry(GetShortName());

// Vector of floats with default interpolation
registry->RegisterVariable(std::make_shared<WeatherVariables::VectorVariable<float>>(
"coefficients",
"Dynamic Coefficients",
"Variable-length coefficient array",
&settings.coefficients,
std::vector<float>{ 1.0f, 0.5f, 0.25f }
));

// Vector of complex types with custom interpolation
registry->RegisterVariable(std::make_shared<WeatherVariables::VectorVariable<LightConfig>>(
"lights",
"Light Configurations",
"Dynamic array of light settings",
&settings.lights,
defaultLights,
[](const LightConfig& from, const LightConfig& to, float factor) {
LightConfig result;
result.intensity = std::lerp(from.intensity, to.intensity, factor);
result.color = float3{
std::lerp(from.color.x, to.color.x, factor),
std::lerp(from.color.y, to.color.y, factor),
std::lerp(from.color.z, to.color.z, factor)
};
return result;
}
));
}
```

**Notes on Vector Interpolation:**

- When vectors have different sizes, interpolation uses the maximum size
- Missing elements from shorter vectors are default-initialized (T{})
- This allows smooth transitions when adding/removing elements between weathers

## System Integration

### How Weather Manager Uses the Registry
Expand Down Expand Up @@ -236,7 +321,25 @@ Uses nlohmann::json for type conversion. Built-in support for:

- Primitive types (float, int, bool)
- float2, float3, float4 (see `Utils/Serialize.h`)
- Custom types require NLOHMANN*DEFINE_TYPE*\* macros
- **std::array<T, N>** - Fixed-size arrays serialized as JSON arrays
- **std::vector<T>** - Dynamic arrays serialized as JSON arrays
- Custom types require NLOHMANN*DEFINE_TYPE*\* macros or custom serialization functions

Example JSON with array/vector types:

```json
{
"MyFeature": {
"intensity": 1.5,
"weights": [1.0, 0.8, 0.6, 0.4, 0.2, 0.1, 0.05, 0.025],
"coefficients": [1.0, 0.5, 0.25],
"profiles": [
{ "hue": 0.5, "saturation": 1.0, "brightness": 1.0 },
{ "hue": 0.7, "saturation": 0.8, "brightness": 0.9 }
]
}
}
```

### Error Handling

Expand Down
84 changes: 84 additions & 0 deletions src/WeatherVariableRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
using json = nlohmann::json;

// Weather variable system - features register variables which are automatically handled by weather system
// Supports primitive types (float, float3, float4), std::array, and std::vector
//
// Usage examples:
// - FloatVariable: Single float values with min/max bounds
// - Float3Variable/Float4Variable: Vector types (colors, positions, etc.)
// - ArrayVariable: Fixed-size arrays (e.g., std::array<ColorProfile, 8>)
// - VectorVariable: Dynamic arrays (e.g., std::vector<float>)
//
// Custom element types require providing an element lerp function:
// auto arrayVar = std::make_shared<ArrayVariable<ColorProfile, 8>>(
// "profiles", "Color Profiles", "Array of color profiles",
// &settings.profiles, defaultProfiles,
// [](const ColorProfile& from, const ColorProfile& to, float factor) {
// ColorProfile result;
// // Implement per-field interpolation
// return result;
// });
namespace WeatherVariables
{
// Base class for weather-controllable variables
Expand Down Expand Up @@ -169,6 +186,73 @@ namespace WeatherVariables
}
};

// Specialized weather variable for std::array types
template <typename T, size_t N>
class ArrayVariable : public WeatherVariable<std::array<T, N>>
{
public:
ArrayVariable(const std::string& name, const std::string& displayName, const std::string& tooltip,
std::array<T, N>* valuePtr, std::array<T, N> defaultValue,
std::function<T(const T&, const T&, float)> elementLerpFunc) :
WeatherVariable<std::array<T, N>>(name, displayName, tooltip, valuePtr, defaultValue,
[elementLerpFunc](const std::array<T, N>& from, const std::array<T, N>& to, float factor) {
std::array<T, N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = elementLerpFunc(from[i], to[i], factor);
}
return result;
})
{
}

// Helper constructor for float element types with default lerp
template <typename U = T, typename = std::enable_if_t<std::is_floating_point_v<U>>>
ArrayVariable(const std::string& name, const std::string& displayName, const std::string& tooltip,
std::array<T, N>* valuePtr, std::array<T, N> defaultValue) :
ArrayVariable(name, displayName, tooltip, valuePtr, defaultValue,
[](const T& from, const T& to, float factor) {
return static_cast<T>(std::lerp(from, to, factor));
})
{
}
};

// Specialized weather variable for std::vector types
template <typename T>
class VectorVariable : public WeatherVariable<std::vector<T>>
{
public:
VectorVariable(const std::string& name, const std::string& displayName, const std::string& tooltip,
std::vector<T>* valuePtr, std::vector<T> defaultValue,
std::function<T(const T&, const T&, float)> elementLerpFunc) :
WeatherVariable<std::vector<T>>(name, displayName, tooltip, valuePtr, defaultValue,
[elementLerpFunc](const std::vector<T>& from, const std::vector<T>& to, float factor) {
size_t maxSize = std::max(from.size(), to.size());
std::vector<T> result;
result.reserve(maxSize);

for (size_t i = 0; i < maxSize; ++i) {
T fromVal = (i < from.size()) ? from[i] : T{};
T toVal = (i < to.size()) ? to[i] : T{};
result.push_back(elementLerpFunc(fromVal, toVal, factor));
}
return result;
})
{
}

// Helper constructor for float element types with default lerp
template <typename U = T, typename = std::enable_if_t<std::is_floating_point_v<U>>>
VectorVariable(const std::string& name, const std::string& displayName, const std::string& tooltip,
std::vector<T>* valuePtr, std::vector<T> defaultValue) :
VectorVariable(name, displayName, tooltip, valuePtr, defaultValue,
[](const T& from, const T& to, float factor) {
return static_cast<T>(std::lerp(from, to, factor));
})
{
}
};

// Registry for a feature's weather variables
class FeatureWeatherRegistry
{
Expand Down