WIP Make shadows visible for Storm delegate #80

Closed
Vasyl Pidhirskyi wants to merge 16 commits from Vasyl-Pidhirskyi/blender_bn:BLEN-469 into hydra-render

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
3 changed files with 57 additions and 62 deletions
Showing only changes of commit 088eb5a0f8 - Show all commits

View File

@ -160,7 +160,6 @@ if(WITH_HYDRA)
hydra/camera.h hydra/camera.h
hydra/curves.h hydra/curves.h
hydra/hydra_scene_delegate.h hydra/hydra_scene_delegate.h
hydra/shadow_matrix.h
hydra/id.h hydra/id.h
hydra/image.h hydra/image.h
hydra/instancer.h hydra/instancer.h

View File

@ -2,10 +2,11 @@
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation */ * SPDX-FileCopyrightText: 2011-2022 Blender Foundation */
#include "light.h" #include "light.h"
#include "shadow_matrix.h"
#include <pxr/base/gf/frustum.h>
#include <pxr/imaging/hd/light.h> #include <pxr/imaging/hd/light.h>
#include <pxr/imaging/hd/tokens.h> #include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hdx/shadowMatrixComputation.h>
#include <pxr/imaging/hdx/simpleLightTask.h> #include <pxr/imaging/hdx/simpleLightTask.h>
#include <pxr/usd/usdLux/tokens.h> #include <pxr/usd/usdLux/tokens.h>
@ -18,6 +19,48 @@
namespace blender::io::hydra { namespace blender::io::hydra {
class ShadowMatrix : public pxr::HdxShadowMatrixComputation {
public:
ShadowMatrix()
{
_frustum = pxr::GfFrustum();
}
std::vector<pxr::GfMatrix4d> Compute(const pxr::GfVec4f &viewport,
pxr::CameraUtilConformWindowPolicy policy) override
{
return {_frustum.ComputeViewMatrix() * _frustum.ComputeProjectionMatrix()};
}
std::vector<pxr::GfMatrix4d> Compute(const pxr::CameraUtilFraming &framing,
pxr::CameraUtilConformWindowPolicy policy) override
{
return {_frustum.ComputeViewMatrix() * _frustum.ComputeProjectionMatrix()};
}
void set_projection_type(const pxr::GfFrustum::ProjectionType projection_type)
{
_frustum.SetProjectionType(projection_type);
}
void set_near_far(const float near_dist, const float far_dist)
{
_frustum.SetNearFar(pxr::GfRange1d(near_dist, far_dist));
}
void set_transform(const pxr::GfMatrix4d &transform)
{
_frustum.Transform(transform);
}
void set_window(const float x, const float y)
{
_frustum.SetWindow(pxr::GfRange2d(pxr::GfVec2d(-x, -y), pxr::GfVec2d(x, y)));
}
pxr::GfFrustum _frustum;
};
LightData::LightData(HydraSceneDelegate *scene_delegate, LightData::LightData(HydraSceneDelegate *scene_delegate,
Object *object, Object *object,
pxr::SdfPath const &prim_id) pxr::SdfPath const &prim_id)
@ -112,15 +155,15 @@ void LightData::write_shadow_params()
/* ShadowMatrix class is used to calculates shadow matrix from light frustum.*/ /* ShadowMatrix class is used to calculates shadow matrix from light frustum.*/
ShadowMatrix *shadow_matrix = new ShadowMatrix(); ShadowMatrix *shadow_matrix = new ShadowMatrix();
shadow_matrix->SetNearFar(0.1, light->cascade_max_dist); shadow_matrix->set_near_far(0.1, light->cascade_max_dist);
shadow_matrix->SetTransform(gf_matrix_from_transform(((Object *)id)->object_to_world)); shadow_matrix->set_transform(gf_matrix_from_transform(((Object *)id)->object_to_world));
/* SetWindow sets size of projected shadow texture texture in /* SetWindow sets size of projected shadow texture texture in
the approximate value is calculated using sun_angle property the approximate value is calculated using sun_angle property
180 degrees is window size 1000 by 1000 */ 180 degrees is window size 1000 by 1000 */
shadow_matrix->SetWindow(static_cast<int>(light->sun_angle * 0.5f / M_PI * 1000), shadow_matrix->set_window(light->sun_angle * 0.5f / M_PI * 1000,
static_cast<int>(light->sun_angle * 0.5f / M_PI * 1000)); light->sun_angle * 0.5f / M_PI * 1000);
shadow_matrix->CalculateMatrix(); shadow_matrix->set_projection_type(pxr::GfFrustum::Orthographic);
shadow_params.enabled = use_shadow; shadow_params.enabled = use_shadow;
shadow_params.resolution = scene_delegate_->scene->eevee.shadow_cascade_size; shadow_params.resolution = scene_delegate_->scene->eevee.shadow_cascade_size;
shadow_params.shadowMatrix = pxr::HdxShadowMatrixComputationSharedPtr(shadow_matrix); shadow_params.shadowMatrix = pxr::HdxShadowMatrixComputationSharedPtr(shadow_matrix);
@ -161,10 +204,15 @@ void LightData::update()
bits = pxr::HdLight::AllDirty; bits = pxr::HdLight::AllDirty;
} }
else if (id->recalc & ID_RECALC_TRANSFORM) { else if (id->recalc & ID_RECALC_TRANSFORM) {
write_shadow_params(); if (light->mode & LA_SHADOW) {
init();
bits = pxr::HdLight::AllDirty;
}
else {
write_transform(); write_transform();
bits = pxr::HdLight::DirtyTransform; bits = pxr::HdLight::DirtyTransform;
} }
}
if (bits != pxr::HdChangeTracker::Clean) { if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits);
ID_LOGN(1, ""); ID_LOGN(1, "");

View File

@ -1,52 +0,0 @@
#pragma once
#include <pxr/base/gf/frustum.h>
#include <pxr/imaging/hdx/shadowMatrixComputation.h>
namespace blender::io::hydra {
class ShadowMatrix : public pxr::HdxShadowMatrixComputation {
public:
ShadowMatrix()
{
_frustum = pxr::GfFrustum();
_frustum.SetProjectionType(pxr::GfFrustum::Orthographic);
}
std::vector<pxr::GfMatrix4d> Compute(const pxr::GfVec4f &viewport,
pxr::CameraUtilConformWindowPolicy policy)
{
return {_shadowMatrix};
}
std::vector<pxr::GfMatrix4d> Compute(const pxr::CameraUtilFraming &framing,
pxr::CameraUtilConformWindowPolicy policy)
{
return {_shadowMatrix};
}
void SetNearFar(const float &near_dist, const float &far_dist)
{
_frustum.SetNearFar(pxr::GfRange1d(near_dist, far_dist));
}
void SetTransform(const pxr::GfMatrix4d &transform)
{
_frustum.Transform(transform);
}
void SetWindow(const double &x, const double &y)
{
_frustum.SetWindow(pxr::GfRange2d(pxr::GfVec2d(-x, -y), pxr::GfVec2d(x, y)));
}
void CalculateMatrix()
{
_shadowMatrix = _frustum.ComputeViewMatrix() * _frustum.ComputeProjectionMatrix();
}
pxr::GfMatrix4d _shadowMatrix;
pxr::GfFrustum _frustum;
};
} // namespace blender::io::hydra