2020-01-10 19:22:34 +08:00
/*************************************************************************/
2020-03-27 05:49:16 +08:00
/* navigation_region_3d.cpp */
2020-01-10 19:22:34 +08:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2022-01-04 04:27:34 +08:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2020-01-10 19:22:34 +08:00
/* */
/* 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. */
/*************************************************************************/
2020-03-27 05:49:16 +08:00
# include "navigation_region_3d.h"
2020-03-31 00:22:57 +08:00
2020-03-27 05:49:16 +08:00
# include "mesh_instance_3d.h"
2020-03-28 02:21:27 +08:00
# include "servers/navigation_server_3d.h"
2020-01-10 19:22:34 +08:00
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : set_enabled ( bool p_enabled ) {
2020-05-14 22:41:43 +08:00
if ( enabled = = p_enabled ) {
2020-01-10 19:22:34 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2020-01-10 19:22:34 +08:00
enabled = p_enabled ;
2020-05-14 22:41:43 +08:00
if ( ! is_inside_tree ( ) ) {
2020-01-10 19:22:34 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2020-01-10 19:22:34 +08:00
if ( ! enabled ) {
2020-03-28 02:21:27 +08:00
NavigationServer3D : : get_singleton ( ) - > region_set_map ( region , RID ( ) ) ;
2020-01-10 19:22:34 +08:00
} else {
2021-03-08 16:47:18 +08:00
NavigationServer3D : : get_singleton ( ) - > region_set_map ( region , get_world_3d ( ) - > get_navigation_map ( ) ) ;
2020-01-10 19:22:34 +08:00
}
if ( debug_view ) {
2020-03-27 05:49:16 +08:00
MeshInstance3D * dm = Object : : cast_to < MeshInstance3D > ( debug_view ) ;
2020-01-10 19:22:34 +08:00
if ( is_enabled ( ) ) {
dm - > set_material_override ( get_tree ( ) - > get_debug_navigation_material ( ) ) ;
} else {
dm - > set_material_override ( get_tree ( ) - > get_debug_navigation_disabled_material ( ) ) ;
}
}
2021-06-23 22:49:50 +08:00
update_gizmos ( ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
bool NavigationRegion3D : : is_enabled ( ) const {
2020-01-10 19:22:34 +08:00
return enabled ;
}
2021-03-09 03:56:33 +08:00
void NavigationRegion3D : : set_layers ( uint32_t p_layers ) {
NavigationServer3D : : get_singleton ( ) - > region_set_layers ( region , p_layers ) ;
}
uint32_t NavigationRegion3D : : get_layers ( ) const {
return NavigationServer3D : : get_singleton ( ) - > region_get_layers ( region ) ;
}
2020-01-10 19:22:34 +08:00
/////////////////////////////
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : _notification ( int p_what ) {
2020-01-10 19:22:34 +08:00
switch ( p_what ) {
case NOTIFICATION_ENTER_TREE : {
2021-03-08 16:47:18 +08:00
if ( enabled ) {
NavigationServer3D : : get_singleton ( ) - > region_set_map ( region , get_world_3d ( ) - > get_navigation_map ( ) ) ;
2020-01-10 19:22:34 +08:00
}
if ( navmesh . is_valid ( ) & & get_tree ( ) - > is_debugging_navigation_hint ( ) ) {
2020-03-27 05:49:16 +08:00
MeshInstance3D * dm = memnew ( MeshInstance3D ) ;
2020-01-10 19:22:34 +08:00
dm - > set_mesh ( navmesh - > get_debug_mesh ( ) ) ;
if ( is_enabled ( ) ) {
dm - > set_material_override ( get_tree ( ) - > get_debug_navigation_material ( ) ) ;
} else {
dm - > set_material_override ( get_tree ( ) - > get_debug_navigation_disabled_material ( ) ) ;
}
add_child ( dm ) ;
debug_view = dm ;
}
} break ;
2022-02-16 01:06:48 +08:00
2020-01-10 19:22:34 +08:00
case NOTIFICATION_TRANSFORM_CHANGED : {
2020-03-28 02:21:27 +08:00
NavigationServer3D : : get_singleton ( ) - > region_set_transform ( region , get_global_transform ( ) ) ;
2020-01-10 19:22:34 +08:00
} break ;
2022-02-16 01:06:48 +08:00
2020-01-10 19:22:34 +08:00
case NOTIFICATION_EXIT_TREE : {
2021-03-08 16:47:18 +08:00
NavigationServer3D : : get_singleton ( ) - > region_set_map ( region , RID ( ) ) ;
2020-01-10 19:22:34 +08:00
if ( debug_view ) {
debug_view - > queue_delete ( ) ;
2020-04-02 07:20:12 +08:00
debug_view = nullptr ;
2020-01-10 19:22:34 +08:00
}
} break ;
}
}
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : set_navigation_mesh ( const Ref < NavigationMesh > & p_navmesh ) {
2020-05-14 22:41:43 +08:00
if ( p_navmesh = = navmesh ) {
2020-01-10 19:22:34 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2020-01-10 19:22:34 +08:00
if ( navmesh . is_valid ( ) ) {
2021-02-11 04:18:45 +08:00
navmesh - > disconnect ( " changed " , callable_mp ( this , & NavigationRegion3D : : _navigation_changed ) ) ;
2020-01-10 19:22:34 +08:00
}
navmesh = p_navmesh ;
if ( navmesh . is_valid ( ) ) {
2021-02-11 04:18:45 +08:00
navmesh - > connect ( " changed " , callable_mp ( this , & NavigationRegion3D : : _navigation_changed ) ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-28 02:21:27 +08:00
NavigationServer3D : : get_singleton ( ) - > region_set_navmesh ( region , p_navmesh ) ;
2020-01-10 19:22:34 +08:00
if ( debug_view & & navmesh . is_valid ( ) ) {
2020-03-27 05:49:16 +08:00
Object : : cast_to < MeshInstance3D > ( debug_view ) - > set_mesh ( navmesh - > get_debug_mesh ( ) ) ;
2020-01-10 19:22:34 +08:00
}
2021-07-18 05:22:52 +08:00
emit_signal ( SNAME ( " navigation_mesh_changed " ) ) ;
2020-01-10 19:22:34 +08:00
2021-06-23 22:49:50 +08:00
update_gizmos ( ) ;
2020-10-29 18:01:28 +08:00
update_configuration_warnings ( ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
Ref < NavigationMesh > NavigationRegion3D : : get_navigation_mesh ( ) const {
2020-01-10 19:22:34 +08:00
return navmesh ;
}
struct BakeThreadsArgs {
2021-02-08 05:29:31 +08:00
NavigationRegion3D * nav_region = nullptr ;
2020-01-10 19:22:34 +08:00
} ;
void _bake_navigation_mesh ( void * p_user_data ) {
BakeThreadsArgs * args = static_cast < BakeThreadsArgs * > ( p_user_data ) ;
2020-03-09 00:33:34 +08:00
if ( args - > nav_region - > get_navigation_mesh ( ) . is_valid ( ) ) {
Ref < NavigationMesh > nav_mesh = args - > nav_region - > get_navigation_mesh ( ) - > duplicate ( ) ;
2020-01-10 19:22:34 +08:00
2020-03-28 02:21:27 +08:00
NavigationServer3D : : get_singleton ( ) - > region_bake_navmesh ( nav_mesh , args - > nav_region ) ;
2021-07-18 05:22:52 +08:00
args - > nav_region - > call_deferred ( SNAME ( " _bake_finished " ) , nav_mesh ) ;
2020-01-10 19:22:34 +08:00
memdelete ( args ) ;
} else {
ERR_PRINT ( " Can't bake the navigation mesh if the `NavigationMesh` resource doesn't exist " ) ;
2021-07-18 05:22:52 +08:00
args - > nav_region - > call_deferred ( SNAME ( " _bake_finished " ) , Ref < NavigationMesh > ( ) ) ;
2020-01-10 19:22:34 +08:00
memdelete ( args ) ;
}
}
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : bake_navigation_mesh ( ) {
2021-09-03 10:30:04 +08:00
ERR_FAIL_COND_MSG ( bake_thread . is_started ( ) , " Unable to start another bake request. The navigation mesh bake thread is already baking a navigation mesh. " ) ;
2020-01-10 19:22:34 +08:00
BakeThreadsArgs * args = memnew ( BakeThreadsArgs ) ;
2020-03-09 00:33:34 +08:00
args - > nav_region = this ;
2020-01-10 19:22:34 +08:00
2021-01-19 20:29:41 +08:00
bake_thread . start ( _bake_navigation_mesh , args ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : _bake_finished ( Ref < NavigationMesh > p_nav_mesh ) {
2020-01-10 19:22:34 +08:00
set_navigation_mesh ( p_nav_mesh ) ;
2021-01-19 20:29:41 +08:00
bake_thread . wait_to_finish ( ) ;
2021-07-18 05:22:52 +08:00
emit_signal ( SNAME ( " bake_finished " ) ) ;
2020-01-10 19:22:34 +08:00
}
2020-10-29 18:01:28 +08:00
TypedArray < String > NavigationRegion3D : : get_configuration_warnings ( ) const {
TypedArray < String > warnings = Node : : get_configuration_warnings ( ) ;
2020-05-15 04:59:27 +08:00
2020-10-29 18:01:28 +08:00
if ( is_visible_in_tree ( ) & & is_inside_tree ( ) ) {
if ( ! navmesh . is_valid ( ) ) {
warnings . push_back ( TTR ( " A NavigationMesh resource must be set or created for this node to work. " ) ) ;
2020-05-15 04:59:27 +08:00
}
2020-01-10 19:22:34 +08:00
}
2020-10-02 15:41:55 +08:00
2020-10-29 18:01:28 +08:00
return warnings ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
void NavigationRegion3D : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " set_navigation_mesh " , " navmesh " ) , & NavigationRegion3D : : set_navigation_mesh ) ;
ClassDB : : bind_method ( D_METHOD ( " get_navigation_mesh " ) , & NavigationRegion3D : : get_navigation_mesh ) ;
2020-01-10 19:22:34 +08:00
2020-03-27 05:49:16 +08:00
ClassDB : : bind_method ( D_METHOD ( " set_enabled " , " enabled " ) , & NavigationRegion3D : : set_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_enabled " ) , & NavigationRegion3D : : is_enabled ) ;
2020-01-10 19:22:34 +08:00
2021-03-09 03:56:33 +08:00
ClassDB : : bind_method ( D_METHOD ( " set_layers " , " layers " ) , & NavigationRegion3D : : set_layers ) ;
ClassDB : : bind_method ( D_METHOD ( " get_layers " ) , & NavigationRegion3D : : get_layers ) ;
2020-03-27 05:49:16 +08:00
ClassDB : : bind_method ( D_METHOD ( " bake_navigation_mesh " ) , & NavigationRegion3D : : bake_navigation_mesh ) ;
ClassDB : : bind_method ( D_METHOD ( " _bake_finished " , " nav_mesh " ) , & NavigationRegion3D : : _bake_finished ) ;
2020-01-10 19:22:34 +08:00
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " navmesh " , PROPERTY_HINT_RESOURCE_TYPE , " NavigationMesh " ) , " set_navigation_mesh " , " get_navigation_mesh " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " enabled " ) , " set_enabled " , " is_enabled " ) ;
2021-03-09 03:56:33 +08:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " layers " , PROPERTY_HINT_LAYERS_3D_NAVIGATION ) , " set_layers " , " get_layers " ) ;
2020-01-10 19:22:34 +08:00
ADD_SIGNAL ( MethodInfo ( " navigation_mesh_changed " ) ) ;
ADD_SIGNAL ( MethodInfo ( " bake_finished " ) ) ;
}
2021-02-11 04:18:45 +08:00
void NavigationRegion3D : : _navigation_changed ( ) {
2021-06-23 22:49:50 +08:00
update_gizmos ( ) ;
2020-10-29 18:01:28 +08:00
update_configuration_warnings ( ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
NavigationRegion3D : : NavigationRegion3D ( ) {
2020-01-10 19:22:34 +08:00
set_notify_transform ( true ) ;
2020-03-28 02:21:27 +08:00
region = NavigationServer3D : : get_singleton ( ) - > region_create ( ) ;
2020-01-10 19:22:34 +08:00
}
2020-03-27 05:49:16 +08:00
NavigationRegion3D : : ~ NavigationRegion3D ( ) {
2020-05-14 22:41:43 +08:00
if ( navmesh . is_valid ( ) ) {
2021-02-11 04:18:45 +08:00
navmesh - > disconnect ( " changed " , callable_mp ( this , & NavigationRegion3D : : _navigation_changed ) ) ;
2020-05-14 22:41:43 +08:00
}
2020-03-28 02:21:27 +08:00
NavigationServer3D : : get_singleton ( ) - > free ( region ) ;
2020-01-10 19:22:34 +08:00
}