rohit.gonsalves Posted March 29, 2022 Posted March 29, 2022 Hi, I am trying to get the shadow maps for LightWorld. as in this figure. The version is 2.14.1.1. I am trying following code for a simple test. /* Copyright (C) 2005-2021, UNIGINE. All rights reserved. * * This file is a part of the UNIGINE 2 SDK. * * Your use and / or redistribution of this software in source and / or * binary form, with or without modification, is subject to: (i) your * ongoing acceptance of and compliance with the terms and conditions of * the UNIGINE License Agreement; and (ii) your inclusion of this notice * in any version of this software that you use or redistribute. * A copy of the UNIGINE License Agreement is available by contacting * UNIGINE. at http://unigine.com/ */ #include <UnigineEngine.h> #include <UnigineRender.h> #include <UnigineCallback.h> #include <UnigineNode.h> #include <UnigineLights.h> #include <UnigineWorld.h> #include <UniginePtr.h> #include<vector> #include<string> #include "AppEditorLogic.h" #include "AppSystemLogic.h" #include "AppWorldLogic.h" using namespace Unigine; using namespace std; void onGBufferCallback(Unigine::Renderer* _renderer) { Vector<NodePtr> vecNodes; World::getRootNodes(vecNodes); NodePtr objNode; for (int iCount = 0; iCount < vecNodes.size(); iCount++) { objNode = vecNodes[iCount]->findNode("sun"); if (objNode.get() != nullptr) { const LightWorld* lightWorld = static_cast<const LightWorld*>(objNode.get()); if (lightWorld != nullptr) { TexturePtr textureShadowDepth = lightWorld->getDynamicDepthTexture(); if (textureShadowDepth.get() != nullptr) { ImagePtr imgGrabScreen = Image::create(); textureShadowDepth->getImage(imgGrabScreen); //imgGrabScreen->convertToFormat(Image::FORMAT_RGBA8); string szFilePath = "D:\\texture.dds"; imgGrabScreen->save(szFilePath.c_str()); } break; } } } } #ifdef _WIN32 int wmain(int argc, wchar_t *argv[]) #else int main(int argc, char *argv[]) #endif { // UnigineLogic AppSystemLogic system_logic; AppWorldLogic world_logic; AppEditorLogic editor_logic; // init engine Unigine::EnginePtr engine(UNIGINE_VERSION, argc, argv); Unigine::Render::addCallback(Unigine::Render::CALLBACK_END_WORLD_SHADOW, MakeCallback(&onGBufferCallback)); // enter main loop engine->main(&system_logic, &world_logic, &editor_logic); return 0; } #ifdef _WIN32 #include <Windows.h> extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; extern "C" __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; #endif When I change the Resolution for PSSM shadows, And number of cascades, I really get the texture with that resolution and number of 2D array. For 2 Cascades 2 image frames of dds and for 4 cascades 4 image frames. But I am little confused about the information put in that. In other words, How should I able to get the shadow map in the form of texture to use it in further custom post processing. Please advise. Regards, Rohit
rohit.gonsalves Posted March 31, 2022 Author Posted March 31, 2022 Hello there, https://developer.unigine.com/en/docs/2.15.1/api/library/rendering/class.renderer?rlang=cpp#getCurrentLight_Light From this documentation, it ask to set the callback and then get the shadow map for the specific light. At this point I am doing this just with the LightWorld. From Light World which texture should I get? What is the information put in this texture as documentation is not really helpful to understand this. Or point me to place where I can read about it. What could be the possible composition or point me to the part of files I should look into the SDK to understand how Unigine compositing shadow map? I need these shadow maps separately as I have Projected the real camera feeds on cubes which is completely filling the frustum so I will always see the whole feed. They are with unlit materials using overlap and Emission. Now I try to put some virtual object on them. It is working. But I am losing shadows as projected real feeds are with overlap state. So I want to grab the shadows from the light and composite with my final render. Rohit.
sweetluna Posted April 4, 2022 Posted April 4, 2022 Hi Rohit, Something like this should work a little. Currently there is no clear way to get light's shadows separately from all other lights, the only way for this is by using internal shaders but this is not recommended since it will change in future. I attached sample material, just attach it to your main camera or add it globally as scriptable material. Also you can have easier way of getting all shadows, refer to lights from Material format. For C++ it would be https://developer-autotest.unigine.com/en/docs/2.15.1/api/library/rendering/class.renderer?rlang=cpp#getTextureLights_Texture int get_shadow_res(int size) { if (size == -1) size = 512; else size = 1 << (6 + size); return size; } void opacity_light_cb() { LightPtr light = checked_ptr_cast<Light>(World::getNodeByType(Node::TYPE::LIGHT_WORLD)); if (!light || light->getType() != Node::LIGHT_WORLD) return; LightWorldPtr world = checked_ptr_cast<LightWorld>(light); TexturePtr light_depth = world->getDepthTexture(); MaterialPtr mat = Materials::findManualMaterial("light_shadow_post"); world->updateRenderShadowCascadeMatrices(Renderer::getCameraPosition(), Renderer::getZFar()); float light_shadow_filter = light->getShadowFilter() * 2.0f; float light_shadow_fade = 1.0f; float light_shadow_radius = light->getBoundSphere().radius; Math::Mat4 transform = light->getWorldTransform(); Math::Vec3 light_world_direction = normalize(transform.getColumn3(2)); Math::vec3 light_direction = Math::vec3(rotation(Renderer::getModelview()) * light_world_direction); // shadow bias float shadow_resolution = (float)get_shadow_res(light->getShadowResolution()); float shadow_bias = 1.0f; shadow_bias += 0.75f * light_shadow_filter; shadow_bias *= 2.0f / shadow_resolution; float light_shadow_penumbra = light->getShadowPenumbra(); if (world->getShadowMode() == Light::SHADOW_MODE_STATIC || world->getShadowMode() == Light::SHADOW_MODE_MIXED) { shadow_bias *= max(world->getShadowWidth(), world->getShadowHeight()); light_shadow_penumbra /= max(world->getShadowWidth(), world->getShadowHeight()); } else { shadow_bias *= light_shadow_radius; light_shadow_penumbra /= light_shadow_radius; } Math::vec2 light_shadow_depth_bias; light_shadow_depth_bias.x = shadow_bias * light->getShadowBias(); light_shadow_depth_bias.y = shadow_bias * light->getShadowNormalBias(); ShaderPtr shader = mat->fetchShader("my_pass", -1); int num = world->getNumShadowCascades(); shader->setParameterInt("s_light_cascades_num", num); shader->setParameterFloat("s_light_shadow_fade", light_shadow_fade); shader->setParameterFloat("s_light_shadow_filter", light_shadow_filter); shader->setParameterFloat("s_light_shadow_iradius", Math::rcp(light_shadow_radius)); shader->setParameterFloat3("s_light_direction", light_direction); shader->setParameterFloat2("s_light_shadow_depth_bias", light_shadow_depth_bias); Math::vec4 light_cascades_border; Math::mat4 light_cascades_projection[4]; for (int i = 0; i < num; i++) { Math::Mat4 light_modelview = world->getRenderShadowCascadeModelview(i); Math::mat4 light_projection = world->getRenderShadowCascadeProjection(i); Math::mat4 cascade_projection = Math::translate(0.5f, 0.5f, 0.5f) * Math::scale(0.5f, -0.5f, 0.5f) * reverseDepthProjection(light_projection) * Math::mat4(light_modelview * Renderer::getIModelview()); light_cascades_projection[i] = cascade_projection; light_cascades_border[i] = world->getShadowCascadeBorder(i); } shader->setParameterArrayFloat4x4("s_light_cascades_projection", light_cascades_projection, 4); shader->setParameterArrayFloat("s_light_cascades_border", light_cascades_border.get(), 4); shader->setParameterFloat4("s_light_color", Math::vec4_one); shader->setParameterFloat("s_light_shadow_penumbra", light_shadow_penumbra); mat->setTexture("light_shadow", light_depth); } int AppWorldLogic::init() { // Write here code to be called on world initialization: initialize resources for your world scene during the world start. Render::addCallback(Render::CALLBACK_BEGIN_OPACITY_LIGHTS, MakeCallback(opacity_light_cb)); return 1; } Hope it helps light_shadow_post.basemat 1 May RenderDoc/Nsight Graphics/Intel GPA bless you
Recommended Posts