From 2314783e5590002feeae8ca4beffa7abf739a08f Mon Sep 17 00:00:00 2001 From: turanszkij Date: Sat, 1 Sep 2018 21:35:19 +0100 Subject: [PATCH] updates --- Editor/DecalWindow.cpp | 69 ++++++++------- Editor/EnvProbeWindow.cpp | 111 ++++++++++++------------ Editor/ForceFieldWindow.cpp | 151 +++++++++++++++++---------------- Editor/LightWindow.cpp | 22 +---- Editor/WorldWindow.cpp | 72 +++++++++------- WickedEngine/wiECS.h | 2 +- WickedEngine/wiRenderer.cpp | 126 +++++++++++++-------------- WickedEngine/wiRenderer.h | 2 - WickedEngine/wiSceneSystem.cpp | 142 ++++++++++++++++++++++++++++--- WickedEngine/wiSceneSystem.h | 54 +++++++++++- 10 files changed, 454 insertions(+), 297 deletions(-) diff --git a/Editor/DecalWindow.cpp b/Editor/DecalWindow.cpp index eca83e606..291a90d8c 100644 --- a/Editor/DecalWindow.cpp +++ b/Editor/DecalWindow.cpp @@ -20,27 +20,29 @@ DecalWindow::DecalWindow(wiGUI* gui) : GUI(gui) float x = 200; float y = 0; - //opacitySlider = new wiSlider(0, 1, 1, 100000, "Opacity: "); - //opacitySlider->SetSize(XMFLOAT2(100, 30)); - //opacitySlider->SetPos(XMFLOAT2(x, y += 30)); - //opacitySlider->OnSlide([&](wiEventArgs args) { - // if (decal != nullptr) - // { - // decal->color.w = args.fValue; - // } - //}); - //decalWindow->AddWidget(opacitySlider); + opacitySlider = new wiSlider(0, 1, 1, 100000, "Opacity: "); + opacitySlider->SetSize(XMFLOAT2(100, 30)); + opacitySlider->SetPos(XMFLOAT2(x, y += 30)); + opacitySlider->OnSlide([&](wiEventArgs args) { + DecalComponent* decal = wiRenderer::GetScene().decals.GetComponent(entity); + if (decal != nullptr) + { + decal->color.w = args.fValue; + } + }); + decalWindow->AddWidget(opacitySlider); - //emissiveSlider = new wiSlider(0, 100, 0, 100000, "Emissive: "); - //emissiveSlider->SetSize(XMFLOAT2(100, 30)); - //emissiveSlider->SetPos(XMFLOAT2(x, y += 30)); - //emissiveSlider->OnSlide([&](wiEventArgs args) { - // if (decal != nullptr) - // { - // decal->emissive = args.fValue; - // } - //}); - //decalWindow->AddWidget(emissiveSlider); + emissiveSlider = new wiSlider(0, 100, 0, 100000, "Emissive: "); + emissiveSlider->SetSize(XMFLOAT2(100, 30)); + emissiveSlider->SetPos(XMFLOAT2(x, y += 30)); + emissiveSlider->OnSlide([&](wiEventArgs args) { + DecalComponent* decal = wiRenderer::GetScene().decals.GetComponent(entity); + if (decal != nullptr) + { + decal->emissive = args.fValue; + } + }); + decalWindow->AddWidget(emissiveSlider); decalWindow->Translate(XMFLOAT3(30, 30, 0)); @@ -59,20 +61,21 @@ DecalWindow::~DecalWindow() void DecalWindow::SetEntity(Entity entity) { + if (this->entity == entity) + return; + this->entity = entity; - //if (this->decal == decal) - // return; + const DecalComponent* decal = wiRenderer::GetScene().decals.GetComponent(entity); - //this->decal = decal; - //if (decal != nullptr) - //{ - // opacitySlider->SetValue(decal->color.w); - // emissiveSlider->SetValue(decal->emissive); - // decalWindow->SetEnabled(true); - //} - //else - //{ - // decalWindow->SetEnabled(false); - //} + if (decal != nullptr) + { + opacitySlider->SetValue(decal->color.w); + emissiveSlider->SetValue(decal->emissive); + decalWindow->SetEnabled(true); + } + else + { + decalWindow->SetEnabled(false); + } } diff --git a/Editor/EnvProbeWindow.cpp b/Editor/EnvProbeWindow.cpp index fdc9f58a4..f01114fb1 100644 --- a/Editor/EnvProbeWindow.cpp +++ b/Editor/EnvProbeWindow.cpp @@ -18,52 +18,52 @@ EnvProbeWindow::EnvProbeWindow(wiGUI* gui) : GUI(gui) float x = 250, y = 0, step = 45; - //realTimeCheckBox = new wiCheckBox("RealTime: "); - //realTimeCheckBox->SetPos(XMFLOAT2(x, y += step)); - //realTimeCheckBox->SetEnabled(false); - //realTimeCheckBox->OnClick([&](wiEventArgs args) { - // if (probe != nullptr) - // { - // probe->realTime = args.bValue; - // probe->isUpToDate = false; - // } - //}); - //envProbeWindow->AddWidget(realTimeCheckBox); + realTimeCheckBox = new wiCheckBox("RealTime: "); + realTimeCheckBox->SetPos(XMFLOAT2(x, y += step)); + realTimeCheckBox->SetEnabled(false); + realTimeCheckBox->OnClick([&](wiEventArgs args) { + EnvironmentProbeComponent* probe = wiRenderer::GetScene().probes.GetComponent(entity); + if (probe != nullptr) + { + probe->realTime = args.bValue; + probe->isUpToDate = false; + } + }); + envProbeWindow->AddWidget(realTimeCheckBox); - //generateButton = new wiButton("Put"); - //generateButton->SetPos(XMFLOAT2(x, y += step)); - //generateButton->OnClick([](wiEventArgs args) { - // XMFLOAT3 pos; - // XMStoreFloat3(&pos, XMVectorAdd(wiRenderer::getCamera()->GetEye(), wiRenderer::getCamera()->GetAt() * 4)); - // wiRenderer::PutEnvProbe(pos); - //}); - //envProbeWindow->AddWidget(generateButton); + generateButton = new wiButton("Put"); + generateButton->SetPos(XMFLOAT2(x, y += step)); + generateButton->OnClick([](wiEventArgs args) { + XMFLOAT3 pos; + XMStoreFloat3(&pos, XMVectorAdd(wiRenderer::getCamera()->GetEye(), wiRenderer::getCamera()->GetAt() * 4)); + wiRenderer::GetScene().Entity_CreateEnvironmentProbe("editorProbe", pos); + }); + envProbeWindow->AddWidget(generateButton); - //refreshButton = new wiButton("Refresh"); - //refreshButton->SetPos(XMFLOAT2(x, y += step)); - //refreshButton->SetEnabled(false); - //refreshButton->OnClick([&](wiEventArgs args) { - // if (probe != nullptr) - // { - // probe->isUpToDate = false; - // } - //}); - //envProbeWindow->AddWidget(refreshButton); + refreshButton = new wiButton("Refresh"); + refreshButton->SetPos(XMFLOAT2(x, y += step)); + refreshButton->SetEnabled(false); + refreshButton->OnClick([&](wiEventArgs args) { + EnvironmentProbeComponent* probe = wiRenderer::GetScene().probes.GetComponent(entity); + if (probe != nullptr) + { + probe->isUpToDate = false; + } + }); + envProbeWindow->AddWidget(refreshButton); - //refreshAllButton = new wiButton("Refresh All"); - //refreshAllButton->SetPos(XMFLOAT2(x, y += step)); - //refreshAllButton->SetEnabled(true); - //refreshAllButton->OnClick([&](wiEventArgs args) { - // const Scene& scene = wiRenderer::GetScene(); - // for (Model* x : scene.models) - // { - // for (EnvironmentProbe* probe : x->environmentProbes) - // { - // probe->isUpToDate = false; - // } - // } - //}); - //envProbeWindow->AddWidget(refreshAllButton); + refreshAllButton = new wiButton("Refresh All"); + refreshAllButton->SetPos(XMFLOAT2(x, y += step)); + refreshAllButton->SetEnabled(true); + refreshAllButton->OnClick([&](wiEventArgs args) { + Scene& scene = wiRenderer::GetScene(); + for (size_t i = 0; i < scene.probes.GetCount(); ++i) + { + EnvironmentProbeComponent& probe = scene.probes[i]; + probe.isUpToDate = false; + } + }); + envProbeWindow->AddWidget(refreshAllButton); @@ -86,17 +86,18 @@ void EnvProbeWindow::SetEntity(Entity entity) { this->entity = entity; - //probe = value; - //if (probe == nullptr) - //{ - // realTimeCheckBox->SetEnabled(false); - // refreshButton->SetEnabled(false); - //} - //else - //{ - // realTimeCheckBox->SetCheck(probe->realTime); - // realTimeCheckBox->SetEnabled(true); - // refreshButton->SetEnabled(true); - //} + const EnvironmentProbeComponent* probe = wiRenderer::GetScene().probes.GetComponent(entity); + + if (probe == nullptr) + { + realTimeCheckBox->SetEnabled(false); + refreshButton->SetEnabled(false); + } + else + { + realTimeCheckBox->SetCheck(probe->realTime); + realTimeCheckBox->SetEnabled(true); + refreshButton->SetEnabled(true); + } } diff --git a/Editor/ForceFieldWindow.cpp b/Editor/ForceFieldWindow.cpp index 1ddd5013f..6d27513e3 100644 --- a/Editor/ForceFieldWindow.cpp +++ b/Editor/ForceFieldWindow.cpp @@ -22,70 +22,73 @@ ForceFieldWindow::ForceFieldWindow(wiGUI* gui) : GUI(gui) float y = 10; float step = 35; - //typeComboBox = new wiComboBox("Force Field type: "); - //typeComboBox->SetPos(XMFLOAT2(x, y += step)); - //typeComboBox->SetSize(XMFLOAT2(300, 25)); - //typeComboBox->OnSelect([&](wiEventArgs args) { - // if (force != nullptr && args.iValue >= 0) - // { - // switch (args.iValue) - // { - // case 0: - // force->type = ENTITY_TYPE_FORCEFIELD_POINT; - // break; - // case 1: - // force->type = ENTITY_TYPE_FORCEFIELD_PLANE; - // break; - // default: - // assert(0); // error - // break; - // } - // } - //}); - //typeComboBox->AddItem("Point"); - //typeComboBox->AddItem("Plane"); - //typeComboBox->SetEnabled(false); - //typeComboBox->SetTooltip("Choose the force field type."); - //forceFieldWindow->AddWidget(typeComboBox); + typeComboBox = new wiComboBox("Force Field type: "); + typeComboBox->SetPos(XMFLOAT2(x, y += step)); + typeComboBox->SetSize(XMFLOAT2(300, 25)); + typeComboBox->OnSelect([&](wiEventArgs args) { + ForceFieldComponent* force = wiRenderer::GetScene().forces.GetComponent(entity); + if (force != nullptr && args.iValue >= 0) + { + switch (args.iValue) + { + case 0: + force->type = ENTITY_TYPE_FORCEFIELD_POINT; + break; + case 1: + force->type = ENTITY_TYPE_FORCEFIELD_PLANE; + break; + default: + assert(0); // error + break; + } + } + }); + typeComboBox->AddItem("Point"); + typeComboBox->AddItem("Plane"); + typeComboBox->SetEnabled(false); + typeComboBox->SetTooltip("Choose the force field type."); + forceFieldWindow->AddWidget(typeComboBox); - //gravitySlider = new wiSlider(-10, 10, 0, 100000, "Gravity: "); - //gravitySlider->SetSize(XMFLOAT2(200, 30)); - //gravitySlider->SetPos(XMFLOAT2(x, y += step)); - //gravitySlider->OnSlide([&](wiEventArgs args) { - // if (force != nullptr) - // { - // force->gravity = args.fValue; - // } - //}); - //gravitySlider->SetEnabled(false); - //gravitySlider->SetTooltip("Set the amount of gravity. Positive values attract, negatives deflect."); - //forceFieldWindow->AddWidget(gravitySlider); + gravitySlider = new wiSlider(-10, 10, 0, 100000, "Gravity: "); + gravitySlider->SetSize(XMFLOAT2(200, 30)); + gravitySlider->SetPos(XMFLOAT2(x, y += step)); + gravitySlider->OnSlide([&](wiEventArgs args) { + ForceFieldComponent* force = wiRenderer::GetScene().forces.GetComponent(entity); + if (force != nullptr) + { + force->gravity = args.fValue; + } + }); + gravitySlider->SetEnabled(false); + gravitySlider->SetTooltip("Set the amount of gravity. Positive values attract, negatives deflect."); + forceFieldWindow->AddWidget(gravitySlider); - //rangeSlider = new wiSlider(0.0f, 100.0f, 10, 100000, "Range: "); - //rangeSlider->SetSize(XMFLOAT2(200, 30)); - //rangeSlider->SetPos(XMFLOAT2(x, y += step)); - //rangeSlider->OnSlide([&](wiEventArgs args) { - // if (force != nullptr) - // { - // force->range = args.fValue; - // } - //}); - //rangeSlider->SetEnabled(false); - //rangeSlider->SetTooltip("Set the range of affection."); - //forceFieldWindow->AddWidget(rangeSlider); + rangeSlider = new wiSlider(0.0f, 100.0f, 10, 100000, "Range: "); + rangeSlider->SetSize(XMFLOAT2(200, 30)); + rangeSlider->SetPos(XMFLOAT2(x, y += step)); + rangeSlider->OnSlide([&](wiEventArgs args) { + ForceFieldComponent* force = wiRenderer::GetScene().forces.GetComponent(entity); + if (force != nullptr) + { + force->range = args.fValue; + } + }); + rangeSlider->SetEnabled(false); + rangeSlider->SetTooltip("Set the range of affection."); + forceFieldWindow->AddWidget(rangeSlider); - //addButton = new wiButton("Add Force Field"); - //addButton->SetSize(XMFLOAT2(150, 30)); - //addButton->SetPos(XMFLOAT2(x, y += step * 2)); - //addButton->OnClick([](wiEventArgs args) { - // wiRenderer::Add(new ForceField); - //}); - //addButton->SetEnabled(true); - //addButton->SetTooltip("Add new Force Field to the simulation."); - //forceFieldWindow->AddWidget(addButton); + addButton = new wiButton("Add Force Field"); + addButton->SetSize(XMFLOAT2(150, 30)); + addButton->SetPos(XMFLOAT2(x, y += step * 2)); + addButton->OnClick([](wiEventArgs args) { + wiRenderer::GetScene().Entity_CreateForce("editorForce"); + }); + addButton->SetEnabled(true); + addButton->SetTooltip("Add new Force Field to the simulation."); + forceFieldWindow->AddWidget(addButton); @@ -102,25 +105,25 @@ ForceFieldWindow::~ForceFieldWindow() void ForceFieldWindow::SetEntity(Entity entity) { + if (this->entity == entity) + return; + this->entity = entity; - //if (this->force == force) - // return; + const ForceFieldComponent* force = wiRenderer::GetScene().forces.GetComponent(entity); - //this->force = force; + if (force != nullptr) + { + typeComboBox->SetSelected(force->type == ENTITY_TYPE_FORCEFIELD_POINT ? 0 : 1); + gravitySlider->SetValue(force->gravity); + rangeSlider->SetValue(force->range); - //if (force != nullptr) - //{ - // typeComboBox->SetSelected(force->type == ENTITY_TYPE_FORCEFIELD_POINT ? 0 : 1); - // gravitySlider->SetValue(force->gravity); - // rangeSlider->SetValue(force->range); + forceFieldWindow->SetEnabled(true); + } + else + { + forceFieldWindow->SetEnabled(false); + } - // forceFieldWindow->SetEnabled(true); - //} - //else - //{ - // forceFieldWindow->SetEnabled(false); - //} - - //addButton->SetEnabled(true); + addButton->SetEnabled(true); } diff --git a/Editor/LightWindow.cpp b/Editor/LightWindow.cpp index 107bc9fe3..30e5878d6 100644 --- a/Editor/LightWindow.cpp +++ b/Editor/LightWindow.cpp @@ -162,25 +162,7 @@ LightWindow::LightWindow(wiGUI* gui) : GUI(gui) addLightButton->SetPos(XMFLOAT2(x, y += step)); addLightButton->SetSize(XMFLOAT2(150, 30)); addLightButton->OnClick([&](wiEventArgs args) { - Scene& scene = wiRenderer::GetScene(); - - Entity newEntity = CreateEntity(); - - scene.owned_entities.insert(newEntity); - - LightComponent& light = scene.lights.Create(newEntity); - light.energy = 2; - light.range = 60; - light.fov = XM_PIDIV4; - light.color = XMFLOAT3(1, 1, 1); - light.SetType(LightComponent::POINT); - - TransformComponent& transform = scene.transforms.Create(newEntity); - transform.Translate(XMFLOAT3(0, 3, 0)); - - scene.cullables.Create(newEntity); - - SetEntity(newEntity); + wiRenderer::GetScene().Entity_CreateLight("editorLight", XMFLOAT3(0, 3, 0), XMFLOAT3(1, 1, 1), 2, 60); }); addLightButton->SetTooltip("Add a light to the scene."); lightWindow->AddWidget(addLightButton); @@ -244,7 +226,7 @@ void LightWindow::SetEntity(Entity entity) this->entity = entity; - LightComponent* light = wiRenderer::GetScene().lights.GetComponent(entity); + const LightComponent* light = wiRenderer::GetScene().lights.GetComponent(entity); if (light != nullptr) { diff --git a/Editor/WorldWindow.cpp b/Editor/WorldWindow.cpp index 67bf797ee..65e6b5e11 100644 --- a/Editor/WorldWindow.cpp +++ b/Editor/WorldWindow.cpp @@ -29,7 +29,7 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) fogStartSlider->SetSize(XMFLOAT2(100, 30)); fogStartSlider->SetPos(XMFLOAT2(x, y += step)); fogStartSlider->OnSlide([&](wiEventArgs args) { - wiRenderer::GetScene().fogSEH.x = args.fValue; + wiRenderer::GetScene().fogStart = args.fValue; }); worldWindow->AddWidget(fogStartSlider); @@ -37,7 +37,7 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) fogEndSlider->SetSize(XMFLOAT2(100, 30)); fogEndSlider->SetPos(XMFLOAT2(x, y += step)); fogEndSlider->OnSlide([&](wiEventArgs args) { - wiRenderer::GetScene().fogSEH.y = args.fValue; + wiRenderer::GetScene().fogEnd = args.fValue; }); worldWindow->AddWidget(fogEndSlider); @@ -45,7 +45,7 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) fogHeightSlider->SetSize(XMFLOAT2(100, 30)); fogHeightSlider->SetPos(XMFLOAT2(x, y += step)); fogHeightSlider->OnSlide([&](wiEventArgs args) { - wiRenderer::GetScene().fogSEH.z = args.fValue; + wiRenderer::GetScene().fogHeight = args.fValue; }); worldWindow->AddWidget(fogHeightSlider); @@ -133,14 +133,17 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) preset1Button->SetPos(XMFLOAT2(x - 100, y += step * 2)); preset1Button->OnClick([=](wiEventArgs args) { - wiRenderer::GetScene().ambient = XMFLOAT3(0.1f, 0.1f, 0.1f); - wiRenderer::GetScene().horizon = XMFLOAT3(0.3f, 0.3f, 0.4f); - wiRenderer::GetScene().zenith = XMFLOAT3(0.05f, 0.05f, 0.5f); - wiRenderer::GetScene().cloudiness = 0.4f; - wiRenderer::GetScene().fogSEH = XMFLOAT3(100, 1000, 0); + Scene& scene = wiRenderer::GetScene(); + + scene.ambient = XMFLOAT3(0.1f, 0.1f, 0.1f); + scene.horizon = XMFLOAT3(0.3f, 0.3f, 0.4f); + scene.zenith = XMFLOAT3(0.05f, 0.05f, 0.5f); + scene.cloudiness = 0.4f; + scene.fogStart = 100; + scene.fogEnd = 1000; + scene.fogHeight = 0; // Also, we invalidate all environment probes to reflect the sky changes. - Scene& scene = wiRenderer::GetScene(); for (size_t i = 0; i < scene.probes.GetCount(); ++i) { scene.probes[i].isUpToDate = false; @@ -155,14 +158,17 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) preset2Button->SetPos(XMFLOAT2(x - 100, y += step)); preset2Button->OnClick([=](wiEventArgs args) { - wiRenderer::GetScene().ambient = XMFLOAT3(0.02f, 0.02f, 0.02f); - wiRenderer::GetScene().horizon = XMFLOAT3(0.2f, 0.05f, 0.15f); - wiRenderer::GetScene().zenith = XMFLOAT3(0.4f, 0.05f, 0.1f); - wiRenderer::GetScene().cloudiness = 0.36f; - wiRenderer::GetScene().fogSEH = XMFLOAT3(50, 600, 0); + Scene& scene = wiRenderer::GetScene(); + + scene.ambient = XMFLOAT3(0.02f, 0.02f, 0.02f); + scene.horizon = XMFLOAT3(0.2f, 0.05f, 0.15f); + scene.zenith = XMFLOAT3(0.4f, 0.05f, 0.1f); + scene.cloudiness = 0.36f; + scene.fogStart = 50; + scene.fogEnd = 600; + scene.fogHeight = 0; // Also, we invalidate all environment probes to reflect the sky changes. - Scene& scene = wiRenderer::GetScene(); for (size_t i = 0; i < scene.probes.GetCount(); ++i) { scene.probes[i].isUpToDate = false; @@ -177,14 +183,17 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) preset3Button->SetPos(XMFLOAT2(x - 100, y += step)); preset3Button->OnClick([=](wiEventArgs args) { - wiRenderer::GetScene().ambient = XMFLOAT3(0.1f, 0.1f, 0.1f); - wiRenderer::GetScene().horizon = XMFLOAT3(0.38f, 0.38f, 0.38f); - wiRenderer::GetScene().zenith = XMFLOAT3(0.42f, 0.42f, 0.42f); - wiRenderer::GetScene().cloudiness = 0.75f; - wiRenderer::GetScene().fogSEH = XMFLOAT3(0, 500, 0); + Scene& scene = wiRenderer::GetScene(); + + scene.ambient = XMFLOAT3(0.1f, 0.1f, 0.1f); + scene.horizon = XMFLOAT3(0.38f, 0.38f, 0.38f); + scene.zenith = XMFLOAT3(0.42f, 0.42f, 0.42f); + scene.cloudiness = 0.75f; + scene.fogStart = 0; + scene.fogEnd = 500; + scene.fogHeight = 0; // Also, we invalidate all environment probes to reflect the sky changes. - Scene& scene = wiRenderer::GetScene(); for (size_t i = 0; i < scene.probes.GetCount(); ++i) { scene.probes[i].isUpToDate = false; @@ -199,14 +208,17 @@ WorldWindow::WorldWindow(wiGUI* gui) : GUI(gui) preset4Button->SetPos(XMFLOAT2(x - 100, y += step)); preset4Button->OnClick([=](wiEventArgs args) { - wiRenderer::GetScene().ambient = XMFLOAT3(0.01f, 0.01f, 0.02f); - wiRenderer::GetScene().horizon = XMFLOAT3(0.02f, 0.05f, 0.1f); - wiRenderer::GetScene().zenith = XMFLOAT3(0.01f, 0.02f, 0.04f); - wiRenderer::GetScene().cloudiness = 0.28f; - wiRenderer::GetScene().fogSEH = XMFLOAT3(10, 400, 0); + Scene& scene = wiRenderer::GetScene(); + + scene.ambient = XMFLOAT3(0.01f, 0.01f, 0.02f); + scene.horizon = XMFLOAT3(0.02f, 0.05f, 0.1f); + scene.zenith = XMFLOAT3(0.01f, 0.02f, 0.04f); + scene.cloudiness = 0.28f; + scene.fogStart = 10; + scene.fogEnd = 400; + scene.fogHeight = 0; // Also, we invalidate all environment probes to reflect the sky changes. - Scene& scene = wiRenderer::GetScene(); for (size_t i = 0; i < scene.probes.GetCount(); ++i) { scene.probes[i].isUpToDate = false; @@ -337,7 +349,7 @@ void WorldWindow::UpdateFromRenderer() { auto& w = wiRenderer::GetScene(); - fogStartSlider->SetValue(w.fogSEH.x); - fogEndSlider->SetValue(w.fogSEH.y); - fogHeightSlider->SetValue(w.fogSEH.z); + fogStartSlider->SetValue(w.fogStart); + fogEndSlider->SetValue(w.fogEnd); + fogHeightSlider->SetValue(w.fogHeight); } diff --git a/WickedEngine/wiECS.h b/WickedEngine/wiECS.h index da637f400..1b7cb9e7f 100644 --- a/WickedEngine/wiECS.h +++ b/WickedEngine/wiECS.h @@ -71,7 +71,6 @@ namespace wiECS // Remove the corresponding entry from the lookup table: const Entity entity = entities[index]; - lookup.erase(entity); if (components.size() > 1) { @@ -86,6 +85,7 @@ namespace wiECS // Shrink the container: components.pop_back(); entities.pop_back(); + lookup.erase(entity); } } diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 8ccec99f4..77e5d571c 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -4020,18 +4020,20 @@ void wiRenderer::DrawDebugWorld(CameraComponent* camera, GRAPHICSTHREAD threadID { EnvironmentProbeComponent& probe = scene.probes[i]; - if (probe.textureIndex < 0) - { - continue; - } - Entity entity = scene.probes.GetEntity(i); const TransformComponent& transform = *scene.transforms.GetComponent(entity); sb.mTransform = XMMatrixTranspose(XMMatrixTranslation(transform.translation.x, transform.translation.y, transform.translation.z)); device->UpdateBuffer(constantBuffers[CBTYPE_MISC], &sb, threadID); - device->BindResource(PS, textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY], TEXSLOT_ONDEMAND0, threadID, textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY]->GetDesc().MipLevels + probe.textureIndex); + if (probe.textureIndex < 0) + { + device->BindResource(PS, wiTextureHelper::getInstance()->getBlackCubeMap(), TEXSLOT_ONDEMAND0, threadID); + } + else + { + device->BindResource(PS, textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY], TEXSLOT_ONDEMAND0, threadID, textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY]->GetDesc().MipLevels + probe.textureIndex); + } device->Draw(2880, 0, threadID); // uv-sphere } @@ -5563,60 +5565,62 @@ void wiRenderer::DrawSun(GRAPHICSTHREAD threadID) void wiRenderer::DrawDecals(CameraComponent* camera, GRAPHICSTHREAD threadID) { - //GraphicsDevice* device = GetDevice(); + GraphicsDevice* device = GetDevice(); + Scene& scene = GetScene(); - //bool boundCB = false; - //for (Model* model : GetScene().models) - //{ - // if (model->decals.empty()) - // continue; + bool boundCB = false; + if(scene.decals.GetCount() > 0) + { + device->EventBegin("Decals", threadID); - // device->EventBegin("Decals", threadID); + if (!boundCB) + { + boundCB = true; + device->BindConstantBuffer(PS, constantBuffers[CBTYPE_DECAL], CB_GETBINDSLOT(DecalCB),threadID); + } - // if (!boundCB) - // { - // boundCB = true; - // device->BindConstantBuffer(PS, constantBuffers[CBTYPE_DECAL], CB_GETBINDSLOT(DecalCB),threadID); - // } + device->BindStencilRef(STENCILREF_DEFAULT, threadID); - // device->BindStencilRef(STENCILREF_DEFAULT, threadID); + device->BindGraphicsPSO(PSO_decal, threadID); - // device->BindGraphicsPSO(PSO_decal, threadID); + for (size_t i = 0; i < scene.decals.GetCount(); ++i) + { + DecalComponent& decal = scene.decals[i]; + Entity entity = scene.decals.GetEntity(i); + const CullableComponent& cullable = *scene.cullables.GetComponent(entity); - // for (Decal* decal : model->decals) - // { + if ((decal.texture != nullptr || decal.normal != nullptr) && camera->frustum.CheckBox(cullable.aabb)) + { - // if ((decal->texture || decal->normal) && camera->frustum.CheckBox(decal->bounds)) { + device->BindResource(PS, decal.texture, TEXSLOT_ONDEMAND0, threadID); + device->BindResource(PS, decal.normal, TEXSLOT_ONDEMAND1, threadID); - // device->BindResource(PS, decal->texture, TEXSLOT_ONDEMAND0, threadID); - // device->BindResource(PS, decal->normal, TEXSLOT_ONDEMAND1, threadID); + XMMATRIX decalWorld = XMLoadFloat4x4(&decal.world); - // XMMATRIX decalWorld = XMLoadFloat4x4(&decal->world); + MiscCB dcbvs; + dcbvs.mTransform = XMMatrixTranspose(decalWorld*camera->GetViewProjection()); + device->UpdateBuffer(constantBuffers[CBTYPE_MISC], &dcbvs, threadID); - // MiscCB dcbvs; - // dcbvs.mTransform =XMMatrixTranspose(decalWorld*camera->GetViewProjection()); - // device->UpdateBuffer(constantBuffers[CBTYPE_MISC], &dcbvs, threadID); + DecalCB dcbps; + dcbps.mDecalVP = XMMatrixTranspose(XMMatrixInverse(nullptr, decalWorld)); // todo: cache the inverse! + dcbps.hasTexNor = 0; + if (decal.texture != nullptr) + dcbps.hasTexNor |= 0x0000001; + if (decal.normal != nullptr) + dcbps.hasTexNor |= 0x0000010; + XMStoreFloat3(&dcbps.eye, camera->GetEye()); + dcbps.opacity = decal.GetOpacity(); + dcbps.front = decal.front; + device->UpdateBuffer(constantBuffers[CBTYPE_DECAL], &dcbps, threadID); - // DecalCB dcbps; - // dcbps.mDecalVP = XMMatrixTranspose(XMMatrixInverse(nullptr, decalWorld)); - // dcbps.hasTexNor = 0; - // if (decal->texture != nullptr) - // dcbps.hasTexNor |= 0x0000001; - // if (decal->normal != nullptr) - // dcbps.hasTexNor |= 0x0000010; - // XMStoreFloat3(&dcbps.eye, camera->GetEye()); - // dcbps.opacity = decal->GetOpacity(); - // dcbps.front = decal->front; - // device->UpdateBuffer(constantBuffers[CBTYPE_DECAL], &dcbps, threadID); + device->Draw(14, 0, threadID); - // device->Draw(14, 0, threadID); + } - // } + } - // } - - // device->EventEnd(threadID); - //} + device->EventEnd(threadID); + } } void wiRenderer::RefreshEnvProbes(GRAPHICSTHREAD threadID) @@ -7451,16 +7455,14 @@ void wiRenderer::ManageDecalAtlas(GRAPHICSTHREAD threadID) for (size_t i = 0; i < scene.decals.GetCount(); ++i) { const DecalComponent& decal = scene.decals[i]; - Entity entity = scene.decals.GetEntity(i); - const MaterialComponent& material = *scene.materials.GetComponent(entity); - if (material.baseColorMap != nullptr) + if (decal.texture != nullptr) { - if (storedTextures.find(material.baseColorMap) == storedTextures.end()) + if (storedTextures.find(decal.texture) == storedTextures.end()) { // we need to pack this decal texture into the atlas - rect_xywhf newRect = rect_xywhf(0, 0, material.baseColorMap->GetDesc().Width + atlasClampBorder * 2, material.baseColorMap->GetDesc().Height + atlasClampBorder * 2); - storedTextures[material.baseColorMap] = newRect; + rect_xywhf newRect = rect_xywhf(0, 0, decal.texture->GetDesc().Width + atlasClampBorder * 2, decal.texture->GetDesc().Height + atlasClampBorder * 2); + storedTextures[decal.texture] = newRect; repackAtlas = true; } @@ -7531,14 +7533,12 @@ void wiRenderer::ManageDecalAtlas(GRAPHICSTHREAD threadID) for (size_t i = 0; i < scene.decals.GetCount(); ++i) { DecalComponent& decal = scene.decals[i]; - Entity entity = scene.decals.GetEntity(i); - const MaterialComponent& material = *scene.materials.GetComponent(entity); - if (material.baseColorMap != nullptr) + if (decal.texture != nullptr) { const TextureDesc& desc = atlasTexture->GetDesc(); - rect_xywhf rect = storedTextures[material.baseColorMap]; + rect_xywhf rect = storedTextures[decal.texture]; // eliminate border expansion: rect.x += atlasClampBorder; @@ -7578,7 +7578,7 @@ void wiRenderer::UpdateWorldCB(GRAPHICSTHREAD threadID) value.mAmbient = scene.ambient; value.mCloudiness = scene.cloudiness; value.mCloudScale = scene.cloudScale; - value.mFog = scene.fogSEH; + value.mFog = XMFLOAT3(scene.fogStart, scene.fogEnd, scene.fogHeight); value.mHorizon = scene.horizon; value.mZenith = scene.zenith; value.mSpecularAA = SPECULARAA; @@ -8055,9 +8055,7 @@ void wiRenderer::LoadWorldInfo(const std::string& fileName) break; case 'm': { - float s, e, h; - file >> s >> e >> h; - scene.fogSEH = XMFLOAT3(s, e, h); + file >> scene.fogStart >> scene.fogEnd >> scene.fogHeight; } break; default:break; @@ -8156,14 +8154,6 @@ void wiRenderer::SynchronizeWithPhysicsEngine(float dt) //} } -void wiRenderer::PutEnvProbe(const XMFLOAT3& position) -{ - //EnvironmentProbe* probe = new EnvironmentProbe; - //probe->transform(position); - - //GetScene().GetWorldNode()->environmentProbes.push_back(probe); -} - void wiRenderer::CreateImpostor(Entity entity, GRAPHICSTHREAD threadID) { //MeshComponent& mesh = GetScene().meshes.GetComponent(mesh_ref); diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h index 214ab9a74..d52554615 100644 --- a/WickedEngine/wiRenderer.h +++ b/WickedEngine/wiRenderer.h @@ -503,8 +503,6 @@ public: static wiECS::Entity LoadModel(const std::string& fileName, const XMMATRIX& transform = XMMatrixIdentity()); static void LoadWorldInfo(const std::string& fileName); - static void PutEnvProbe(const XMFLOAT3& position); - static void CreateImpostor(wiECS::Entity, GRAPHICSTHREAD threadID); static std::vector> renderableBoxes; diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index 96526aaf0..acbb6a27a 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -1,6 +1,7 @@ #include "wiSceneSystem.h" #include "wiMath.h" #include "wiTextureHelper.h" +#include "wiResourceManager.h" using namespace wiECS; using namespace wiGraphicsTypes; @@ -396,6 +397,9 @@ namespace wiSceneSystem Entity entity = decals.GetEntity(i); const TransformComponent* transform = transforms.GetComponent(entity); decal.world = transform->world; + XMVECTOR front = XMVectorSet(0, 0, 1, 0); + front = XMVector3TransformNormal(front, XMLoadFloat4x4(&decal.world)); + XMStoreFloat3(&decal.front, front); } // Update Light components: @@ -545,8 +549,43 @@ namespace wiSceneSystem } + // Iterate all cullables and recompute scene bounds: + bounds.create(XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX), XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX)); + for (size_t i = 0; i < cullables.GetCount(); ++i) + { + const CullableComponent& cullable = cullables[i]; + + bounds = AABB::Merge(bounds, cullable.aabb); + } + } - void Scene::Remove(Entity entity) + void Scene::Clear() + { + for (Entity entity : owned_entities) + { + names.Remove(entity); + layers.Remove(entity); + transforms.Remove(entity); + parents.Remove(entity); + materials.Remove(entity); + meshes.Remove(entity); + objects.Remove(entity); + physicscomponents.Remove(entity); + cullables.Remove(entity); + bones.Remove(entity); + armatures.Remove(entity); + lights.Remove(entity); + cameras.Remove(entity); + probes.Remove(entity); + forces.Remove(entity); + decals.Remove(entity); + models.Remove(entity); + } + + owned_entities.clear(); + } + + void Scene::Entity_Remove(Entity entity) { owned_entities.erase(entity); @@ -568,15 +607,7 @@ namespace wiSceneSystem decals.Remove(entity); models.Remove(entity); } - void Scene::Clear() - { - for (auto& x : owned_entities) - { - Remove(x); - } - } - - Entity Scene::Component_FindName(const std::string& name) + Entity Scene::Entity_FindByName(const std::string& name) { for (size_t i = 0; i < names.GetCount(); ++i) { @@ -587,6 +618,97 @@ namespace wiSceneSystem } return INVALID_ENTITY; } + Entity Scene::Entity_CreateLight( + const std::string& name, + const XMFLOAT3& position, + const XMFLOAT3& color, + float energy, + float range) + { + Entity entity = CreateEntity(); + + owned_entities.insert(entity); + + names.Create(entity) = name; + + transforms.Create(entity).Translate(position); + + cullables.Create(entity).aabb.createFromHalfWidth(position, XMFLOAT3(range, range, range)); + + LightComponent& light = lights.Create(entity); + light.energy = energy; + light.range = range; + light.fov = XM_PIDIV4; + light.color = color; + light.SetType(LightComponent::POINT); + + return entity; + } + wiECS::Entity Scene::Entity_CreateForce( + const std::string& name, + const XMFLOAT3& position + ) + { + Entity entity = CreateEntity(); + + owned_entities.insert(entity); + + names.Create(entity) = name; + + transforms.Create(entity).Translate(position); + + ForceFieldComponent& force = forces.Create(entity); + force.gravity = 0; + force.range = 0; + force.type = ENTITY_TYPE_FORCEFIELD_POINT; + + return entity; + } + wiECS::Entity Scene::Entity_CreateEnvironmentProbe( + const std::string& name, + const XMFLOAT3& position + ) + { + Entity entity = CreateEntity(); + + owned_entities.insert(entity); + + names.Create(entity) = name; + + transforms.Create(entity).Translate(position); + + EnvironmentProbeComponent& probe = probes.Create(entity); + probe.isUpToDate = false; + probe.realTime = false; + probe.textureIndex = -1; + + return entity; + } + wiECS::Entity Scene::Entity_CreateDecal( + const std::string& name, + const std::string& textureName, + const std::string& normalMapName + ) + { + Entity entity = CreateEntity(); + + owned_entities.insert(entity); + + names.Create(entity) = name; + + transforms.Create(entity); + + cullables.Create(entity); + + DecalComponent& decal = decals.Create(entity); + decal.textureName = textureName; + decal.texture = (Texture2D*)wiResourceManager::GetGlobal()->add(decal.textureName); + decal.normalMapName = normalMapName; + decal.normal = (Texture2D*)wiResourceManager::GetGlobal()->add(decal.normalMapName); + + return entity; + } + void Scene::Component_Attach(Entity entity, Entity parent) { assert(entity != parent); diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index 9e0ab3f56..8745ed698 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -557,8 +557,20 @@ namespace wiSceneSystem struct DecalComponent { + XMFLOAT4 color = XMFLOAT4(1, 1, 1, 1); XMFLOAT4 atlasMulAdd = XMFLOAT4(0, 0, 0, 0); XMFLOAT4X4 world; + XMFLOAT3 front; + + float emissive = 0; + + std::string textureName; + wiGraphicsTypes::Texture2D* texture = nullptr; + + std::string normalMapName; + wiGraphicsTypes::Texture2D* normal = nullptr; + + inline float GetOpacity() const { return color.w; } }; struct ModelComponent @@ -594,8 +606,10 @@ namespace wiSceneSystem XMFLOAT3 horizon = XMFLOAT3(0.0f, 0.0f, 0.0f); XMFLOAT3 zenith = XMFLOAT3(0.00f, 0.00f, 0.0f); XMFLOAT3 ambient = XMFLOAT3(0.2f, 0.2f, 0.2f); - XMFLOAT3 fogSEH = XMFLOAT3(100, 1000, 0); - XMFLOAT4 water = XMFLOAT4(0, 0, 0, 0); + float fogStart = 100; + float fogEnd = 1000; + float fogHeight = 0; + XMFLOAT4 waterPlane = XMFLOAT4(0, 0, 0, 0); float cloudiness = 0.0f; float cloudScale = 0.0003f; float cloudSpeed = 0.1f; @@ -603,13 +617,45 @@ namespace wiSceneSystem float windRandomness = 5; float windWaveSize = 1; + // Update all components by a given timestep (in milliseconds): void Update(float dt); - void Remove(wiECS::Entity entity); + // Remove everything from the scene that it owns: void Clear(); - wiECS::Entity Component_FindName(const std::string& name); + // Removes a specific entity from the scene (if it exists): + void Entity_Remove(wiECS::Entity entity); + // Finds the first entity by the name (if it exists, otherwise returns INVALID_ENTITY): + wiECS::Entity Entity_FindByName(const std::string& name); + // Helper function to create a light entity: + wiECS::Entity Entity_CreateLight( + const std::string& name, + const XMFLOAT3& position = XMFLOAT3(0, 0, 0), + const XMFLOAT3& color = XMFLOAT3(1, 1, 1), + float energy = 1, + float range = 10 + ); + // Helper function to create a force entity: + wiECS::Entity Entity_CreateForce( + const std::string& name, + const XMFLOAT3& position = XMFLOAT3(0, 0, 0) + ); + // Helper function to create an environment capture probe entity: + wiECS::Entity Entity_CreateEnvironmentProbe( + const std::string& name, + const XMFLOAT3& position = XMFLOAT3(0, 0, 0) + ); + // Helper function to create a decal entity: + wiECS::Entity Entity_CreateDecal( + const std::string& name, + const std::string& textureName, + const std::string& normalMapName = "" + ); + + // Attaches an entity to a parent: void Component_Attach(wiECS::Entity entity, wiECS::Entity parent); + // Detaches the entity from its parent (if it is attached): void Component_Detach(wiECS::Entity entity); + // Detaches all children from an entity (if there are any): void Component_DetachChildren(wiECS::Entity parent); };