2015-09-20 17:30:37 +02:00
|
|
|
#include "scene_view.h"
|
2015-10-24 14:28:03 +02:00
|
|
|
#include "core/crc32.h"
|
2016-01-13 23:36:38 +01:00
|
|
|
#include "core/path.h"
|
2015-09-25 21:44:46 +02:00
|
|
|
#include "core/profiler.h"
|
2015-09-20 17:30:37 +02:00
|
|
|
#include "core/resource_manager.h"
|
|
|
|
#include "editor/gizmo.h"
|
|
|
|
#include "editor/world_editor.h"
|
|
|
|
#include "engine/engine.h"
|
2016-01-12 13:52:14 +01:00
|
|
|
#include "engine/plugin_manager.h"
|
2015-09-20 17:30:37 +02:00
|
|
|
#include "renderer/frame_buffer.h"
|
|
|
|
#include "renderer/pipeline.h"
|
2015-10-24 14:28:03 +02:00
|
|
|
#include "renderer/render_scene.h"
|
2016-01-12 13:52:14 +01:00
|
|
|
#include "renderer/renderer.h"
|
2016-01-14 00:09:57 +01:00
|
|
|
#include "editor/imgui/imgui.h"
|
|
|
|
#include "editor/settings.h"
|
2015-09-20 17:30:37 +02:00
|
|
|
|
|
|
|
|
2016-01-03 17:07:07 +01:00
|
|
|
static const char* WINDOW_NAME = "Scene View";
|
2015-09-20 17:30:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
SceneView::SceneView()
|
|
|
|
{
|
|
|
|
m_pipeline = nullptr;
|
|
|
|
m_editor = nullptr;
|
2015-09-20 20:28:51 +02:00
|
|
|
m_camera_speed = 0.1f;
|
2015-09-20 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SceneView::~SceneView()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-21 22:55:15 +02:00
|
|
|
void SceneView::setWireframe(bool wireframe)
|
|
|
|
{
|
|
|
|
m_pipeline->setWireframe(wireframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-20 17:30:37 +02:00
|
|
|
void SceneView::setScene(Lumix::RenderScene* scene)
|
|
|
|
{
|
|
|
|
m_pipeline->setScene(scene);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SceneView::shutdown()
|
|
|
|
{
|
2016-01-13 17:21:59 +01:00
|
|
|
m_editor->universeCreated().unbind<SceneView, &SceneView::onUniverseCreated>(this);
|
|
|
|
m_editor->universeDestroyed().unbind<SceneView, &SceneView::onUniverseDestroyed>(this);
|
2016-01-12 13:52:14 +01:00
|
|
|
Lumix::Pipeline::destroy(m_pipeline);
|
|
|
|
m_pipeline = nullptr;
|
2015-09-20 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-24 14:28:03 +02:00
|
|
|
void SceneView::onUniverseCreated()
|
|
|
|
{
|
|
|
|
auto* scene = m_editor->getScene(Lumix::crc32("renderer"));
|
|
|
|
m_pipeline->setScene(static_cast<Lumix::RenderScene*>(scene));
|
2015-12-19 20:58:15 +01:00
|
|
|
auto* settings = Settings::getInstance();
|
|
|
|
if (!settings) return;
|
|
|
|
|
|
|
|
int count = m_pipeline->getParameterCount();
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
bool b = settings->getValue(m_pipeline->getParameterName(i), m_pipeline->getParameter(i));
|
|
|
|
m_pipeline->setParameter(i, b);
|
|
|
|
}
|
2015-10-24 14:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SceneView::onUniverseDestroyed()
|
|
|
|
{
|
|
|
|
m_pipeline->setScene(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-16 21:19:27 +02:00
|
|
|
bool SceneView::init(Lumix::WorldEditor& editor, Lumix::Array<Action*>& actions)
|
2015-09-20 17:30:37 +02:00
|
|
|
{
|
|
|
|
m_editor = &editor;
|
|
|
|
auto& engine = editor.getEngine();
|
|
|
|
auto& allocator = engine.getAllocator();
|
2016-01-12 13:52:14 +01:00
|
|
|
auto* renderer = static_cast<Lumix::Renderer*>(engine.getPluginManager().getPlugin("renderer"));
|
2015-09-20 17:30:37 +02:00
|
|
|
|
2016-01-12 13:52:14 +01:00
|
|
|
Lumix::Path path("pipelines/main.lua");
|
|
|
|
m_pipeline = Lumix::Pipeline::create(*renderer, path, engine.getAllocator());
|
|
|
|
m_pipeline->load();
|
2016-01-12 19:37:00 +01:00
|
|
|
m_pipeline->addCustomCommandHandler("renderGizmos")
|
|
|
|
.callback.bind<SceneView, &SceneView::renderGizmos>(this);
|
2015-09-20 17:30:37 +02:00
|
|
|
|
2015-10-24 14:28:03 +02:00
|
|
|
editor.universeCreated().bind<SceneView, &SceneView::onUniverseCreated>(this);
|
|
|
|
editor.universeDestroyed().bind<SceneView, &SceneView::onUniverseDestroyed>(this);
|
|
|
|
onUniverseCreated();
|
|
|
|
|
2015-10-16 21:19:27 +02:00
|
|
|
m_toggle_gizmo_step_action =
|
|
|
|
LUMIX_NEW(editor.getAllocator(), Action)("Enable/disable gizmo step", "toggleGizmoStep");
|
|
|
|
m_toggle_gizmo_step_action->is_global = false;
|
|
|
|
|
|
|
|
actions.push(m_toggle_gizmo_step_action);
|
|
|
|
|
2015-09-20 17:30:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SceneView::update()
|
|
|
|
{
|
2015-09-25 21:44:46 +02:00
|
|
|
PROFILE_FUNCTION();
|
2015-10-16 21:19:27 +02:00
|
|
|
if (ImGui::IsAnyItemActive()) return;
|
2015-09-20 17:30:37 +02:00
|
|
|
if (!m_is_opened) return;
|
2015-10-24 14:34:57 +02:00
|
|
|
if (ImGui::GetIO().KeyCtrl) return;
|
2015-09-20 17:30:37 +02:00
|
|
|
|
2015-10-16 21:19:27 +02:00
|
|
|
m_camera_speed =
|
|
|
|
Lumix::Math::maxValue(0.01f, m_camera_speed + ImGui::GetIO().MouseWheel / 20.0f);
|
2015-09-20 20:28:51 +02:00
|
|
|
|
2015-09-20 17:30:37 +02:00
|
|
|
int screen_x = int(ImGui::GetIO().MousePos.x);
|
|
|
|
int screen_y = int(ImGui::GetIO().MousePos.y);
|
|
|
|
bool is_inside = screen_x >= m_screen_x && screen_y >= m_screen_y &&
|
2015-10-16 21:19:27 +02:00
|
|
|
screen_x <= m_screen_x + m_width && screen_y <= m_screen_y + m_height;
|
2015-09-20 17:30:37 +02:00
|
|
|
if (!is_inside) return;
|
|
|
|
|
2015-09-20 20:28:51 +02:00
|
|
|
float speed = m_camera_speed;
|
2015-10-24 14:34:57 +02:00
|
|
|
if (ImGui::GetIO().KeyShift)
|
2015-09-20 17:30:37 +02:00
|
|
|
{
|
|
|
|
speed *= 10;
|
|
|
|
}
|
|
|
|
if (ImGui::GetIO().KeysDown['W'])
|
|
|
|
{
|
|
|
|
m_editor->navigate(1.0f, 0, speed);
|
|
|
|
}
|
|
|
|
if (ImGui::GetIO().KeysDown['S'])
|
|
|
|
{
|
|
|
|
m_editor->navigate(-1.0f, 0, speed);
|
|
|
|
}
|
|
|
|
if (ImGui::GetIO().KeysDown['A'])
|
|
|
|
{
|
|
|
|
m_editor->navigate(0.0f, -1.0f, speed);
|
|
|
|
}
|
|
|
|
if (ImGui::GetIO().KeysDown['D'])
|
|
|
|
{
|
|
|
|
m_editor->navigate(0.0f, 1.0f, speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SceneView::renderGizmos()
|
|
|
|
{
|
2016-01-13 23:36:38 +01:00
|
|
|
m_editor->renderIcons();
|
|
|
|
m_editor->getGizmo().render();
|
2015-09-20 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-03 01:14:38 +02:00
|
|
|
void SceneView::onGUI()
|
2015-09-20 17:30:37 +02:00
|
|
|
{
|
2015-09-27 12:56:01 +02:00
|
|
|
PROFILE_FUNCTION();
|
2015-09-20 17:30:37 +02:00
|
|
|
m_is_opened = false;
|
2016-01-01 20:31:34 +01:00
|
|
|
if (ImGui::BeginDock(WINDOW_NAME))
|
2015-09-20 17:30:37 +02:00
|
|
|
{
|
|
|
|
m_is_opened = true;
|
|
|
|
auto size = ImGui::GetContentRegionAvail();
|
2015-09-20 20:28:51 +02:00
|
|
|
size.y -= ImGui::GetTextLineHeightWithSpacing();
|
2015-09-21 21:26:49 +02:00
|
|
|
if (size.x > 0 && size.y > 0)
|
|
|
|
{
|
|
|
|
auto pos = ImGui::GetWindowPos();
|
|
|
|
m_pipeline->setViewport(0, 0, int(size.x), int(size.y));
|
|
|
|
auto* fb = m_pipeline->getFramebuffer("default");
|
|
|
|
m_texture_handle = fb->getRenderbufferHandle(0);
|
|
|
|
auto cursor_pos = ImGui::GetCursorScreenPos();
|
|
|
|
m_screen_x = int(cursor_pos.x);
|
|
|
|
m_screen_y = int(cursor_pos.y);
|
|
|
|
m_width = int(size.x);
|
|
|
|
m_height = int(size.y);
|
|
|
|
ImGui::Image(&m_texture_handle, size);
|
2016-01-13 17:21:59 +01:00
|
|
|
if (ImGui::IsItemHovered())
|
|
|
|
{
|
|
|
|
m_editor->setGizmoUseStep(m_toggle_gizmo_step_action->isActive());
|
|
|
|
auto rel_mp = ImGui::GetMousePos();
|
|
|
|
rel_mp.x -= m_screen_x;
|
|
|
|
rel_mp.y -= m_screen_y;
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
if (ImGui::IsMouseClicked(i))
|
|
|
|
{
|
|
|
|
m_editor->onMouseDown((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i);
|
|
|
|
}
|
|
|
|
if (ImGui::IsMouseReleased(i))
|
|
|
|
{
|
|
|
|
m_editor->onMouseUp((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i);
|
|
|
|
}
|
|
|
|
|
2016-01-13 20:37:59 +01:00
|
|
|
auto delta = Lumix::Vec2(rel_mp.x, rel_mp.y) - m_last_mouse_pos;
|
2016-01-13 17:21:59 +01:00
|
|
|
if(delta.x != 0 || delta.y != 0)
|
|
|
|
{
|
|
|
|
m_editor->onMouseMove((int)rel_mp.x, (int)rel_mp.y, (int)delta.x, (int)delta.y);
|
|
|
|
m_last_mouse_pos = {rel_mp.x, rel_mp.y};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 21:26:49 +02:00
|
|
|
|
|
|
|
m_pipeline->render();
|
|
|
|
}
|
2015-09-20 20:28:51 +02:00
|
|
|
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::PushItemWidth(60);
|
|
|
|
ImGui::DragFloat("Camera speed", &m_camera_speed, 0.1f, 0.01f, 999.0f, "%.2f");
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (m_editor->isMeasureToolActive())
|
|
|
|
{
|
|
|
|
ImGui::Text("| Measured distance: %f", m_editor->getMeasuredDistance());
|
|
|
|
}
|
2015-10-16 21:19:27 +02:00
|
|
|
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
int step = m_editor->getGizmo().getStep();
|
|
|
|
if (ImGui::DragInt("Gizmo step", &step, 1.0f, 0, 200))
|
2015-11-10 22:25:36 +01:00
|
|
|
{
|
2016-01-01 20:31:34 +01:00
|
|
|
m_editor->getGizmo().setStep(step);
|
2015-11-10 22:25:36 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
int count = m_pipeline->getParameterCount();
|
|
|
|
if (count)
|
2015-11-10 22:25:36 +01:00
|
|
|
{
|
2016-01-01 20:31:34 +01:00
|
|
|
if (ImGui::Button("Pipeline"))
|
2015-11-10 22:25:36 +01:00
|
|
|
{
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::OpenPopup("pipeline_parameters_popup");
|
|
|
|
}
|
2016-01-06 20:43:26 +01:00
|
|
|
ImGui::SameLine();
|
2016-01-01 20:31:34 +01:00
|
|
|
|
|
|
|
if (ImGui::BeginPopup("pipeline_parameters_popup"))
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
2015-11-10 22:25:36 +01:00
|
|
|
{
|
2016-01-01 20:31:34 +01:00
|
|
|
bool b = m_pipeline->getParameter(i);
|
|
|
|
if (ImGui::Checkbox(m_pipeline->getParameterName(i), &b))
|
2015-12-19 20:42:50 +01:00
|
|
|
{
|
2016-01-01 20:31:34 +01:00
|
|
|
auto* settings = Settings::getInstance();
|
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
settings->setValue(m_pipeline->getParameterName(i), b);
|
|
|
|
}
|
|
|
|
m_pipeline->setParameter(i, b);
|
2015-12-19 20:42:50 +01:00
|
|
|
}
|
2015-11-10 22:25:36 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2015-11-10 22:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 20:31:34 +01:00
|
|
|
ImGui::EndDock();
|
2015-09-20 17:30:37 +02:00
|
|
|
}
|