VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
shadowCasterFiltering.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 13.02.2026.
5//
7
8#include <algorithm>
9
12#include "framework/entity.h"
13#include "scene/camera.h"
14#include "scene/frustumUtils.h"
15#include "scene/meshInstance.h"
16#include "scene/mesh.h"
18
19namespace visutwin::canvas
20{
21 namespace
22 {
23 CameraComponent* findCameraComponentForCamera(const Camera* camera)
24 {
25 if (!camera) {
26 return nullptr;
27 }
28 for (auto* cameraComponent : CameraComponent::instances()) {
29 if (cameraComponent && cameraComponent->camera() == camera) {
30 return cameraComponent;
31 }
32 }
33 return nullptr;
34 }
35
36 bool cameraRendersRenderComponent(const CameraComponent* cameraComponent, const RenderComponent* renderComponent)
37 {
38 if (!renderComponent) {
39 return false;
40 }
41 if (!cameraComponent) {
42 return true;
43 }
44
45 const auto& layers = renderComponent->layers();
46 if (layers.empty()) {
47 return true;
48 }
49
50 return std::any_of(layers.begin(), layers.end(), [cameraComponent](const int layerId) {
51 return cameraComponent->rendersLayer(layerId);
52 });
53 }
54 }
55
56 bool shouldRenderShadowRenderComponent(const RenderComponent* renderComponent, const Camera* camera)
57 {
58 if (!renderComponent || !renderComponent->enabled() || !renderComponent->entity() || !renderComponent->entity()->enabled()) {
59 return false;
60 }
61
62 const auto* cameraComponent = findCameraComponentForCamera(camera);
63 return cameraRendersRenderComponent(cameraComponent, renderComponent);
64 }
65
66 bool shouldRenderShadowMeshInstance(MeshInstance* meshInstance, Camera* shadowCamera)
67 {
68 if (!meshInstance || !meshInstance->mesh()) {
69 return false;
70 }
71 if (!meshInstance->castShadow()) {
72 return false;
73 }
74 if (meshInstance->node() && !meshInstance->node()->enabled()) {
75 return false;
76 }
77
78 Material* material = meshInstance->material();
79 const bool alphaTestCaster = material && material->alphaMode() == AlphaMode::MASK;
80 if (material && material->transparent() && !alphaTestCaster) {
81 return false;
82 }
83
84 if (!meshInstance->mesh()->getVertexBuffer()) {
85 return false;
86 }
87
88 if (meshInstance->cull()) {
89 if (!shadowCamera || !shadowCamera->node()) {
90 return false;
91 }
92 if (!isVisibleInCameraFrustum(shadowCamera, shadowCamera->node().get(), meshInstance->aabb())) {
93 return false;
94 }
95 }
96
97 return true;
98 }
99}
static const std::vector< CameraComponent * > & instances()
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
const std::unique_ptr< GraphNode > & node() const
Definition camera.h:102
Entity * entity() const
Definition component.cpp:16
virtual bool enabled() const
Definition component.h:49
Base class for GPU materials — owns uniform data, texture bindings, blend/depth state,...
Definition material.h:143
AlphaMode alphaMode() const
Definition material.h:242
std::shared_ptr< VertexBuffer > getVertexBuffer() const
Definition mesh.h:115
Renderable instance of a Mesh with its own material, transform node, and optional GPU instancing.
Material * material() const
bool shouldRenderShadowMeshInstance(MeshInstance *meshInstance, Camera *shadowCamera)
bool shouldRenderShadowRenderComponent(const RenderComponent *renderComponent, const Camera *camera)
bool isVisibleInCameraFrustum(Camera *camera, GraphNode *cameraNode, const BoundingBox &bounds)