Environment settings become scene entities (UE Skybox/SkyLight semantics)

Skybox/SkyLight are now Actor-carried components instead of resident
global settings, matching UE where SkyAtmosphere/SkyLight/fog are
Actors added from Place Actors and configured in Details.

- New SkyboxComponent (active + envPath asset slot) and SkyLightComponent
  (active + ambientColor); first-active entity wins per frame
- SceneRenderer collects env from components into FrameSceneData
  (DrawSkybox/envPath/ambient); Renderer performs delta sync - env
  reloads only when the resolved path changes, neutral fallback when
  empty or unloadable
- App-side env plumbing removed: kEnvPaths table and getEnvIndex/
  setEnvIndex callbacks deleted; StatsPanel keeps ClearColor + debug
  view, env params move to Details
- Serializer: scene-level Settings stores ClearColor only; env
  components serialize per entity; legacy v1 scenes (Environment header
  + Settings.AmbientColor/DrawSkybox) auto-migrate into an Environment
  entity on load for visual parity
- Inspector: Skybox/Sky Light sections with .hdr asset slot dropdown
  and Add Component entries; Outliner "+ Create" popup offers
  Empty/Light/Camera/Environment
This commit is contained in:
2026-10-05 01:02:21 +08:00
parent b7d6f53591
commit 432089747f
14 changed files with 317 additions and 144 deletions
+76 -31
View File
@@ -279,6 +279,21 @@ namespace Engine::Serialization {
out << YAML::EndMap;
}
if (const auto* sb = reg.try_get<ECS::SkyboxComponent>(e)) {
out << YAML::Key << "Skybox" << YAML::Value << YAML::BeginMap;
out << YAML::Key << "Active" << YAML::Value << sb->active;
out << YAML::Key << "EnvMap" << YAML::Value << sb->envPath;
out << YAML::EndMap;
}
if (const auto* sl = reg.try_get<ECS::SkyLightComponent>(e)) {
out << YAML::Key << "SkyLight" << YAML::Value << YAML::BeginMap;
out << YAML::Key << "Ambient" << YAML::Value;
EmitVec4(out, sl->ambientColor);
out << YAML::Key << "Active" << YAML::Value << sl->active;
out << YAML::EndMap;
}
if (const auto* rb = reg.try_get<ECS::RigidBodyComponent>(e)) {
out << YAML::Key << "RigidBody" << YAML::Value
<< YAML::BeginMap;
@@ -397,8 +412,7 @@ namespace Engine::Serialization {
// 文档头 "Scene" 字段(快照无文件名,该字段是注释性的)
static std::string SerializeDoc(const ECS::Scene& scene,
Assets::AssetManager& assets,
const std::string& documentName,
const std::string& environmentPath)
const std::string& documentName)
{
// float32 无损往返需 9 位有效数字(std::numeric_limits
// <float>::max_digits10)
@@ -410,18 +424,11 @@ namespace Engine::Serialization {
out << YAML::Key << "Scene" << YAML::Value << documentName;
}
out << YAML::Key << "Version" << YAML::Value << kSceneVersion;
if (!environmentPath.empty()) {
out << YAML::Key << "Environment" << YAML::Value
<< environmentPath;
}
const auto& s = scene.GetSettings();
out << YAML::Key << "Settings" << YAML::Value << YAML::BeginMap;
out << YAML::Key << "ClearColor" << YAML::Value;
EmitVec4(out, s.ClearColor);
out << YAML::Key << "AmbientColor" << YAML::Value;
EmitVec4(out, s.AmbientColor);
out << YAML::Key << "DrawSkybox" << YAML::Value << s.DrawSkybox;
out << YAML::EndMap;
out << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq;
@@ -442,14 +449,12 @@ namespace Engine::Serialization {
bool SceneSerializer::Serialize(const ECS::Scene& scene,
Assets::AssetManager& assets,
const std::string& outPath,
const std::string& environmentPath)
const std::string& outPath)
{
try {
const std::string doc = SerializeDoc(
scene, assets,
std::filesystem::path(outPath).stem().string(),
environmentPath);
std::filesystem::path(outPath).stem().string());
if (doc.empty()) {
return false; // emit 失败,日志已在 SerializeDoc 打过
}
@@ -477,11 +482,10 @@ namespace Engine::Serialization {
std::string SceneSerializer::SerializeToString(
const ECS::Scene& scene,
Assets::AssetManager& assets,
const std::string& documentName,
const std::string& environmentPath)
const std::string& documentName)
{
try {
return SerializeDoc(scene, assets, documentName, environmentPath);
return SerializeDoc(scene, assets, documentName);
}
catch (const YAML::Exception& e) {
ENGINE_ERROR("SceneSerializer: snapshot failed ({})", e.what());
@@ -500,11 +504,8 @@ namespace Engine::Serialization {
static bool ParseRoot(ECS::Scene& scene,
Assets::AssetManager& assets,
const YAML::Node& root,
const std::string& sourceName,
std::string& outEnvironmentPath)
const std::string& sourceName)
{
outEnvironmentPath.clear();
// Version 闸:高版本拒载(结构未知),缺失按 0 宽容旧档
if (const int version = root["Version"].as<int>(0);
version > kSceneVersion) {
@@ -513,14 +514,27 @@ namespace Engine::Serialization {
return false;
}
// 僵尸节点安全:as<T>(fallback) 对缺失键返回 fallback
outEnvironmentPath = root["Environment"].as<std::string>("");
// ---- 旧档 v1 环境语义兼容读(v2 起环境 = 实体组件)----
// 僵尸节点安全:as<T>(fallback) 对缺失键返回 fallback;环境
// 键仅在真实存在时置位(避免新档误触发兼容建实体)
const std::string legacyEnv =
root["Environment"].as<std::string>("");
bool legacyEnvKey = !legacyEnv.empty();
glm::vec4 legacyAmbient{0.07f, 0.07f, 0.09f, 0.0f};
bool legacyAmbientKey = false;
bool legacyDrawSkybox = true;
bool legacySkyboxKey = false;
if (const YAML::Node st = root["Settings"]; st && st.IsMap()) {
auto& s = scene.GetSettings();
s.ClearColor = ReadVec4(st["ClearColor"], s.ClearColor);
s.AmbientColor = ReadVec4(st["AmbientColor"], s.AmbientColor);
s.DrawSkybox = st["DrawSkybox"].as<bool>(s.DrawSkybox);
if (const YAML::Node amb = st["AmbientColor"]) {
legacyAmbient = ReadVec4(amb, legacyAmbient);
legacyAmbientKey = true;
}
if (const YAML::Node ds = st["DrawSkybox"]) {
legacyDrawSkybox = ds.as<bool>(legacyDrawSkybox);
legacySkyboxKey = true;
}
}
// ---- 阶段 1:解析到中间结构(逐实体容错,不动 registry)----
@@ -746,6 +760,39 @@ namespace Engine::Serialization {
}
}
}
if (const YAML::Node n = comps["Skybox"]) {
auto& sb = handle.AddComponent<ECS::SkyboxComponent>();
sb.active = n["Active"].as<bool>(true);
sb.envPath = n["EnvMap"].as<std::string>("");
}
if (const YAML::Node n = comps["SkyLight"]) {
auto& sl = handle.AddComponent<ECS::SkyLightComponent>();
sl.ambientColor = ReadVec4(n["Ambient"], sl.ambientColor);
sl.active = n["Active"].as<bool>(true);
}
}
// ---- 旧档环境兼容迁移:v1 的 Environment/Settings.AmbientColor/
// DrawSkybox → 补建一个 Environment 实体(视觉等价)。仅当旧键
// 真实存在且实体列表没有环境组件时触发(新档缺省键不触发;
// 快照恢复同理——快照里组件已实体化,不走这里)
const bool legacyEnvPresent =
legacyEnvKey || legacyAmbientKey || legacySkyboxKey;
const bool hasEnvComponents =
scene.GetRegistry().view<ECS::SkyboxComponent>().size() > 0 ||
scene.GetRegistry().view<ECS::SkyLightComponent>().size() > 0;
if (legacyEnvPresent && !hasEnvComponents) {
auto env = scene.CreateEntity("Environment");
auto& sb = env.AddComponent<ECS::SkyboxComponent>();
sb.envPath = legacyEnv;
sb.active = legacyDrawSkybox;
auto& sl = env.AddComponent<ECS::SkyLightComponent>();
sl.ambientColor = legacyAmbient;
ENGINE_INFO("SceneSerializer: legacy environment migrated to "
"'Environment' entity (env='{}')",
legacyEnv);
}
// ---- 阶段 3:重链父子(两遍解析:父可能出现在子之后)。
@@ -779,8 +826,7 @@ namespace Engine::Serialization {
bool SceneSerializer::Deserialize(ECS::Scene& scene,
Assets::AssetManager& assets,
const std::string& inPath,
std::string& outEnvironmentPath)
const std::string& inPath)
{
YAML::Node root;
try {
@@ -796,14 +842,13 @@ namespace Engine::Serialization {
e.what());
return false;
}
return ParseRoot(scene, assets, root, inPath, outEnvironmentPath);
return ParseRoot(scene, assets, root, inPath);
}
bool SceneSerializer::DeserializeFromString(
ECS::Scene& scene,
Assets::AssetManager& assets,
const std::string& yamlText,
std::string& outEnvironmentPath)
const std::string& yamlText)
{
YAML::Node root;
try {
@@ -819,7 +864,7 @@ namespace Engine::Serialization {
e.what());
return false;
}
return ParseRoot(scene, assets, root, "snapshot", outEnvironmentPath);
return ParseRoot(scene, assets, root, "snapshot");
}
} // namespace Engine::Serialization