Skip to content

Commit

Permalink
Add unit tests for OccluderInstance3D. (godotengine#43440)
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonetb committed Nov 17, 2024
1 parent 5efd124 commit f626cc7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/scene/test_occluder_instance_3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**************************************************************************/
/* test_occluder_instance_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_OCCLUDER_INSTANCE_3D_H
#define TEST_OCCLUDER_INSTANCE_3D_H

#include "scene/3d/occluder_instance_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/resources/3d/primitive_meshes.h"
#include "core/io/file_access.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"

#include "tests/test_macros.h"

namespace TestOccluderInstance3D {

TEST_CASE("[SceneTree][OccluderInstance3D] Test baking functionality") {
// We need to load an occluder path because when the light occluder generates
// it has to save to a file.
#ifdef WINDOWS_ENABLED
const String occluder_path = OS::get_singleton()->get_environment("TEMP").path_join("test_occluder.occ");
#else
const String occluder_path = "/tmp/test_occluder.occ";
#endif

SceneTree *scene_tree = SceneTree::get_singleton();
scene_tree->get_root()->set_use_occlusion_culling(true);

OccluderInstance3D *occluder_instance = memnew(OccluderInstance3D);
Node *test_bake_scene = memnew(Node);

scene_tree->get_root()->add_child(test_bake_scene);
scene_tree->get_root()->add_child(occluder_instance);

// Instantiate some meshes to generate. There's a box and a cylinder here.
MeshInstance3D *box_mesh_instance = memnew(MeshInstance3D);
Ref<BoxMesh> box_mesh = Ref<BoxMesh>();
box_mesh.instantiate();
box_mesh_instance->set_mesh(box_mesh);
test_bake_scene->add_child(box_mesh_instance);
box_mesh_instance->set_owner(test_bake_scene);

MeshInstance3D *cylinder_mesh_instance = memnew(MeshInstance3D);
Ref<BoxMesh> cylinder_mesh = Ref<CylinderMesh>();
cylinder_mesh.instantiate();
cylinder_mesh_instance->set_mesh(cylinder_mesh);
test_bake_scene->add_child(cylinder_mesh_instance);
cylinder_mesh_instance->set_owner(test_bake_scene);

OccluderInstance3D::BakeError error = occluder_instance->bake_scene(test_bake_scene, occluder_path);

CHECK(error == OccluderInstance3D::BAKE_ERROR_OK);

memdelete(box_mesh_instance);
memdelete(cylinder_mesh_instance);
memdelete(test_bake_scene);
memdelete(occluder_instance);
memdelete(scene_tree);
}

} // namespace TestOccluderInstance3D

#endif // TEST_OCCLUDER_INSTANCE_3D_H
1 change: 1 addition & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "editor/editor_settings.h"
#endif // TOOLS_ENABLED

#include "tests/scene\test_occluder_instance_3d.h"
#include "tests/core/config/test_project_settings.h"
#include "tests/core/input/test_input_event.h"
#include "tests/core/input/test_input_event_key.h"
Expand Down

0 comments on commit f626cc7

Please sign in to comment.