| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <Samples/Common/SampleApp.h>
- #include <AnKi/Scene/Events/AnimationEvent.h>
- using namespace anki;
- class ButtonsUiNode : public SceneNode
- {
- public:
- FontPtr m_font;
- Renderer* m_renderer = nullptr;
- ConfigSet* m_config = nullptr;
- Bool m_vrs = true;
- Bool m_lsVis = false;
- Bool m_giVis = false;
- Bool m_reflVis = false;
- ButtonsUiNode(SceneGraph* scene, CString name)
- : SceneNode(scene, name)
- {
- SpatialComponent* spatialc = newComponent<SpatialComponent>();
- spatialc->setAlwaysVisible(true);
- UiComponent* uic = newComponent<UiComponent>();
- uic->init(
- [](CanvasPtr& canvas, void* ud) {
- static_cast<ButtonsUiNode*>(ud)->draw(canvas);
- },
- this);
- ANKI_CHECK_AND_IGNORE(getSceneGraph().getUiManager().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf",
- Array<U32, 1>{46}));
- }
- void draw(CanvasPtr& canvas)
- {
- if(!m_renderer->getGrManager().getDeviceCapabilities().m_vrs)
- {
- return;
- }
- const Vec4 oldWindowColor = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
- ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.3f;
- ImGui::Begin("VrsButtons", nullptr,
- ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove
- | ImGuiWindowFlags_AlwaysAutoResize);
- canvas->pushFont(m_font, 46);
- ImGui::SetWindowPos(Vec2(canvas->getWidth() - ImGui::GetWindowWidth() - 195.0f, 55.0f));
- ImGui::SetWindowSize(Vec2(230.0f, 450.0f));
- if(m_config->getRVrs())
- {
- //ImGui::Text("VRS visualization: ");
- //ImGui::SameLine();
- if(ImGui::Checkbox("Light Shading", &m_lsVis))
- {
- m_giVis = m_reflVis = false;
- }
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- if(ImGui::Checkbox("Global Illumination", &m_giVis))
- {
- m_lsVis = m_reflVis = false;
- }
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- if(ImGui::Checkbox("Reflections", &m_reflVis))
- {
- m_lsVis = m_giVis = false;
- }
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- ImGui::Spacing();
- ImGui::SameLine();
- }
- if(ImGui::Checkbox("VRS", &m_vrs))
- {
- }
- m_config->setRVrs(m_vrs);
- if(m_config->getRVrs() && m_lsVis)
- {
- m_renderer->setCurrentDebugRenderTarget("VrsSri");
- }
- else if(m_config->getRVrs() && m_giVis)
- {
- m_renderer->setCurrentDebugRenderTarget("IndirectDiffuseVrsSri");
- }
- else if(m_config->getRVrs() && m_reflVis)
- {
- m_renderer->setCurrentDebugRenderTarget("VrsSriDownscaled");
- }
- else
- {
- m_renderer->setCurrentDebugRenderTarget("");
- }
- canvas->popFont();
- ImGui::End();
- ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldWindowColor;
- }
- };
- Error SampleApp::init(int argc, char** argv, CString sampleName)
- {
- HeapMemoryPool pool(allocAligned, nullptr);
- // Init the super class
- m_config.init(allocAligned, nullptr);
- m_config.setWindowFullscreen(true);
- #if !ANKI_OS_ANDROID
- StringRaii mainDataPath(&pool, ANKI_SOURCE_DIRECTORY);
- StringRaii assetsDataPath(&pool);
- assetsDataPath.sprintf("%s/Samples/%s", ANKI_SOURCE_DIRECTORY, sampleName.cstr());
- if(!directoryExists(assetsDataPath))
- {
- ANKI_LOGE("Cannot find directory \"%s\". Have you moved the clone of the repository? "
- "I'll continue but expect issues",
- assetsDataPath.cstr());
- }
- else
- {
- m_config.setRsrcDataPaths(StringRaii(&pool).sprintf("%s:%s", mainDataPath.cstr(), assetsDataPath.cstr()));
- }
- #endif
- ANKI_CHECK(m_config.setFromCommandLineArguments(argc - 1, argv + 1));
- ANKI_CHECK(App::init(&m_config, allocAligned, nullptr));
- ANKI_CHECK(sampleExtraInit());
- ButtonsUiNode* node;
- getSceneGraph().newSceneNode("VrsButtons", node);
- node->m_renderer = &getMainRenderer().getOffscreenRenderer();
- node->m_config = &m_config;
- node->m_vrs = m_config.getRVrs();
- return Error::kNone;
- }
- Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
- {
- constexpr F32 ROTATE_ANGLE = toRad(2.5f);
- constexpr F32 MOUSE_SENSITIVITY = 5.0f;
- quit = false;
- SceneGraph& scene = getSceneGraph();
- Renderer& renderer = getMainRenderer().getOffscreenRenderer();
- Input& in = getInput();
- if(in.getKey(KeyCode::kEscape))
- {
- quit = true;
- return Error::kNone;
- }
- if(in.getKey(KeyCode::kBackquote) == 1)
- {
- setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
- }
- if(in.getKey(KeyCode::kY) == 1)
- {
- renderer.setCurrentDebugRenderTarget(
- (renderer.getCurrentDebugRenderTarget() == "GBufferNormals") ? "" : "GBufferNormals");
- }
- if(in.getKey(KeyCode::kU) == 1)
- {
- renderer.setCurrentDebugRenderTarget(
- (renderer.getCurrentDebugRenderTarget() == "IndirectDiffuse") ? "" : "IndirectDiffuse");
- }
- if(in.getKey(KeyCode::kI) == 1)
- {
- renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SSR") ? "" : "SSR");
- }
- if(in.getKey(KeyCode::kO) == 1)
- {
- renderer.setCurrentDebugRenderTarget(
- (renderer.getCurrentDebugRenderTarget() == "ResolvedShadows") ? "" : "ResolvedShadows");
- }
- if(in.getKey(KeyCode::kP) == 1)
- {
- static U32 idx = 3;
- ++idx;
- idx %= 4;
- if(idx == 0)
- {
- renderer.setCurrentDebugRenderTarget("IndirectDiffuseVrsSri");
- }
- else if(idx == 1)
- {
- renderer.setCurrentDebugRenderTarget("VrsSriDownscaled");
- }
- else if(idx == 2)
- {
- renderer.setCurrentDebugRenderTarget("VrsSri");
- }
- else
- {
- renderer.setCurrentDebugRenderTarget("");
- }
- }
- if(in.getKey(KeyCode::kL) == 1)
- {
- renderer.setCurrentDebugRenderTarget(
- (renderer.getCurrentDebugRenderTarget() == "LightShading") ? "" : "LightShading");
- }
- if(in.getKey(KeyCode::kH) == 1)
- {
- static U32 pressCount = 0;
- CString rtName;
- switch(pressCount)
- {
- case 0:
- rtName = "RtShadows";
- break;
- case 1:
- rtName = "RtShadows1";
- break;
- case 2:
- rtName = "RtShadows2";
- break;
- default:
- rtName = "";
- }
- renderer.setCurrentDebugRenderTarget(rtName);
- pressCount = (pressCount + 1) % 4;
- }
- if(in.getKey(KeyCode::kJ) == 1)
- {
- m_config.setRVrs(!m_config.getRVrs());
- }
- static Vec2 mousePosOn1stClick = in.getMousePosition();
- if(in.getMouseButton(MouseButton::kRight) == 1)
- {
- // Re-init mouse pos
- mousePosOn1stClick = in.getMousePosition();
- }
- if(in.getMouseButton(MouseButton::kRight) || in.hasTouchDevice())
- {
- in.hideCursor(true);
- // move the camera
- static MoveComponent* mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
- if(in.getKey(KeyCode::k1) == 1)
- {
- mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
- }
- if(in.getKey(KeyCode::kF1) == 1)
- {
- static U mode = 0;
- mode = (mode + 1) % 3;
- if(mode == 0)
- {
- getConfig().setRDbgEnabled(false);
- }
- else if(mode == 1)
- {
- getConfig().setRDbgEnabled(true);
- renderer.getDbg().setDepthTestEnabled(true);
- renderer.getDbg().setDitheredDepthTestEnabled(false);
- }
- else
- {
- getConfig().setRDbgEnabled(true);
- renderer.getDbg().setDepthTestEnabled(false);
- renderer.getDbg().setDitheredDepthTestEnabled(true);
- }
- }
- if(in.getKey(KeyCode::kF2) == 1)
- {
- // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
- }
- if(in.getKey(KeyCode::kUp))
- {
- mover->rotateLocalX(ROTATE_ANGLE);
- }
- if(in.getKey(KeyCode::kDown))
- {
- mover->rotateLocalX(-ROTATE_ANGLE);
- }
- if(in.getKey(KeyCode::kLeft))
- {
- mover->rotateLocalY(ROTATE_ANGLE);
- }
- if(in.getKey(KeyCode::kRight))
- {
- mover->rotateLocalY(-ROTATE_ANGLE);
- }
- static F32 moveDistance = 0.1f;
- if(in.getMouseButton(MouseButton::kScrollUp) == 1)
- {
- moveDistance += 0.1f;
- moveDistance = min(moveDistance, 10.0f);
- }
- if(in.getMouseButton(MouseButton::kScrollDown) == 1)
- {
- moveDistance -= 0.1f;
- moveDistance = max(moveDistance, 0.1f);
- }
- if(in.getKey(KeyCode::kA))
- {
- mover->moveLocalX(-moveDistance);
- }
- if(in.getKey(KeyCode::kD))
- {
- mover->moveLocalX(moveDistance);
- }
- if(in.getKey(KeyCode::kQ))
- {
- mover->moveLocalY(-moveDistance);
- }
- if(in.getKey(KeyCode::kE))
- {
- mover->moveLocalY(moveDistance);
- }
- if(in.getKey(KeyCode::kW))
- {
- mover->moveLocalZ(-moveDistance);
- }
- if(in.getKey(KeyCode::kS))
- {
- mover->moveLocalZ(moveDistance);
- }
- if(in.getKey(KeyCode::kF12) == 1 && ANKI_ENABLE_TRACE)
- {
- TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
- }
- const Vec2 velocity = in.getMousePosition() - mousePosOn1stClick;
- in.moveCursor(mousePosOn1stClick);
- if(velocity != Vec2(0.0))
- {
- Euler angles(mover->getLocalRotation().getRotationPart());
- angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
- angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
- angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
- angles.z() = 0.0f;
- mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
- }
- static TouchPointer rotateCameraTouch = TouchPointer::kCount;
- static Vec2 rotateEventInitialPos = Vec2(0.0f);
- for(TouchPointer touch : EnumIterable<TouchPointer>())
- {
- if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1
- && in.getTouchPointerNdcPosition(touch).x() > 0.1f && in.getTouchPointerNdcPosition(touch).y() < 0.6f)
- {
- rotateCameraTouch = touch;
- rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
- break;
- }
- }
- if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
- {
- rotateCameraTouch = TouchPointer::kCount;
- }
- if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
- {
- Vec2 velocity =
- in.getTouchPointerNdcPosition(rotateCameraTouch) * getWindow().getAspectRatio() - rotateEventInitialPos;
- velocity *= 0.1f;
- Euler angles(mover->getLocalRotation().getRotationPart());
- angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
- angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
- angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
- angles.z() = 0.0f;
- mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
- }
- static TouchPointer moveCameraTouch = TouchPointer::kCount;
- static Vec2 moveEventInitialPos = Vec2(0.0f);
- for(TouchPointer touch : EnumIterable<TouchPointer>())
- {
- if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1
- && in.getTouchPointerNdcPosition(touch).x() < -0.1f && in.getTouchPointerNdcPosition(touch).y() < 0.6f)
- {
- moveCameraTouch = touch;
- moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
- break;
- }
- }
- if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
- {
- moveCameraTouch = TouchPointer::kCount;
- }
- if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
- {
- Vec2 velocity =
- in.getTouchPointerNdcPosition(moveCameraTouch) * getWindow().getAspectRatio() - moveEventInitialPos;
- velocity *= 2.0f;
- mover->moveLocalX(moveDistance * velocity.x());
- mover->moveLocalZ(moveDistance * -velocity.y());
- }
- // Camera fly mode
- if(moveCameraTouch != TouchPointer::kCount || rotateCameraTouch != TouchPointer::kCount)
- {
- m_timesOfLastTouchEvent = kIdleTime;
- if(m_cameraAnimationEvent)
- {
- m_cameraAnimationEvent->setMarkedForDeletion();
- m_cameraAnimationEvent = nullptr;
- }
- }
- m_timesOfLastTouchEvent -= elapsedTime;
- if(m_timesOfLastTouchEvent < 0.0 && m_cameraAnimationEvent == nullptr)
- {
- scene.getEventManager().newEvent(m_cameraAnimationEvent,
- "Assets/Camera.001Action.003_67159cf18109479.ankianim", "Camera.001",
- &scene.getActiveCameraNode());
- }
- }
- else
- {
- in.hideCursor(false);
- }
- return Error::kNone;
- }
|