2017-03-05 14:47:28 +00:00
/*************************************************************************/
2020-03-28 12:19:05 +00:00
/* rendering_server_scene.cpp */
2017-03-05 14:47:28 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2017-03-05 14:47:28 +00:00
/*************************************************************************/
2020-01-01 10:16:22 +00:00
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 14:47:28 +00: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. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2020-03-27 18:21:27 +00:00
# include "rendering_server_scene.h"
2020-01-21 20:32:27 +00:00
2018-09-11 16:13:45 +00:00
# include "core/os/os.h"
2020-03-27 18:21:27 +00:00
# include "rendering_server_globals.h"
# include "rendering_server_raster.h"
2020-01-21 20:32:27 +00:00
2019-01-25 11:06:44 +00:00
# include <new>
2020-01-21 20:32:27 +00:00
2016-10-19 14:14:41 +00:00
/* CAMERA API */
2020-03-27 18:21:27 +00:00
RID RenderingServerScene : : camera_create ( ) {
2017-03-05 15:44:50 +00:00
Camera * camera = memnew ( Camera ) ;
return camera_owner . make_rid ( camera ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_perspective ( RID p_camera , float p_fovy_degrees , float p_z_near , float p_z_far ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > type = Camera : : PERSPECTIVE ;
camera - > fov = p_fovy_degrees ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_orthogonal ( RID p_camera , float p_size , float p_z_near , float p_z_far ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > type = Camera : : ORTHOGONAL ;
camera - > size = p_size ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_frustum ( RID p_camera , float p_size , Vector2 p_offset , float p_z_near , float p_z_far ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2019-02-19 16:17:02 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > type = Camera : : FRUSTUM ;
camera - > size = p_size ;
camera - > offset = p_offset ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_transform ( RID p_camera , const Transform & p_transform ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > transform = p_transform . orthonormalized ( ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_cull_mask ( RID p_camera , uint32_t p_layers ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > visible_layers = p_layers ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_environment ( RID p_camera , RID p_env ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > env = p_env ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_camera_effects ( RID p_camera , RID p_fx ) {
2020-01-13 18:37:24 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
ERR_FAIL_COND ( ! camera ) ;
camera - > effects = p_fx ;
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : camera_set_use_vertical_aspect ( RID p_camera , bool p_enable ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! camera ) ;
2017-03-05 15:44:50 +00:00
camera - > vaspect = p_enable ;
2016-10-19 14:14:41 +00:00
}
/* SCENARIO API */
2020-03-27 18:21:27 +00:00
void * RenderingServerScene : : _instance_pair ( void * p_self , OctreeElementID , Instance * p_A , int , OctreeElementID , Instance * p_B , int ) {
//RenderingServerScene *self = (RenderingServerScene*)p_self;
2016-10-19 14:14:41 +00:00
Instance * A = p_A ;
Instance * B = p_B ;
//instance indices are designed so greater always contains lesser
if ( A - > base_type > B - > base_type ) {
2017-03-05 15:44:50 +00:00
SWAP ( A , B ) ; //lesser always first
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
if ( B - > base_type = = RS : : INSTANCE_LIGHT & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-10-19 14:14:41 +00:00
InstanceLightData : : PairInfo pinfo ;
2017-03-05 15:44:50 +00:00
pinfo . geometry = A ;
2016-10-19 14:14:41 +00:00
pinfo . L = geom - > lighting . push_back ( B ) ;
List < InstanceLightData : : PairInfo > : : Element * E = light - > geometries . push_back ( pinfo ) ;
2016-11-10 02:55:06 +00:00
if ( geom - > can_cast_shadows ) {
2017-03-05 15:44:50 +00:00
light - > shadow_dirty = true ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > lighting_dirty = true ;
2016-10-19 14:14:41 +00:00
2016-11-19 16:23:37 +00:00
return E ; //this element should make freeing faster
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_REFLECTION_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-11-19 16:23:37 +00:00
InstanceReflectionProbeData : : PairInfo pinfo ;
2017-03-05 15:44:50 +00:00
pinfo . geometry = A ;
2016-11-19 16:23:37 +00:00
pinfo . L = geom - > reflection_probes . push_back ( B ) ;
List < InstanceReflectionProbeData : : PairInfo > : : Element * E = reflection_probe - > geometries . push_back ( pinfo ) ;
2017-03-05 15:44:50 +00:00
geom - > reflection_dirty = true ;
2016-11-19 16:23:37 +00:00
2020-04-14 03:05:21 +00:00
return E ; //this element should make freeing faster
} else if ( B - > base_type = = RS : : INSTANCE_DECAL & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
InstanceDecalData : : PairInfo pinfo ;
pinfo . geometry = A ;
pinfo . L = geom - > decals . push_back ( B ) ;
List < InstanceDecalData : : PairInfo > : : Element * E = decal - > geometries . push_back ( pinfo ) ;
geom - > decal_dirty = true ;
2017-12-14 11:59:46 +00:00
return E ; //this element should make freeing faster
2020-05-01 12:34:23 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_LIGHTMAP & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( B - > base_data ) ;
2017-12-14 11:59:46 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-05-01 12:34:23 +00:00
if ( A - > dynamic_gi ) {
InstanceLightmapData : : PairInfo pinfo ;
pinfo . geometry = A ;
pinfo . L = geom - > lightmap_captures . push_back ( B ) ;
List < InstanceLightmapData : : PairInfo > : : Element * E = lightmap_data - > geometries . push_back ( pinfo ) ;
( ( RenderingServerScene * ) p_self ) - > _instance_queue_update ( A , false , false ) ; //need to update capture
return E ; //this element should make freeing faster
} else {
return nullptr ;
}
2017-12-14 11:59:46 +00:00
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_GI_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-12-20 03:21:07 +00:00
InstanceGIProbeData : : PairInfo pinfo ;
2017-03-05 15:44:50 +00:00
pinfo . geometry = A ;
2016-12-20 03:21:07 +00:00
pinfo . L = geom - > gi_probes . push_back ( B ) ;
2019-10-11 02:14:56 +00:00
List < InstanceGIProbeData : : PairInfo > : : Element * E ;
if ( A - > dynamic_gi ) {
E = gi_probe - > dynamic_geometries . push_back ( pinfo ) ;
} else {
E = gi_probe - > geometries . push_back ( pinfo ) ;
}
2016-12-20 03:21:07 +00:00
2017-03-05 15:44:50 +00:00
geom - > gi_probes_dirty = true ;
2016-12-20 03:21:07 +00:00
return E ; //this element should make freeing faster
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_GI_PROBE & & A - > base_type = = RS : : INSTANCE_LIGHT ) {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( B - > base_data ) ;
2016-12-20 03:21:07 +00:00
return gi_probe - > lights . insert ( A ) ;
2016-10-19 14:14:41 +00:00
}
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : _instance_unpair ( void * p_self , OctreeElementID , Instance * p_A , int , OctreeElementID , Instance * p_B , int , void * udata ) {
//RenderingServerScene *self = (RenderingServerScene*)p_self;
2016-10-19 14:14:41 +00:00
Instance * A = p_A ;
Instance * B = p_B ;
//instance indices are designed so greater always contains lesser
if ( A - > base_type > B - > base_type ) {
2017-03-05 15:44:50 +00:00
SWAP ( A , B ) ; //lesser always first
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
if ( B - > base_type = = RS : : INSTANCE_LIGHT & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
List < InstanceLightData : : PairInfo > : : Element * E = reinterpret_cast < List < InstanceLightData : : PairInfo > : : Element * > ( udata ) ;
2016-10-19 14:14:41 +00:00
geom - > lighting . erase ( E - > get ( ) . L ) ;
light - > geometries . erase ( E ) ;
2016-11-10 02:55:06 +00:00
if ( geom - > can_cast_shadows ) {
2017-03-05 15:44:50 +00:00
light - > shadow_dirty = true ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > lighting_dirty = true ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_REFLECTION_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
List < InstanceReflectionProbeData : : PairInfo > : : Element * E = reinterpret_cast < List < InstanceReflectionProbeData : : PairInfo > : : Element * > ( udata ) ;
2016-11-19 16:23:37 +00:00
geom - > reflection_probes . erase ( E - > get ( ) . L ) ;
reflection_probe - > geometries . erase ( E ) ;
2017-03-05 15:44:50 +00:00
geom - > reflection_dirty = true ;
2020-04-14 03:05:21 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_DECAL & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
List < InstanceDecalData : : PairInfo > : : Element * E = reinterpret_cast < List < InstanceDecalData : : PairInfo > : : Element * > ( udata ) ;
geom - > decals . erase ( E - > get ( ) . L ) ;
decal - > geometries . erase ( E ) ;
geom - > decal_dirty = true ;
2020-05-01 12:34:23 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_LIGHTMAP & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
if ( udata ) { //only for dynamic geometries
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
List < InstanceLightmapData : : PairInfo > : : Element * E = reinterpret_cast < List < InstanceLightmapData : : PairInfo > : : Element * > ( udata ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
geom - > lightmap_captures . erase ( E - > get ( ) . L ) ;
lightmap_data - > geometries . erase ( E ) ;
( ( RenderingServerScene * ) p_self ) - > _instance_queue_update ( A , false , false ) ; //need to update capture
}
2016-11-19 16:23:37 +00:00
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_GI_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2016-12-20 03:21:07 +00:00
2017-03-05 15:44:50 +00:00
List < InstanceGIProbeData : : PairInfo > : : Element * E = reinterpret_cast < List < InstanceGIProbeData : : PairInfo > : : Element * > ( udata ) ;
2016-12-20 03:21:07 +00:00
geom - > gi_probes . erase ( E - > get ( ) . L ) ;
2019-10-11 02:14:56 +00:00
if ( A - > dynamic_gi ) {
gi_probe - > dynamic_geometries . erase ( E ) ;
} else {
gi_probe - > geometries . erase ( E ) ;
}
2016-12-20 03:21:07 +00:00
2017-03-05 15:44:50 +00:00
geom - > gi_probes_dirty = true ;
2016-12-20 03:21:07 +00:00
2020-03-27 18:21:27 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_GI_PROBE & & A - > base_type = = RS : : INSTANCE_LIGHT ) {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( B - > base_data ) ;
Set < Instance * > : : Element * E = reinterpret_cast < Set < Instance * > : : Element * > ( udata ) ;
2016-12-20 03:21:07 +00:00
gi_probe - > lights . erase ( E ) ;
2016-10-19 14:14:41 +00:00
}
}
2020-03-27 18:21:27 +00:00
RID RenderingServerScene : : scenario_create ( ) {
2017-03-05 15:44:50 +00:00
Scenario * scenario = memnew ( Scenario ) ;
ERR_FAIL_COND_V ( ! scenario , RID ( ) ) ;
RID scenario_rid = scenario_owner . make_rid ( scenario ) ;
scenario - > self = scenario_rid ;
scenario - > octree . set_pair_callback ( _instance_pair , this ) ;
scenario - > octree . set_unpair_callback ( _instance_unpair , this ) ;
2020-03-27 18:21:27 +00:00
scenario - > reflection_probe_shadow_atlas = RSG : : scene_render - > shadow_atlas_create ( ) ;
RSG : : scene_render - > shadow_atlas_set_size ( scenario - > reflection_probe_shadow_atlas , 1024 ) ; //make enough shadows for close distance, don't bother with rest
RSG : : scene_render - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 0 , 4 ) ;
RSG : : scene_render - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 1 , 4 ) ;
RSG : : scene_render - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 2 , 4 ) ;
RSG : : scene_render - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 3 , 8 ) ;
scenario - > reflection_atlas = RSG : : scene_render - > reflection_atlas_create ( ) ;
2016-10-19 14:14:41 +00:00
return scenario_rid ;
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : scenario_set_debug ( RID p_scenario , RS : : ScenarioDebugMode p_debug_mode ) {
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2017-03-05 15:44:50 +00:00
scenario - > debug = p_debug_mode ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : scenario_set_environment ( RID p_scenario , RID p_environment ) {
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2017-03-05 15:44:50 +00:00
scenario - > environment = p_environment ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : scenario_set_camera_effects ( RID p_scenario , RID p_camera_effects ) {
2020-01-13 18:37:24 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
ERR_FAIL_COND ( ! scenario ) ;
scenario - > camera_effects = p_camera_effects ;
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : scenario_set_fallback_environment ( RID p_scenario , RID p_environment ) {
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2017-03-05 15:44:50 +00:00
scenario - > fallback_environment = p_environment ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : scenario_set_reflection_atlas_size ( RID p_scenario , int p_reflection_size , int p_reflection_count ) {
2019-09-09 20:50:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
ERR_FAIL_COND ( ! scenario ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > reflection_atlas_set_size ( scenario - > reflection_atlas , p_reflection_size , p_reflection_count ) ;
2019-09-09 20:50:51 +00:00
}
2016-10-19 14:14:41 +00:00
/* INSTANCING API */
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : _instance_queue_update ( Instance * p_instance , bool p_update_aabb , bool p_update_dependencies ) {
2020-05-14 14:41:43 +00:00
if ( p_update_aabb ) {
2017-03-05 15:44:50 +00:00
p_instance - > update_aabb = true ;
2020-05-14 14:41:43 +00:00
}
if ( p_update_dependencies ) {
2019-07-21 14:31:30 +00:00
p_instance - > update_dependencies = true ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2020-05-14 14:41:43 +00:00
if ( p_instance - > update_item . in_list ( ) ) {
2016-10-19 14:14:41 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
_instance_update_list . add ( & p_instance - > update_item ) ;
}
2020-03-27 18:21:27 +00:00
RID RenderingServerScene : : instance_create ( ) {
2017-03-05 15:44:50 +00:00
Instance * instance = memnew ( Instance ) ;
ERR_FAIL_COND_V ( ! instance , RID ( ) ) ;
2016-10-19 14:14:41 +00:00
RID instance_rid = instance_owner . make_rid ( instance ) ;
2017-03-05 15:44:50 +00:00
instance - > self = instance_rid ;
2016-10-19 14:14:41 +00:00
return instance_rid ;
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_base ( RID p_instance , RID p_base ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
Scenario * scenario = instance - > scenario ;
2020-03-27 18:21:27 +00:00
if ( instance - > base_type ! = RS : : INSTANCE_NONE ) {
2016-10-19 14:14:41 +00:00
//free anything related to that base
if ( scenario & & instance - > octree_id ) {
scenario - > octree . erase ( instance - > octree_id ) ; //make dependencies generated by the octree go away
2017-03-05 15:44:50 +00:00
instance - > octree_id = 0 ;
2016-10-19 14:14:41 +00:00
}
2017-03-05 15:44:50 +00:00
switch ( instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_LIGHT : {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2020-06-25 13:33:28 +00:00
2020-07-23 10:51:29 +00:00
if ( scenario & & RSG : : storage - > light_get_type ( instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
scenario - > dynamic_lights . erase ( light - > instance ) ;
2020-06-25 13:33:28 +00:00
}
2019-10-05 13:27:43 +00:00
# ifdef DEBUG_ENABLED
if ( light - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from light. " ) ;
}
# endif
2020-07-23 10:51:29 +00:00
if ( scenario & & light - > D ) {
scenario - > directional_lights . erase ( light - > D ) ;
2020-04-01 23:20:12 +00:00
light - > D = nullptr ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > free ( light - > instance ) ;
2016-10-27 14:50:26 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( instance - > base_data ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > free ( reflection_probe - > instance ) ;
2016-11-19 16:23:37 +00:00
if ( reflection_probe - > update_list . in_list ( ) ) {
reflection_probe_render_list . remove ( & reflection_probe - > update_list ) ;
}
} break ;
2020-04-14 03:05:21 +00:00
case RS : : INSTANCE_DECAL : {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( instance - > base_data ) ;
RSG : : scene_render - > free ( decal - > instance ) ;
} break ;
2020-05-01 12:34:23 +00:00
case RS : : INSTANCE_LIGHTMAP : {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( instance - > base_data ) ;
2017-12-14 11:59:46 +00:00
//erase dependencies, since no longer a lightmap
2020-05-01 12:34:23 +00:00
while ( lightmap_data - > users . front ( ) ) {
instance_geometry_set_lightmap ( lightmap_data - > users . front ( ) - > get ( ) - > self , RID ( ) , Rect2 ( ) , 0 ) ;
2017-12-14 11:59:46 +00:00
}
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_GI_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( instance - > base_data ) ;
2019-10-05 13:27:43 +00:00
# ifdef DEBUG_ENABLED
if ( gi_probe - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from GIProbe. " ) ;
}
# endif
# ifdef DEBUG_ENABLED
if ( gi_probe - > lights . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair lights from GIProbe. " ) ;
}
# endif
2016-12-20 03:21:07 +00:00
if ( gi_probe - > update_element . in_list ( ) ) {
gi_probe_update_list . remove ( & gi_probe - > update_element ) ;
}
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > free ( gi_probe - > probe_instance ) ;
2016-12-20 03:21:07 +00:00
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-19 14:14:41 +00:00
}
if ( instance - > base_data ) {
2017-03-05 15:44:50 +00:00
memdelete ( instance - > base_data ) ;
2020-04-01 23:20:12 +00:00
instance - > base_data = nullptr ;
2016-10-19 14:14:41 +00:00
}
2017-01-12 11:34:00 +00:00
instance - > blend_values . clear ( ) ;
2016-10-19 14:14:41 +00:00
instance - > materials . clear ( ) ;
}
2020-03-27 18:21:27 +00:00
instance - > base_type = RS : : INSTANCE_NONE ;
2017-03-05 15:44:50 +00:00
instance - > base = RID ( ) ;
2016-10-19 14:14:41 +00:00
if ( p_base . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
instance - > base_type = RSG : : storage - > get_base_type ( p_base ) ;
ERR_FAIL_COND ( instance - > base_type = = RS : : INSTANCE_NONE ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
switch ( instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_LIGHT : {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = memnew ( InstanceLightData ) ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
if ( scenario & & RSG : : storage - > light_get_type ( p_base ) = = RS : : LIGHT_DIRECTIONAL ) {
2016-10-19 14:14:41 +00:00
light - > D = scenario - > directional_lights . push_back ( instance ) ;
}
2020-03-27 18:21:27 +00:00
light - > instance = RSG : : scene_render - > light_instance_create ( p_base ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
instance - > base_data = light ;
2016-10-27 14:50:26 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_MESH :
case RS : : INSTANCE_MULTIMESH :
case RS : : INSTANCE_IMMEDIATE :
case RS : : INSTANCE_PARTICLES : {
2017-03-05 15:44:50 +00:00
InstanceGeometryData * geom = memnew ( InstanceGeometryData ) ;
instance - > base_data = geom ;
2020-03-27 18:21:27 +00:00
if ( instance - > base_type = = RS : : INSTANCE_MESH ) {
instance - > blend_values . resize ( RSG : : storage - > mesh_get_blend_shape_count ( p_base ) ) ;
2019-03-01 19:01:21 +00:00
}
2016-11-22 04:26:56 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = memnew ( InstanceReflectionProbeData ) ;
reflection_probe - > owner = instance ;
instance - > base_data = reflection_probe ;
2016-11-19 16:23:37 +00:00
2020-03-27 18:21:27 +00:00
reflection_probe - > instance = RSG : : scene_render - > reflection_probe_instance_create ( p_base ) ;
2016-11-19 16:23:37 +00:00
} break ;
2020-04-14 03:05:21 +00:00
case RS : : INSTANCE_DECAL : {
InstanceDecalData * decal = memnew ( InstanceDecalData ) ;
decal - > owner = instance ;
instance - > base_data = decal ;
decal - > instance = RSG : : scene_render - > decal_instance_create ( p_base ) ;
} break ;
2020-05-01 12:34:23 +00:00
case RS : : INSTANCE_LIGHTMAP : {
InstanceLightmapData * lightmap_data = memnew ( InstanceLightmapData ) ;
instance - > base_data = lightmap_data ;
//lightmap_data->instance = RSG::scene_render->lightmap_data_instance_create(p_base);
2017-12-14 11:59:46 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_GI_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = memnew ( InstanceGIProbeData ) ;
instance - > base_data = gi_probe ;
gi_probe - > owner = instance ;
2016-12-20 03:21:07 +00:00
if ( scenario & & ! gi_probe - > update_element . in_list ( ) ) {
gi_probe_update_list . add ( & gi_probe - > update_element ) ;
}
2020-03-27 18:21:27 +00:00
gi_probe - > probe_instance = RSG : : scene_render - > gi_probe_instance_create ( p_base ) ;
2016-12-20 03:21:07 +00:00
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-19 14:14:41 +00:00
}
2017-03-05 15:44:50 +00:00
instance - > base = p_base ;
2016-10-19 14:14:41 +00:00
2019-07-21 14:31:30 +00:00
//forcefully update the dependency now, so if for some reason it gets removed, we can immediately clear it
2020-03-27 18:21:27 +00:00
RSG : : storage - > base_update_dependency ( p_base , instance ) ;
2016-10-19 14:14:41 +00:00
}
2019-07-21 14:31:30 +00:00
_instance_queue_update ( instance , true , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_scenario ( RID p_instance , RID p_scenario ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
if ( instance - > scenario ) {
2017-03-05 15:44:50 +00:00
instance - > scenario - > instances . remove ( & instance - > scenario_item ) ;
2016-10-19 14:14:41 +00:00
if ( instance - > octree_id ) {
instance - > scenario - > octree . erase ( instance - > octree_id ) ; //make dependencies generated by the octree go away
2017-03-05 15:44:50 +00:00
instance - > octree_id = 0 ;
2016-10-19 14:14:41 +00:00
}
2017-03-05 15:44:50 +00:00
switch ( instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_LIGHT : {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2019-10-05 13:27:43 +00:00
# ifdef DEBUG_ENABLED
if ( light - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from light. " ) ;
}
# endif
2016-10-19 14:14:41 +00:00
if ( light - > D ) {
2017-03-05 15:44:50 +00:00
instance - > scenario - > directional_lights . erase ( light - > D ) ;
2020-04-01 23:20:12 +00:00
light - > D = nullptr ;
2016-10-19 14:14:41 +00:00
}
2016-10-27 14:50:26 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
2019-09-09 20:50:51 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( instance - > base_data ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > reflection_probe_release_atlas_index ( reflection_probe - > instance ) ;
2016-11-19 16:23:37 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_GI_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( instance - > base_data ) ;
2019-10-05 13:27:43 +00:00
# ifdef DEBUG_ENABLED
if ( gi_probe - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from GIProbe. " ) ;
}
# endif
# ifdef DEBUG_ENABLED
if ( gi_probe - > lights . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair lights from GIProbe. " ) ;
}
# endif
2016-12-20 03:21:07 +00:00
if ( gi_probe - > update_element . in_list ( ) ) {
gi_probe_update_list . remove ( & gi_probe - > update_element ) ;
}
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-19 14:14:41 +00:00
}
2020-04-01 23:20:12 +00:00
instance - > scenario = nullptr ;
2016-10-19 14:14:41 +00:00
}
if ( p_scenario . is_valid ( ) ) {
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2016-10-19 14:14:41 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2017-03-05 15:44:50 +00:00
instance - > scenario = scenario ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
scenario - > instances . add ( & instance - > scenario_item ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
switch ( instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_LIGHT : {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > light_get_type ( instance - > base ) = = RS : : LIGHT_DIRECTIONAL ) {
2016-10-19 14:14:41 +00:00
light - > D = scenario - > directional_lights . push_back ( instance ) ;
}
2016-10-27 14:50:26 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_GI_PROBE : {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( instance - > base_data ) ;
2016-12-20 03:21:07 +00:00
if ( ! gi_probe - > update_element . in_list ( ) ) {
gi_probe_update_list . add ( & gi_probe - > update_element ) ;
}
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-19 14:14:41 +00:00
}
2017-03-05 15:44:50 +00:00
_instance_queue_update ( instance , true , true ) ;
2016-10-19 14:14:41 +00:00
}
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_layer_mask ( RID p_instance , uint32_t p_mask ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
instance - > layer_mask = p_mask ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_transform ( RID p_instance , const Transform & p_transform ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
2020-05-14 14:41:43 +00:00
if ( instance - > transform = = p_transform ) {
2016-10-19 14:14:41 +00:00
return ; //must be checked to avoid worst evil
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2019-01-26 22:09:48 +00:00
# ifdef DEBUG_ENABLED
for ( int i = 0 ; i < 4 ; i + + ) {
const Vector3 & v = i < 3 ? p_transform . basis . elements [ i ] : p_transform . origin ;
ERR_FAIL_COND ( Math : : is_inf ( v . x ) ) ;
ERR_FAIL_COND ( Math : : is_nan ( v . x ) ) ;
ERR_FAIL_COND ( Math : : is_inf ( v . y ) ) ;
ERR_FAIL_COND ( Math : : is_nan ( v . y ) ) ;
ERR_FAIL_COND ( Math : : is_inf ( v . z ) ) ;
ERR_FAIL_COND ( Math : : is_nan ( v . z ) ) ;
}
# endif
2017-03-05 15:44:50 +00:00
instance - > transform = p_transform ;
_instance_queue_update ( instance , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_attach_object_instance_id ( RID p_instance , ObjectID p_id ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
2019-05-09 09:21:49 +00:00
instance - > object_id = p_id ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_blend_shape_weight ( RID p_instance , int p_shape , float p_weight ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-11-24 23:46:55 +00:00
if ( instance - > update_item . in_list ( ) ) {
_update_dirty_instance ( instance ) ;
}
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX ( p_shape , instance - > blend_values . size ( ) ) ;
2018-07-25 01:11:03 +00:00
instance - > blend_values . write [ p_shape ] = p_weight ;
2016-10-19 14:14:41 +00:00
}
2016-11-24 23:46:55 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_surface_material ( RID p_instance , int p_surface , RID p_material ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
if ( instance - > base_type = = RS : : INSTANCE_MESH ) {
2019-07-21 14:31:30 +00:00
//may not have been updated yet, may also have not been set yet. When updated will be correcte, worst case
2020-03-27 18:21:27 +00:00
instance - > materials . resize ( MAX ( p_surface + 1 , RSG : : storage - > mesh_get_surface_count ( instance - > base ) ) ) ;
2016-11-24 23:46:55 +00:00
}
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX ( p_surface , instance - > materials . size ( ) ) ;
2016-10-19 14:14:41 +00:00
2018-07-25 01:11:03 +00:00
instance - > materials . write [ p_surface ] = p_material ;
2016-11-10 02:55:06 +00:00
2019-07-21 14:31:30 +00:00
_instance_queue_update ( instance , false , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_visible ( RID p_instance , bool p_visible ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2017-01-04 04:16:14 +00:00
2020-05-14 14:41:43 +00:00
if ( instance - > visible = = p_visible ) {
2017-01-04 04:16:14 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-01-04 04:16:14 +00:00
2017-03-05 15:44:50 +00:00
instance - > visible = p_visible ;
2017-01-04 04:16:14 +00:00
2017-03-05 15:44:50 +00:00
switch ( instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_LIGHT : {
if ( RSG : : storage - > light_get_type ( instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & instance - > octree_id & & instance - > scenario ) {
instance - > scenario - > octree . set_pairable ( instance - > octree_id , p_visible , 1 < < RS : : INSTANCE_LIGHT , p_visible ? RS : : INSTANCE_GEOMETRY_MASK : 0 ) ;
2017-01-04 04:16:14 +00:00
}
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
2017-01-04 04:16:14 +00:00
if ( instance - > octree_id & & instance - > scenario ) {
2020-03-27 18:21:27 +00:00
instance - > scenario - > octree . set_pairable ( instance - > octree_id , p_visible , 1 < < RS : : INSTANCE_REFLECTION_PROBE , p_visible ? RS : : INSTANCE_GEOMETRY_MASK : 0 ) ;
2017-01-04 04:16:14 +00:00
}
2020-04-14 03:05:21 +00:00
} break ;
case RS : : INSTANCE_DECAL : {
if ( instance - > octree_id & & instance - > scenario ) {
instance - > scenario - > octree . set_pairable ( instance - > octree_id , p_visible , 1 < < RS : : INSTANCE_DECAL , p_visible ? RS : : INSTANCE_GEOMETRY_MASK : 0 ) ;
}
2017-12-14 11:59:46 +00:00
} break ;
2020-05-01 12:34:23 +00:00
case RS : : INSTANCE_LIGHTMAP : {
2017-12-14 11:59:46 +00:00
if ( instance - > octree_id & & instance - > scenario ) {
2020-05-01 12:34:23 +00:00
instance - > scenario - > octree . set_pairable ( instance - > octree_id , p_visible , 1 < < RS : : INSTANCE_LIGHTMAP , p_visible ? RS : : INSTANCE_GEOMETRY_MASK : 0 ) ;
2017-12-14 11:59:46 +00:00
}
2017-01-04 04:16:14 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_GI_PROBE : {
2017-01-04 04:16:14 +00:00
if ( instance - > octree_id & & instance - > scenario ) {
2020-03-27 18:21:27 +00:00
instance - > scenario - > octree . set_pairable ( instance - > octree_id , p_visible , 1 < < RS : : INSTANCE_GI_PROBE , p_visible ? ( RS : : INSTANCE_GEOMETRY_MASK | ( 1 < < RS : : INSTANCE_LIGHT ) ) : 0 ) ;
2017-01-04 04:16:14 +00:00
}
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2017-01-04 04:16:14 +00:00
}
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
inline bool is_geometry_instance ( RenderingServer : : InstanceType p_type ) {
return p_type = = RS : : INSTANCE_MESH | | p_type = = RS : : INSTANCE_MULTIMESH | | p_type = = RS : : INSTANCE_PARTICLES | | p_type = = RS : : INSTANCE_IMMEDIATE ;
2017-11-20 19:54:26 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_custom_aabb ( RID p_instance , AABB p_aabb ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-11-20 19:54:26 +00:00
ERR_FAIL_COND ( ! instance ) ;
ERR_FAIL_COND ( ! is_geometry_instance ( instance - > base_type ) ) ;
2017-11-25 03:07:54 +00:00
if ( p_aabb ! = AABB ( ) ) {
2017-11-20 19:54:26 +00:00
// Set custom AABB
2020-05-14 14:41:43 +00:00
if ( instance - > custom_aabb = = nullptr ) {
2017-11-20 19:54:26 +00:00
instance - > custom_aabb = memnew ( AABB ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-20 19:54:26 +00:00
* instance - > custom_aabb = p_aabb ;
} else {
// Clear custom AABB
2020-04-01 23:20:12 +00:00
if ( instance - > custom_aabb ! = nullptr ) {
2017-11-20 19:54:26 +00:00
memdelete ( instance - > custom_aabb ) ;
2020-04-01 23:20:12 +00:00
instance - > custom_aabb = nullptr ;
2017-11-20 19:54:26 +00:00
}
}
2020-05-14 14:41:43 +00:00
if ( instance - > scenario ) {
2017-11-20 19:54:26 +00:00
_instance_queue_update ( instance , true , false ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-20 19:54:26 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_attach_skeleton ( RID p_instance , RID p_skeleton ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-19 14:14:41 +00:00
2020-05-14 14:41:43 +00:00
if ( instance - > skeleton = = p_skeleton ) {
2016-11-22 04:26:56 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2016-11-22 04:26:56 +00:00
2017-03-05 15:44:50 +00:00
instance - > skeleton = p_skeleton ;
2016-10-19 14:14:41 +00:00
2019-07-21 14:31:30 +00:00
if ( p_skeleton . is_valid ( ) ) {
//update the dependency now, so if cleared, we remove it
2020-03-27 18:21:27 +00:00
RSG : : storage - > skeleton_update_dependency ( p_skeleton , instance ) ;
2016-11-22 04:26:56 +00:00
}
2019-07-21 14:31:30 +00:00
_instance_queue_update ( instance , true , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_exterior ( RID p_instance , bool p_enabled ) {
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_set_extra_visibility_margin ( RID p_instance , real_t p_margin ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2018-03-04 13:20:59 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > extra_margin = p_margin ;
_instance_queue_update ( instance , true , false ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
Vector < ObjectID > RenderingServerScene : : instances_cull_aabb ( const AABB & p_aabb , RID p_scenario ) const {
2016-10-29 23:48:09 +00:00
Vector < ObjectID > instances ;
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2016-10-29 23:48:09 +00:00
2020-03-27 18:21:27 +00:00
const_cast < RenderingServerScene * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2016-10-29 23:48:09 +00:00
2017-03-05 15:44:50 +00:00
int culled = 0 ;
2016-10-29 23:48:09 +00:00
Instance * cull [ 1024 ] ;
2017-08-16 15:00:07 +00:00
culled = scenario - > octree . cull_aabb ( p_aabb , cull , 1024 ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < culled ; i + + ) {
Instance * instance = cull [ i ] ;
2016-10-29 23:48:09 +00:00
ERR_CONTINUE ( ! instance ) ;
2020-05-14 14:41:43 +00:00
if ( instance - > object_id . is_null ( ) ) {
2016-10-29 23:48:09 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2016-10-29 23:48:09 +00:00
2019-05-09 09:21:49 +00:00
instances . push_back ( instance - > object_id ) ;
2016-10-29 23:48:09 +00:00
}
return instances ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
Vector < ObjectID > RenderingServerScene : : instances_cull_ray ( const Vector3 & p_from , const Vector3 & p_to , RID p_scenario ) const {
2016-10-29 23:48:09 +00:00
Vector < ObjectID > instances ;
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2020-03-27 18:21:27 +00:00
const_cast < RenderingServerScene * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2016-10-29 23:48:09 +00:00
2017-03-05 15:44:50 +00:00
int culled = 0 ;
2016-10-29 23:48:09 +00:00
Instance * cull [ 1024 ] ;
2017-11-25 20:14:54 +00:00
culled = scenario - > octree . cull_segment ( p_from , p_from + p_to * 10000 , cull , 1024 ) ;
2016-10-29 23:48:09 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < culled ; i + + ) {
Instance * instance = cull [ i ] ;
2016-10-29 23:48:09 +00:00
ERR_CONTINUE ( ! instance ) ;
2020-05-14 14:41:43 +00:00
if ( instance - > object_id . is_null ( ) ) {
2016-10-29 23:48:09 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2016-10-29 23:48:09 +00:00
2019-05-09 09:21:49 +00:00
instances . push_back ( instance - > object_id ) ;
2016-10-29 23:48:09 +00:00
}
return instances ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
Vector < ObjectID > RenderingServerScene : : instances_cull_convex ( const Vector < Plane > & p_convex , RID p_scenario ) const {
2016-10-29 23:48:09 +00:00
Vector < ObjectID > instances ;
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2020-03-27 18:21:27 +00:00
const_cast < RenderingServerScene * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2016-10-29 23:48:09 +00:00
2017-03-05 15:44:50 +00:00
int culled = 0 ;
2016-10-29 23:48:09 +00:00
Instance * cull [ 1024 ] ;
2017-03-05 15:44:50 +00:00
culled = scenario - > octree . cull_convex ( p_convex , cull , 1024 ) ;
2016-10-29 23:48:09 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < culled ; i + + ) {
Instance * instance = cull [ i ] ;
2016-10-29 23:48:09 +00:00
ERR_CONTINUE ( ! instance ) ;
2020-05-14 14:41:43 +00:00
if ( instance - > object_id . is_null ( ) ) {
2016-10-29 23:48:09 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2016-10-29 23:48:09 +00:00
2019-05-09 09:21:49 +00:00
instances . push_back ( instance - > object_id ) ;
2016-10-29 23:48:09 +00:00
}
return instances ;
}
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_geometry_set_flag ( RID p_instance , RS : : InstanceFlags p_flags , bool p_enabled ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-27 14:50:26 +00:00
2020-03-27 18:21:27 +00:00
//ERR_FAIL_COND(((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK));
2019-10-11 02:14:56 +00:00
2017-03-05 15:44:50 +00:00
switch ( p_flags ) {
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_FLAG_USE_BAKED_LIGHT : {
2017-07-16 02:24:37 +00:00
instance - > baked_light = p_enabled ;
2019-10-11 02:14:56 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_FLAG_USE_DYNAMIC_GI : {
2019-10-11 02:14:56 +00:00
if ( p_enabled = = instance - > dynamic_gi ) {
//bye, redundant
return ;
}
if ( instance - > octree_id ! = 0 ) {
//remove from octree, it needs to be re-paired
instance - > scenario - > octree . erase ( instance - > octree_id ) ;
instance - > octree_id = 0 ;
_instance_queue_update ( instance , true , true ) ;
}
//once out of octree, can be changed
instance - > dynamic_gi = p_enabled ;
2017-07-16 02:24:37 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE : {
2018-07-23 15:28:15 +00:00
instance - > redraw_if_visible = p_enabled ;
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-27 14:50:26 +00:00
}
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_geometry_set_cast_shadows_setting ( RID p_instance , RS : : ShadowCastingSetting p_shadow_casting_setting ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-08-19 23:06:40 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > cast_shadows = p_shadow_casting_setting ;
2019-07-21 14:31:30 +00:00
_instance_queue_update ( instance , false , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_geometry_set_material_override ( RID p_instance , RID p_material ) {
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_instance ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ! instance ) ;
2016-10-27 14:50:26 +00:00
2017-03-05 15:44:50 +00:00
instance - > material_override = p_material ;
2019-07-21 14:31:30 +00:00
_instance_queue_update ( instance , false , true ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_geometry_set_draw_range ( RID p_instance , float p_min , float p_max , float p_min_margin , float p_max_margin ) {
2016-10-19 14:14:41 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : instance_geometry_set_as_instance_lod ( RID p_instance , RID p_as_lod_of_instance ) {
2016-10-19 14:14:41 +00:00
}
2020-05-01 12:34:23 +00:00
void RenderingServerScene : : instance_geometry_set_lightmap ( RID p_instance , RID p_lightmap , const Rect2 & p_lightmap_uv_scale , int p_slice_index ) {
Instance * instance = instance_owner . getornull ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > lightmap ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( ( ( Instance * ) instance - > lightmap ) - > base_data ) ;
lightmap_data - > users . erase ( instance ) ;
instance - > lightmap = nullptr ;
}
Instance * lightmap_instance = instance_owner . getornull ( p_lightmap ) ;
instance - > lightmap = lightmap_instance ;
instance - > lightmap_uv_scale = p_lightmap_uv_scale ;
instance - > lightmap_slice_index = p_slice_index ;
if ( lightmap_instance ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( lightmap_instance - > base_data ) ;
lightmap_data - > users . insert ( instance ) ;
}
}
2020-04-17 02:52:00 +00:00
void RenderingServerScene : : instance_geometry_set_shader_parameter ( RID p_instance , const StringName & p_parameter , const Variant & p_value ) {
Instance * instance = instance_owner . getornull ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > : : Element * E = instance - > instance_shader_parameters . find ( p_parameter ) ;
if ( ! E ) {
RasterizerScene : : InstanceBase : : InstanceShaderParameter isp ;
isp . index = - 1 ;
isp . info = PropertyInfo ( ) ;
isp . value = p_value ;
instance - > instance_shader_parameters [ p_parameter ] = isp ;
} else {
E - > get ( ) . value = p_value ;
if ( E - > get ( ) . index > = 0 & & instance - > instance_allocated_shader_parameters ) {
//update directly
RSG : : storage - > global_variables_instance_update ( p_instance , E - > get ( ) . index , p_value ) ;
}
}
}
Variant RenderingServerScene : : instance_geometry_get_shader_parameter ( RID p_instance , const StringName & p_parameter ) const {
const Instance * instance = const_cast < RenderingServerScene * > ( this ) - > instance_owner . getornull ( p_instance ) ;
ERR_FAIL_COND_V ( ! instance , Variant ( ) ) ;
if ( instance - > instance_shader_parameters . has ( p_parameter ) ) {
return instance - > instance_shader_parameters [ p_parameter ] . value ;
}
return Variant ( ) ;
}
Variant RenderingServerScene : : instance_geometry_get_shader_parameter_default_value ( RID p_instance , const StringName & p_parameter ) const {
const Instance * instance = const_cast < RenderingServerScene * > ( this ) - > instance_owner . getornull ( p_instance ) ;
ERR_FAIL_COND_V ( ! instance , Variant ( ) ) ;
if ( instance - > instance_shader_parameters . has ( p_parameter ) ) {
return instance - > instance_shader_parameters [ p_parameter ] . default_value ;
}
return Variant ( ) ;
}
void RenderingServerScene : : instance_geometry_get_shader_parameter_list ( RID p_instance , List < PropertyInfo > * p_parameters ) const {
const Instance * instance = const_cast < RenderingServerScene * > ( this ) - > instance_owner . getornull ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
const_cast < RenderingServerScene * > ( this ) - > update_dirty_instances ( ) ;
Vector < StringName > names ;
for ( Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > : : Element * E = instance - > instance_shader_parameters . front ( ) ; E ; E = E - > next ( ) ) {
names . push_back ( E - > key ( ) ) ;
}
names . sort_custom < StringName : : AlphCompare > ( ) ;
for ( int i = 0 ; i < names . size ( ) ; i + + ) {
PropertyInfo pinfo = instance - > instance_shader_parameters [ names [ i ] ] . info ;
p_parameters - > push_back ( pinfo ) ;
}
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : _update_instance ( Instance * p_instance ) {
2016-10-19 14:14:41 +00:00
p_instance - > version + + ;
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_LIGHT ) {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > light_instance_set_transform ( light - > instance , p_instance - > transform ) ;
2020-06-25 13:33:28 +00:00
RSG : : scene_render - > light_instance_set_aabb ( light - > instance , p_instance - > transform . xform ( p_instance - > aabb ) ) ;
2017-03-05 15:44:50 +00:00
light - > shadow_dirty = true ;
2020-06-25 13:33:28 +00:00
RS : : LightBakeMode bake_mode = RSG : : storage - > light_get_bake_mode ( p_instance - > base ) ;
if ( RSG : : storage - > light_get_type ( p_instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & bake_mode ! = light - > bake_mode ) {
2020-07-23 10:51:29 +00:00
if ( p_instance - > scenario & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2020-06-25 13:33:28 +00:00
p_instance - > scenario - > dynamic_lights . erase ( light - > instance ) ;
}
light - > bake_mode = bake_mode ;
2020-07-23 10:51:29 +00:00
if ( p_instance - > scenario & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2020-06-25 13:33:28 +00:00
p_instance - > scenario - > dynamic_lights . push_back ( light - > instance ) ;
}
}
uint32_t max_sdfgi_cascade = RSG : : storage - > light_get_max_sdfgi_cascade ( p_instance - > base ) ;
if ( light - > max_sdfgi_cascade ! = max_sdfgi_cascade ) {
light - > max_sdfgi_cascade = max_sdfgi_cascade ; //should most likely make sdfgi dirty in scenario
}
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_REFLECTION_PROBE ) {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( p_instance - > base_data ) ;
2016-11-19 16:23:37 +00:00
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > reflection_probe_instance_set_transform ( reflection_probe - > instance , p_instance - > transform ) ;
2017-03-05 15:44:50 +00:00
reflection_probe - > reflection_dirty = true ;
2016-11-19 16:23:37 +00:00
}
2020-04-14 03:05:21 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_DECAL ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( p_instance - > base_data ) ;
RSG : : scene_render - > decal_instance_set_transform ( decal - > instance , p_instance - > transform ) ;
}
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_GI_PROBE ) {
2019-10-03 20:39:08 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( p_instance - > base_data ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > gi_probe_instance_set_transform_to_data ( gi_probe - > probe_instance , p_instance - > transform ) ;
2019-10-03 20:39:08 +00:00
}
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
RSG : : storage - > particles_set_emission_transform ( p_instance - > base , p_instance - > transform ) ;
2017-04-07 02:36:37 +00:00
}
2017-08-19 17:54:04 +00:00
if ( p_instance - > aabb . has_no_surface ( ) ) {
2016-10-19 14:14:41 +00:00
return ;
2017-08-18 18:11:16 +00:00
}
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
2017-03-05 15:44:50 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2016-11-10 02:55:06 +00:00
//make sure lights are updated if it casts shadow
2016-10-19 14:14:41 +00:00
2016-11-10 02:55:06 +00:00
if ( geom - > can_cast_shadows ) {
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = geom - > lighting . front ( ) ; E ; E = E - > next ( ) ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > get ( ) - > base_data ) ;
light - > shadow_dirty = true ;
2016-11-10 02:55:06 +00:00
}
2016-10-19 14:14:41 +00:00
}
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
if ( ! p_instance - > lightmap & & geom - > lightmap_captures . size ( ) ) {
2017-12-14 11:59:46 +00:00
//affected by lightmap captures, must update capture info!
_update_instance_lightmap_captures ( p_instance ) ;
} else {
2020-05-01 12:34:23 +00:00
if ( ! p_instance - > lightmap_sh . empty ( ) ) {
p_instance - > lightmap_sh . clear ( ) ; //don't need SH
p_instance - > lightmap_target_sh . clear ( ) ; //don't need SH
2017-12-14 11:59:46 +00:00
}
}
2016-10-19 14:14:41 +00:00
}
2020-05-01 12:34:23 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_LIGHTMAP ) {
//if this moved, update the captured objects
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( p_instance - > base_data ) ;
//erase dependencies, since no longer a lightmap
for ( List < InstanceLightmapData : : PairInfo > : : Element * E = lightmap_data - > geometries . front ( ) ; E ; E = E - > next ( ) ) {
Instance * geom = E - > get ( ) . geometry ;
_instance_queue_update ( geom , true , false ) ;
}
}
2016-10-19 14:14:41 +00:00
p_instance - > mirror = p_instance - > transform . basis . determinant ( ) < 0.0 ;
2017-11-17 02:09:00 +00:00
AABB new_aabb ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
new_aabb = p_instance - > transform . xform ( p_instance - > aabb ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
p_instance - > transformed_aabb = new_aabb ;
2016-10-19 14:14:41 +00:00
if ( ! p_instance - > scenario ) {
return ;
}
2017-03-05 15:44:50 +00:00
if ( p_instance - > octree_id = = 0 ) {
uint32_t base_type = 1 < < p_instance - > base_type ;
uint32_t pairable_mask = 0 ;
bool pairable = false ;
2016-10-19 14:14:41 +00:00
2020-05-01 12:34:23 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_LIGHT | | p_instance - > base_type = = RS : : INSTANCE_REFLECTION_PROBE | | p_instance - > base_type = = RS : : INSTANCE_DECAL | | p_instance - > base_type = = RS : : INSTANCE_LIGHTMAP ) {
2020-03-27 18:21:27 +00:00
pairable_mask = p_instance - > visible ? RS : : INSTANCE_GEOMETRY_MASK : 0 ;
2017-03-05 15:44:50 +00:00
pairable = true ;
2016-10-19 14:14:41 +00:00
}
2016-12-20 03:21:07 +00:00
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_GI_PROBE ) {
2016-12-20 03:21:07 +00:00
//lights and geometries
2020-03-27 18:21:27 +00:00
pairable_mask = p_instance - > visible ? RS : : INSTANCE_GEOMETRY_MASK | ( 1 < < RS : : INSTANCE_LIGHT ) : 0 ;
2017-03-05 15:44:50 +00:00
pairable = true ;
2016-12-20 03:21:07 +00:00
}
2016-10-19 14:14:41 +00:00
// not inside octree
2017-03-05 15:44:50 +00:00
p_instance - > octree_id = p_instance - > scenario - > octree . create ( p_instance , new_aabb , 0 , pairable , base_type , pairable_mask ) ;
2016-10-19 14:14:41 +00:00
} else {
2017-01-14 11:26:56 +00:00
/*
if ( new_aabb = = p_instance - > data . transformed_aabb )
return ;
*/
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
p_instance - > scenario - > octree . move ( p_instance - > octree_id , new_aabb ) ;
2016-10-19 14:14:41 +00:00
}
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : _update_instance_aabb ( Instance * p_instance ) {
2017-11-17 02:09:00 +00:00
AABB new_aabb ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND ( p_instance - > base_type ! = RS : : INSTANCE_NONE & & ! p_instance - > base . is_valid ( ) ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
switch ( p_instance - > base_type ) {
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_NONE : {
2016-10-19 14:14:41 +00:00
// do nothing
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_MESH : {
2020-05-14 14:41:43 +00:00
if ( p_instance - > custom_aabb ) {
2017-11-20 19:54:26 +00:00
new_aabb = * p_instance - > custom_aabb ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
new_aabb = RSG : : storage - > mesh_get_aabb ( p_instance - > base , p_instance - > skeleton ) ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
} break ;
2016-11-22 04:26:56 +00:00
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_MULTIMESH : {
2020-05-14 14:41:43 +00:00
if ( p_instance - > custom_aabb ) {
2017-11-20 19:54:26 +00:00
new_aabb = * p_instance - > custom_aabb ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
new_aabb = RSG : : storage - > multimesh_get_aabb ( p_instance - > base ) ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_IMMEDIATE : {
2020-05-14 14:41:43 +00:00
if ( p_instance - > custom_aabb ) {
2017-11-20 19:54:26 +00:00
new_aabb = * p_instance - > custom_aabb ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
new_aabb = RSG : : storage - > immediate_get_aabb ( p_instance - > base ) ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_PARTICLES : {
2020-05-14 14:41:43 +00:00
if ( p_instance - > custom_aabb ) {
2017-11-20 19:54:26 +00:00
new_aabb = * p_instance - > custom_aabb ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
new_aabb = RSG : : storage - > particles_get_aabb ( p_instance - > base ) ;
2020-05-14 14:41:43 +00:00
}
2017-04-07 02:36:37 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_LIGHT : {
new_aabb = RSG : : storage - > light_get_aabb ( p_instance - > base ) ;
2016-10-19 14:14:41 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_REFLECTION_PROBE : {
new_aabb = RSG : : storage - > reflection_probe_get_aabb ( p_instance - > base ) ;
2016-11-19 16:23:37 +00:00
2020-04-14 03:05:21 +00:00
} break ;
case RenderingServer : : INSTANCE_DECAL : {
new_aabb = RSG : : storage - > decal_get_aabb ( p_instance - > base ) ;
2016-11-19 16:23:37 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RenderingServer : : INSTANCE_GI_PROBE : {
new_aabb = RSG : : storage - > gi_probe_get_bounds ( p_instance - > base ) ;
2016-12-20 03:21:07 +00:00
2017-12-14 11:59:46 +00:00
} break ;
2020-05-01 12:34:23 +00:00
case RenderingServer : : INSTANCE_LIGHTMAP : {
new_aabb = RSG : : storage - > lightmap_get_aabb ( p_instance - > base ) ;
2017-12-14 11:59:46 +00:00
2016-12-20 03:21:07 +00:00
} break ;
2019-04-09 15:08:36 +00:00
default : {
}
2016-10-19 14:14:41 +00:00
}
2017-11-20 19:54:26 +00:00
// <Zylann> This is why I didn't re-use Instance::aabb to implement custom AABBs
2020-05-14 14:41:43 +00:00
if ( p_instance - > extra_margin ) {
2016-10-19 14:14:41 +00:00
new_aabb . grow_by ( p_instance - > extra_margin ) ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
p_instance - > aabb = new_aabb ;
2016-10-19 14:14:41 +00:00
}
2020-05-01 12:34:23 +00:00
void RenderingServerScene : : _update_instance_lightmap_captures ( Instance * p_instance ) {
bool first_set = p_instance - > lightmap_sh . size ( ) = = 0 ;
p_instance - > lightmap_sh . resize ( 9 ) ; //using SH
p_instance - > lightmap_target_sh . resize ( 9 ) ; //using SH
Color * instance_sh = p_instance - > lightmap_target_sh . ptrw ( ) ;
bool inside = false ;
Color accum_sh [ 9 ] ;
float accum_blend = 0.0 ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
for ( List < Instance * > : : Element * E = geom - > lightmap_captures . front ( ) ; E ; E = E - > next ( ) ) {
Instance * lightmap = E - > get ( ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
bool interior = RSG : : storage - > lightmap_is_interior ( lightmap - > base ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
if ( inside & & ! interior ) {
continue ; //we are inside, ignore exteriors
}
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
Transform to_bounds = lightmap - > transform . affine_inverse ( ) ;
Vector3 center = p_instance - > transform . xform ( p_instance - > aabb . position + p_instance - > aabb . size * 0.5 ) ; //use aabb center
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
Vector3 lm_pos = to_bounds . xform ( center ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
AABB bounds = RSG : : storage - > lightmap_get_aabb ( lightmap - > base ) ;
if ( ! bounds . has_point ( lm_pos ) ) {
continue ; //not in this lightmap
}
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
Color sh [ 9 ] ;
RSG : : storage - > lightmap_tap_sh_light ( lightmap - > base , lm_pos , sh ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
//rotate it
Basis rot = lightmap - > transform . basis . orthonormalized ( ) ;
for ( int i = 0 ; i < 3 ; i + + ) {
float csh [ 9 ] ;
for ( int j = 0 ; j < 9 ; j + + ) {
csh [ j ] = sh [ j ] [ i ] ;
2017-12-14 11:59:46 +00:00
}
2020-05-01 12:34:23 +00:00
rot . rotate_sh ( csh ) ;
for ( int j = 0 ; j < 9 ; j + + ) {
sh [ j ] [ i ] = csh [ j ] ;
2017-12-14 11:59:46 +00:00
}
}
2020-05-01 12:34:23 +00:00
Vector3 inner_pos = ( ( lm_pos - bounds . position ) / bounds . size ) * 2.0 - Vector3 ( 1.0 , 1.0 , 1.0 ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
float blend = MAX ( inner_pos . x , MAX ( inner_pos . y , inner_pos . z ) ) ;
//make blend more rounded
blend = Math : : lerp ( inner_pos . length ( ) , blend , blend ) ;
blend * = blend ;
blend = MAX ( 0.0 , 1.0 - blend ) ;
2017-12-14 11:59:46 +00:00
2020-05-01 12:34:23 +00:00
if ( interior & & ! inside ) {
//do not blend, just replace
for ( int j = 0 ; j < 9 ; j + + ) {
accum_sh [ j ] = sh [ j ] * blend ;
}
accum_blend = blend ;
inside = true ;
} else {
for ( int j = 0 ; j < 9 ; j + + ) {
accum_sh [ j ] + = sh [ j ] * blend ;
}
accum_blend + = blend ;
}
2017-12-14 11:59:46 +00:00
}
2020-05-01 12:34:23 +00:00
if ( accum_blend > 0.0 ) {
for ( int j = 0 ; j < 9 ; j + + ) {
instance_sh [ j ] = accum_sh [ j ] / accum_blend ;
if ( first_set ) {
p_instance - > lightmap_sh . write [ j ] = instance_sh [ j ] ;
}
2017-12-14 11:59:46 +00:00
}
}
}
2020-04-08 01:51:52 +00:00
bool RenderingServerScene : : _light_instance_update_shadow ( Instance * p_instance , const Transform p_cam_transform , const CameraMatrix & p_cam_projection , bool p_cam_orthogonal , bool p_cam_vaspect , RID p_shadow_atlas , Scenario * p_scenario ) {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
2016-10-19 14:14:41 +00:00
2018-07-03 11:34:06 +00:00
Transform light_transform = p_instance - > transform ;
light_transform . orthonormalize ( ) ; //scale does not count on lights
2018-11-14 12:32:39 +00:00
bool animated_material_found = false ;
2020-03-27 18:21:27 +00:00
switch ( RSG : : storage - > light_get_type ( p_instance - > base ) ) {
case RS : : LIGHT_DIRECTIONAL : {
2020-04-08 01:51:52 +00:00
real_t max_distance = p_cam_projection . get_z_far ( ) ;
real_t shadow_max = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SHADOW_MAX_DISTANCE ) ;
2017-09-07 21:00:47 +00:00
if ( shadow_max > 0 & & ! p_cam_orthogonal ) { //its impractical (and leads to unwanted behaviors) to set max distance in orthogonal camera
2017-03-05 15:44:50 +00:00
max_distance = MIN ( shadow_max , max_distance ) ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
max_distance = MAX ( max_distance , p_cam_projection . get_z_near ( ) + 0.001 ) ;
2020-04-08 01:51:52 +00:00
real_t min_distance = MIN ( p_cam_projection . get_z_near ( ) , max_distance ) ;
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
RS : : LightDirectionalShadowDepthRangeMode depth_range_mode = RSG : : storage - > light_directional_get_shadow_depth_range_mode ( p_instance - > base ) ;
2017-09-07 21:00:47 +00:00
2020-04-08 01:51:52 +00:00
real_t pancake_size = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SHADOW_PANCAKE_SIZE ) ;
2020-03-27 18:21:27 +00:00
if ( depth_range_mode = = RS : : LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_OPTIMIZED ) {
2017-09-07 21:00:47 +00:00
//optimize min/max
Vector < Plane > planes = p_cam_projection . get_projection_planes ( p_cam_transform ) ;
2020-03-27 18:21:27 +00:00
int cull_count = p_scenario - > octree . cull_convex ( planes , instance_shadow_cull_result , MAX_INSTANCE_CULL , RS : : INSTANCE_GEOMETRY_MASK ) ;
2017-09-07 19:48:50 +00:00
Plane base ( p_cam_transform . origin , - p_cam_transform . basis . get_axis ( 2 ) ) ;
2017-09-07 21:00:47 +00:00
//check distance max and min
2017-09-07 19:48:50 +00:00
bool found_items = false ;
2020-04-08 01:51:52 +00:00
real_t z_max = - 1e20 ;
real_t z_min = 1e20 ;
2017-09-07 21:00:47 +00:00
2017-09-07 19:48:50 +00:00
for ( int i = 0 ; i < cull_count ; i + + ) {
2017-09-07 21:00:47 +00:00
Instance * instance = instance_shadow_cull_result [ i ] ;
2020-03-27 18:21:27 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows ) {
2017-09-07 21:00:47 +00:00
continue ;
}
2018-11-14 12:32:39 +00:00
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2020-04-08 01:51:52 +00:00
real_t max , min ;
2017-09-07 21:00:47 +00:00
instance - > transformed_aabb . project_range_in_plane ( base , min , max ) ;
2017-09-07 19:48:50 +00:00
if ( max > z_max ) {
z_max = max ;
2017-09-07 21:00:47 +00:00
}
2017-09-07 19:48:50 +00:00
if ( min < z_min ) {
z_min = min ;
2017-09-07 21:00:47 +00:00
}
2017-09-07 19:48:50 +00:00
found_items = true ;
2017-09-07 21:00:47 +00:00
}
if ( found_items ) {
2017-09-07 19:48:50 +00:00
min_distance = MAX ( min_distance , z_min ) ;
max_distance = MIN ( max_distance , z_max ) ;
2017-09-07 21:00:47 +00:00
}
}
2020-04-08 01:51:52 +00:00
real_t range = max_distance - min_distance ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
int splits = 0 ;
2020-03-27 18:21:27 +00:00
switch ( RSG : : storage - > light_directional_get_shadow_mode ( p_instance - > base ) ) {
2020-05-10 11:00:47 +00:00
case RS : : LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL :
splits = 1 ;
break ;
case RS : : LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS :
splits = 2 ;
break ;
case RS : : LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS :
splits = 4 ;
break ;
2016-11-10 02:55:06 +00:00
}
2020-04-08 01:51:52 +00:00
real_t distances [ 5 ] ;
2016-11-10 02:55:06 +00:00
2017-09-07 21:00:47 +00:00
distances [ 0 ] = min_distance ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < splits ; i + + ) {
2020-03-27 18:21:27 +00:00
distances [ i + 1 ] = min_distance + RSG : : storage - > light_get_param ( p_instance - > base , RS : : LightParam ( RS : : LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i ) ) * range ;
2016-11-10 02:55:06 +00:00
} ;
2017-03-05 15:44:50 +00:00
distances [ splits ] = max_distance ;
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t texture_size = RSG : : scene_render - > get_directional_light_shadow_size ( light - > instance ) ;
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
bool overlap = RSG : : storage - > light_directional_get_blend_splits ( p_instance - > base ) ;
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t first_radius = 0.0 ;
real_t min_distance_bias_scale = pancake_size > 0 ? distances [ 1 ] / 10.0 : 0 ;
2017-06-14 19:56:47 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < splits ; i + + ) {
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Culling Directional Light split " + itos ( i ) ) ;
2016-11-10 02:55:06 +00:00
// setup a camera matrix for that range!
CameraMatrix camera_matrix ;
2020-04-08 01:51:52 +00:00
real_t aspect = p_cam_projection . get_aspect ( ) ;
2016-11-10 02:55:06 +00:00
2016-11-19 16:23:37 +00:00
if ( p_cam_orthogonal ) {
2020-01-21 18:39:16 +00:00
Vector2 vp_he = p_cam_projection . get_viewport_half_extents ( ) ;
camera_matrix . set_orthogonal ( vp_he . y * 2.0 , aspect , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , false ) ;
2016-11-19 16:23:37 +00:00
} else {
2020-04-08 01:51:52 +00:00
real_t fov = p_cam_projection . get_fov ( ) ; //this is actually yfov, because set aspect tries to keep it
camera_matrix . set_perspective ( fov , aspect , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , true ) ;
2016-11-10 02:55:06 +00:00
}
//obtain the frustum endpoints
Vector3 endpoints [ 8 ] ; // frustum plane endpoints
2017-03-05 15:44:50 +00:00
bool res = camera_matrix . get_endpoints ( p_cam_transform , endpoints ) ;
2016-11-10 02:55:06 +00:00
ERR_CONTINUE ( ! res ) ;
// obtain the light frustm ranges (given endpoints)
2018-07-03 11:34:06 +00:00
Transform transform = light_transform ; //discard scale and stabilize light
2017-06-14 19:56:47 +00:00
Vector3 x_vec = transform . basis . get_axis ( Vector3 : : AXIS_X ) . normalized ( ) ;
Vector3 y_vec = transform . basis . get_axis ( Vector3 : : AXIS_Y ) . normalized ( ) ;
Vector3 z_vec = transform . basis . get_axis ( Vector3 : : AXIS_Z ) . normalized ( ) ;
2016-11-10 02:55:06 +00:00
//z_vec points agsint the camera, like in default opengl
2020-04-08 01:51:52 +00:00
real_t x_min = 0.f , x_max = 0.f ;
real_t y_min = 0.f , y_max = 0.f ;
real_t z_min = 0.f , z_max = 0.f ;
2016-11-10 02:55:06 +00:00
2018-09-27 10:07:59 +00:00
// FIXME: z_max_cam is defined, computed, but not used below when setting up
// ortho_camera. Commented out for now to fix warnings but should be investigated.
2020-04-08 01:51:52 +00:00
real_t x_min_cam = 0.f , x_max_cam = 0.f ;
real_t y_min_cam = 0.f , y_max_cam = 0.f ;
real_t z_min_cam = 0.f ;
//real_t z_max_cam = 0.f;
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t bias_scale = 1.0 ;
real_t aspect_bias_scale = 1.0 ;
2017-06-14 19:56:47 +00:00
2016-11-10 02:55:06 +00:00
//used for culling
2017-06-14 19:56:47 +00:00
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
2020-04-08 01:51:52 +00:00
real_t d_x = x_vec . dot ( endpoints [ j ] ) ;
real_t d_y = y_vec . dot ( endpoints [ j ] ) ;
real_t d_z = z_vec . dot ( endpoints [ j ] ) ;
2017-03-05 15:44:50 +00:00
2020-05-14 14:41:43 +00:00
if ( j = = 0 | | d_x < x_min ) {
2017-03-05 15:44:50 +00:00
x_min = d_x ;
2020-05-14 14:41:43 +00:00
}
if ( j = = 0 | | d_x > x_max ) {
2017-03-05 15:44:50 +00:00
x_max = d_x ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
2020-05-14 14:41:43 +00:00
if ( j = = 0 | | d_y < y_min ) {
2017-03-05 15:44:50 +00:00
y_min = d_y ;
2020-05-14 14:41:43 +00:00
}
if ( j = = 0 | | d_y > y_max ) {
2017-03-05 15:44:50 +00:00
y_max = d_y ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
2020-05-14 14:41:43 +00:00
if ( j = = 0 | | d_z < z_min ) {
2017-03-05 15:44:50 +00:00
z_min = d_z ;
2020-05-14 14:41:43 +00:00
}
if ( j = = 0 | | d_z > z_max ) {
2017-03-05 15:44:50 +00:00
z_max = d_z ;
2020-05-14 14:41:43 +00:00
}
2016-11-10 02:55:06 +00:00
}
2020-04-09 18:11:15 +00:00
2020-04-08 01:51:52 +00:00
real_t radius = 0 ;
2020-04-09 18:11:15 +00:00
real_t soft_shadow_expand = 0 ;
2020-04-08 01:51:52 +00:00
Vector3 center ;
2016-11-10 02:55:06 +00:00
{
//camera viewport stuff
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
center + = endpoints [ j ] ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
center / = 8.0 ;
2016-11-10 02:55:06 +00:00
//center=x_vec*(x_max-x_min)*0.5 + y_vec*(y_max-y_min)*0.5 + z_vec*(z_max-z_min)*0.5;
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
2020-04-08 01:51:52 +00:00
real_t d = center . distance_to ( endpoints [ j ] ) ;
2020-05-14 14:41:43 +00:00
if ( d > radius ) {
2017-03-05 15:44:50 +00:00
radius = d ;
2020-05-14 14:41:43 +00:00
}
2016-11-10 02:55:06 +00:00
}
2017-09-07 21:00:47 +00:00
radius * = texture_size / ( texture_size - 2.0 ) ; //add a texel by each side
2016-11-10 02:55:06 +00:00
2017-06-14 19:56:47 +00:00
if ( i = = 0 ) {
first_radius = radius ;
} else {
bias_scale = radius / first_radius ;
}
2017-03-05 15:44:50 +00:00
z_min_cam = z_vec . dot ( center ) - radius ;
2016-11-10 02:55:06 +00:00
2020-04-09 18:11:15 +00:00
{
float soft_shadow_angle = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SIZE ) ;
if ( soft_shadow_angle > 0.0 & & pancake_size > 0.0 ) {
float z_range = ( z_vec . dot ( center ) + radius + pancake_size ) - z_min_cam ;
soft_shadow_expand = Math : : tan ( Math : : deg2rad ( soft_shadow_angle ) ) * z_range ;
x_max + = soft_shadow_expand ;
y_max + = soft_shadow_expand ;
x_min - = soft_shadow_expand ;
y_min - = soft_shadow_expand ;
}
}
x_max_cam = x_vec . dot ( center ) + radius + soft_shadow_expand ;
x_min_cam = x_vec . dot ( center ) - radius - soft_shadow_expand ;
y_max_cam = y_vec . dot ( center ) + radius + soft_shadow_expand ;
y_min_cam = y_vec . dot ( center ) - radius - soft_shadow_expand ;
2020-03-27 18:21:27 +00:00
if ( depth_range_mode = = RS : : LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE ) {
2017-09-07 21:00:47 +00:00
//this trick here is what stabilizes the shadow (make potential jaggies to not move)
//at the cost of some wasted resolution. Still the quality increase is very well worth it
2020-04-08 01:51:52 +00:00
real_t unit = radius * 2.0 / texture_size ;
2017-09-07 21:00:47 +00:00
x_max_cam = Math : : stepify ( x_max_cam , unit ) ;
x_min_cam = Math : : stepify ( x_min_cam , unit ) ;
y_max_cam = Math : : stepify ( y_max_cam , unit ) ;
y_min_cam = Math : : stepify ( y_min_cam , unit ) ;
}
2016-11-10 02:55:06 +00:00
}
//now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
Vector < Plane > light_frustum_planes ;
light_frustum_planes . resize ( 6 ) ;
//right/left
2018-07-25 01:11:03 +00:00
light_frustum_planes . write [ 0 ] = Plane ( x_vec , x_max ) ;
light_frustum_planes . write [ 1 ] = Plane ( - x_vec , - x_min ) ;
2016-11-10 02:55:06 +00:00
//top/bottom
2018-07-25 01:11:03 +00:00
light_frustum_planes . write [ 2 ] = Plane ( y_vec , y_max ) ;
light_frustum_planes . write [ 3 ] = Plane ( - y_vec , - y_min ) ;
2016-11-10 02:55:06 +00:00
//near/far
2018-07-25 01:11:03 +00:00
light_frustum_planes . write [ 4 ] = Plane ( z_vec , z_max + 1e6 ) ;
light_frustum_planes . write [ 5 ] = Plane ( - z_vec , - z_min ) ; // z_min is ok, since casters further than far-light plane are not needed
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
int cull_count = p_scenario - > octree . cull_convex ( light_frustum_planes , instance_shadow_cull_result , MAX_INSTANCE_CULL , RS : : INSTANCE_GEOMETRY_MASK ) ;
2016-11-10 02:55:06 +00:00
// a pre pass will need to be needed to determine the actual z-near to be used
2018-07-03 11:34:06 +00:00
Plane near_plane ( light_transform . origin , - light_transform . basis . get_axis ( 2 ) ) ;
2017-07-15 04:23:10 +00:00
2020-04-08 01:51:52 +00:00
real_t cull_max = 0 ;
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < cull_count ; j + + ) {
2020-04-08 01:51:52 +00:00
real_t min , max ;
2016-11-10 02:55:06 +00:00
Instance * instance = instance_shadow_cull_result [ j ] ;
2020-03-27 18:21:27 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows ) {
2016-11-10 02:55:06 +00:00
cull_count - - ;
2017-03-05 15:44:50 +00:00
SWAP ( instance_shadow_cull_result [ j ] , instance_shadow_cull_result [ cull_count ] ) ;
2016-11-10 02:55:06 +00:00
j - - ;
2017-02-16 11:55:11 +00:00
continue ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
instance - > transformed_aabb . project_range_in_plane ( Plane ( z_vec , 0 ) , min , max ) ;
2017-07-15 04:23:10 +00:00
instance - > depth = near_plane . distance_to ( instance - > transform . origin ) ;
instance - > depth_layer = 0 ;
2020-04-08 01:51:52 +00:00
if ( j = = 0 | | max > cull_max ) {
cull_max = max ;
}
}
2020-04-09 18:11:15 +00:00
if ( cull_max > z_max ) {
2020-04-08 01:51:52 +00:00
z_max = cull_max ;
2020-04-09 18:11:15 +00:00
}
2020-04-08 01:51:52 +00:00
if ( pancake_size > 0 ) {
z_max = z_vec . dot ( center ) + radius + pancake_size ;
}
if ( aspect ! = 1.0 ) {
// if the aspect is different, then the radius will become larger.
// if this happens, then bias needs to be adjusted too, as depth will increase
// to do this, compare the depth of one that would have resulted from a square frustum
CameraMatrix camera_matrix_square ;
if ( p_cam_orthogonal ) {
Vector2 vp_he = camera_matrix . get_viewport_half_extents ( ) ;
if ( p_cam_vaspect ) {
camera_matrix_square . set_orthogonal ( vp_he . x * 2.0 , 1.0 , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , true ) ;
} else {
camera_matrix_square . set_orthogonal ( vp_he . y * 2.0 , 1.0 , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , false ) ;
}
} else {
Vector2 vp_he = camera_matrix . get_viewport_half_extents ( ) ;
if ( p_cam_vaspect ) {
camera_matrix_square . set_frustum ( vp_he . x * 2.0 , 1.0 , Vector2 ( ) , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , true ) ;
} else {
camera_matrix_square . set_frustum ( vp_he . y * 2.0 , 1.0 , Vector2 ( ) , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , false ) ;
}
}
Vector3 endpoints_square [ 8 ] ; // frustum plane endpoints
res = camera_matrix_square . get_endpoints ( p_cam_transform , endpoints_square ) ;
ERR_CONTINUE ( ! res ) ;
Vector3 center_square ;
real_t z_max_square = 0 ;
for ( int j = 0 ; j < 8 ; j + + ) {
center_square + = endpoints_square [ j ] ;
real_t d_z = z_vec . dot ( endpoints_square [ j ] ) ;
2020-05-14 14:41:43 +00:00
if ( j = = 0 | | d_z > z_max_square ) {
2020-04-08 01:51:52 +00:00
z_max_square = d_z ;
2020-05-14 14:41:43 +00:00
}
2020-04-08 01:51:52 +00:00
}
if ( cull_max > z_max_square ) {
z_max_square = cull_max ;
}
center_square / = 8.0 ;
real_t radius_square = 0 ;
for ( int j = 0 ; j < 8 ; j + + ) {
real_t d = center_square . distance_to ( endpoints_square [ j ] ) ;
2020-05-14 14:41:43 +00:00
if ( d > radius_square ) {
2020-04-08 01:51:52 +00:00
radius_square = d ;
2020-05-14 14:41:43 +00:00
}
2020-04-08 01:51:52 +00:00
}
radius_square * = texture_size / ( texture_size - 2.0 ) ; //add a texel by each side
if ( pancake_size > 0 ) {
z_max_square = z_vec . dot ( center_square ) + radius_square + pancake_size ;
}
real_t z_min_cam_square = z_vec . dot ( center_square ) - radius_square ;
aspect_bias_scale = ( z_max - z_min_cam ) / ( z_max_square - z_min_cam_square ) ;
// this is not entirely perfect, because the cull-adjusted z-max may be different
// but at least it's warranted that it results in a greater bias, so no acne should be present either way.
// pancaking also helps with this.
2016-11-10 02:55:06 +00:00
}
{
CameraMatrix ortho_camera ;
2017-03-05 15:44:50 +00:00
real_t half_x = ( x_max_cam - x_min_cam ) * 0.5 ;
real_t half_y = ( y_max_cam - y_min_cam ) * 0.5 ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
ortho_camera . set_orthogonal ( - half_x , half_x , - half_y , half_y , 0 , ( z_max - z_min_cam ) ) ;
2016-11-10 02:55:06 +00:00
2020-04-09 18:11:15 +00:00
Vector2 uv_scale ( 1.0 / ( x_max_cam - x_min_cam ) , 1.0 / ( y_max_cam - y_min_cam ) ) ;
2016-11-10 02:55:06 +00:00
Transform ortho_transform ;
2017-06-14 19:56:47 +00:00
ortho_transform . basis = transform . basis ;
2017-03-05 15:44:50 +00:00
ortho_transform . origin = x_vec * ( x_min_cam + half_x ) + y_vec * ( y_min_cam + half_y ) + z_vec * z_max ;
2016-11-10 02:55:06 +00:00
2020-04-09 18:11:15 +00:00
{
Vector3 max_in_view = p_cam_transform . affine_inverse ( ) . xform ( z_vec * cull_max ) ;
Vector3 dir_in_view = p_cam_transform . xform_inv ( z_vec ) . normalized ( ) ;
cull_max = dir_in_view . dot ( max_in_view ) ;
}
RSG : : scene_render - > light_instance_set_shadow_transform ( light - > instance , ortho_camera , ortho_transform , z_max - z_min_cam , distances [ i + 1 ] , i , radius * 2.0 / texture_size , bias_scale * aspect_bias_scale * min_distance_bias_scale , z_max , uv_scale ) ;
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > render_shadow ( light - > instance , p_shadow_atlas , i , ( RasterizerScene : : InstanceBase * * ) instance_shadow_cull_result , cull_count ) ;
2016-11-10 02:55:06 +00:00
}
} break ;
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_OMNI : {
RS : : LightOmniShadowMode shadow_mode = RSG : : storage - > light_omni_get_shadow_mode ( p_instance - > base ) ;
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
if ( shadow_mode = = RS : : LIGHT_OMNI_SHADOW_DUAL_PARABOLOID | | ! RSG : : scene_render - > light_instances_can_render_shadow_cube ( ) ) {
2019-01-22 13:04:25 +00:00
for ( int i = 0 ; i < 2 ; i + + ) {
//using this one ensures that raster deferred will have it
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Culling Shadow Paraboloid " + itos ( i ) ) ;
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t radius = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t z = i = = 0 ? - 1 : 1 ;
2019-01-22 13:04:25 +00:00
Vector < Plane > planes ;
2020-04-29 00:13:36 +00:00
planes . resize ( 6 ) ;
2019-01-22 13:04:25 +00:00
planes . write [ 0 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 0 , z ) , radius ) ) ;
planes . write [ 1 ] = light_transform . xform ( Plane ( Vector3 ( 1 , 0 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 2 ] = light_transform . xform ( Plane ( Vector3 ( - 1 , 0 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 3 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 1 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 4 ] = light_transform . xform ( Plane ( Vector3 ( 0 , - 1 , z ) . normalized ( ) , radius ) ) ;
2020-04-29 00:13:36 +00:00
planes . write [ 5 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 0 , - z ) , 0 ) ) ;
2019-01-22 13:04:25 +00:00
2020-03-27 18:21:27 +00:00
int cull_count = p_scenario - > octree . cull_convex ( planes , instance_shadow_cull_result , MAX_INSTANCE_CULL , RS : : INSTANCE_GEOMETRY_MASK ) ;
2019-01-22 13:04:25 +00:00
Plane near_plane ( light_transform . origin , light_transform . basis . get_axis ( 2 ) * z ) ;
for ( int j = 0 ; j < cull_count ; j + + ) {
Instance * instance = instance_shadow_cull_result [ j ] ;
2020-03-27 18:21:27 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows ) {
2019-01-22 13:04:25 +00:00
cull_count - - ;
SWAP ( instance_shadow_cull_result [ j ] , instance_shadow_cull_result [ cull_count ] ) ;
j - - ;
} else {
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2016-11-10 02:55:06 +00:00
2019-01-22 13:04:25 +00:00
instance - > depth = near_plane . distance_to ( instance - > transform . origin ) ;
instance - > depth_layer = 0 ;
}
}
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
RSG : : scene_render - > light_instance_set_shadow_transform ( light - > instance , CameraMatrix ( ) , light_transform , radius , 0 , i , 0 ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > render_shadow ( light - > instance , p_shadow_atlas , i , ( RasterizerScene : : InstanceBase * * ) instance_shadow_cull_result , cull_count ) ;
2019-01-22 13:04:25 +00:00
}
} else { //shadow cube
2016-11-10 02:55:06 +00:00
2020-04-08 01:51:52 +00:00
real_t radius = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
2019-01-22 13:04:25 +00:00
CameraMatrix cm ;
cm . set_perspective ( 90 , 1 , 0.01 , radius ) ;
2018-11-14 12:32:39 +00:00
2019-01-22 13:04:25 +00:00
for ( int i = 0 ; i < 6 ; i + + ) {
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Culling Shadow Cube side " + itos ( i ) ) ;
2019-01-22 13:04:25 +00:00
//using this one ensures that raster deferred will have it
static const Vector3 view_normals [ 6 ] = {
Vector3 ( + 1 , 0 , 0 ) ,
2019-09-07 01:51:27 +00:00
Vector3 ( - 1 , 0 , 0 ) ,
2019-01-22 13:04:25 +00:00
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , + 1 , 0 ) ,
2019-09-07 01:51:27 +00:00
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , 0 , - 1 )
2019-01-22 13:04:25 +00:00
} ;
static const Vector3 view_up [ 6 ] = {
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , - 1 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 )
} ;
Transform xform = light_transform * Transform ( ) . looking_at ( view_normals [ i ] , view_up [ i ] ) ;
Vector < Plane > planes = cm . get_projection_planes ( xform ) ;
2020-03-27 18:21:27 +00:00
int cull_count = p_scenario - > octree . cull_convex ( planes , instance_shadow_cull_result , MAX_INSTANCE_CULL , RS : : INSTANCE_GEOMETRY_MASK ) ;
2019-01-22 13:04:25 +00:00
Plane near_plane ( xform . origin , - xform . basis . get_axis ( 2 ) ) ;
for ( int j = 0 ; j < cull_count ; j + + ) {
Instance * instance = instance_shadow_cull_result [ j ] ;
2020-03-27 18:21:27 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows ) {
2019-01-22 13:04:25 +00:00
cull_count - - ;
SWAP ( instance_shadow_cull_result [ j ] , instance_shadow_cull_result [ cull_count ] ) ;
j - - ;
} else {
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
2016-11-10 02:55:06 +00:00
}
2019-01-22 13:04:25 +00:00
instance - > depth = near_plane . distance_to ( instance - > transform . origin ) ;
instance - > depth_layer = 0 ;
2016-11-10 02:55:06 +00:00
}
}
2020-04-08 01:51:52 +00:00
RSG : : scene_render - > light_instance_set_shadow_transform ( light - > instance , cm , xform , radius , 0 , i , 0 ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > render_shadow ( light - > instance , p_shadow_atlas , i , ( RasterizerScene : : InstanceBase * * ) instance_shadow_cull_result , cull_count ) ;
2019-01-22 13:04:25 +00:00
}
2016-11-10 02:55:06 +00:00
2019-01-22 13:04:25 +00:00
//restore the regular DP matrix
2020-04-08 01:51:52 +00:00
RSG : : scene_render - > light_instance_set_shadow_transform ( light - > instance , CameraMatrix ( ) , light_transform , radius , 0 , 0 , 0 ) ;
2016-11-10 02:55:06 +00:00
}
} break ;
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_SPOT : {
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Culling Spot Light " ) ;
2020-04-08 01:51:52 +00:00
real_t radius = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
real_t angle = RSG : : storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
2016-11-10 02:55:06 +00:00
CameraMatrix cm ;
2017-03-05 15:44:50 +00:00
cm . set_perspective ( angle * 2.0 , 1.0 , 0.01 , radius ) ;
2016-11-10 02:55:06 +00:00
2018-07-03 11:34:06 +00:00
Vector < Plane > planes = cm . get_projection_planes ( light_transform ) ;
2020-03-27 18:21:27 +00:00
int cull_count = p_scenario - > octree . cull_convex ( planes , instance_shadow_cull_result , MAX_INSTANCE_CULL , RS : : INSTANCE_GEOMETRY_MASK ) ;
2016-11-10 02:55:06 +00:00
2018-07-03 11:34:06 +00:00
Plane near_plane ( light_transform . origin , - light_transform . basis . get_axis ( 2 ) ) ;
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < cull_count ; j + + ) {
2016-11-10 02:55:06 +00:00
Instance * instance = instance_shadow_cull_result [ j ] ;
2020-03-27 18:21:27 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows ) {
2016-11-10 02:55:06 +00:00
cull_count - - ;
2017-03-05 15:44:50 +00:00
SWAP ( instance_shadow_cull_result [ j ] , instance_shadow_cull_result [ cull_count ] ) ;
2016-11-10 02:55:06 +00:00
j - - ;
2017-07-15 04:23:10 +00:00
} else {
2018-11-14 12:32:39 +00:00
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2017-07-15 04:23:10 +00:00
instance - > depth = near_plane . distance_to ( instance - > transform . origin ) ;
instance - > depth_layer = 0 ;
2016-11-10 02:55:06 +00:00
}
}
2020-04-08 01:51:52 +00:00
RSG : : scene_render - > light_instance_set_shadow_transform ( light - > instance , cm , light_transform , radius , 0 , 0 , 0 ) ;
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > render_shadow ( light - > instance , p_shadow_atlas , 0 , ( RasterizerScene : : InstanceBase * * ) instance_shadow_cull_result , cull_count ) ;
2016-11-10 02:55:06 +00:00
} break ;
}
2018-11-14 12:32:39 +00:00
return animated_material_found ;
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : render_camera ( RID p_render_buffers , RID p_camera , RID p_scenario , Size2 p_viewport_size , RID p_shadow_atlas ) {
2018-07-21 20:26:14 +00:00
// render to mono camera
# ifndef _3D_DISABLED
2016-11-10 02:55:06 +00:00
2016-10-19 14:14:41 +00:00
Camera * camera = camera_owner . getornull ( p_camera ) ;
ERR_FAIL_COND ( ! camera ) ;
/* STEP 1 - SETUP CAMERA */
CameraMatrix camera_matrix ;
2017-03-05 15:44:50 +00:00
bool ortho = false ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
switch ( camera - > type ) {
2016-10-19 14:14:41 +00:00
case Camera : : ORTHOGONAL : {
camera_matrix . set_orthogonal (
2017-03-05 15:44:50 +00:00
camera - > size ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > znear ,
camera - > zfar ,
2017-12-06 20:36:34 +00:00
camera - > vaspect ) ;
2017-03-05 15:44:50 +00:00
ortho = true ;
2016-10-19 14:14:41 +00:00
} break ;
case Camera : : PERSPECTIVE : {
camera_matrix . set_perspective (
2017-03-05 15:44:50 +00:00
camera - > fov ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > znear ,
camera - > zfar ,
2017-12-06 20:36:34 +00:00
camera - > vaspect ) ;
2017-03-05 15:44:50 +00:00
ortho = false ;
2016-10-19 14:14:41 +00:00
} break ;
2019-02-19 16:17:02 +00:00
case Camera : : FRUSTUM : {
camera_matrix . set_frustum (
camera - > size ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > offset ,
camera - > znear ,
camera - > zfar ,
camera - > vaspect ) ;
ortho = false ;
} break ;
2016-10-19 14:14:41 +00:00
}
2020-06-25 13:33:28 +00:00
RID environment = _render_get_environment ( p_camera , p_scenario ) ;
_prepare_scene ( camera - > transform , camera_matrix , ortho , camera - > vaspect , p_render_buffers , environment , camera - > visible_layers , p_scenario , p_shadow_atlas , RID ( ) ) ;
_render_scene ( p_render_buffers , camera - > transform , camera_matrix , ortho , environment , camera - > effects , p_scenario , p_shadow_atlas , RID ( ) , - 1 ) ;
2018-07-21 20:26:14 +00:00
# endif
2016-11-19 16:23:37 +00:00
}
2020-04-08 14:47:36 +00:00
void RenderingServerScene : : render_camera ( RID p_render_buffers , Ref < XRInterface > & p_interface , XRInterface : : Eyes p_eye , RID p_camera , RID p_scenario , Size2 p_viewport_size , RID p_shadow_atlas ) {
2017-04-23 12:10:41 +00:00
// render for AR/VR interface
Camera * camera = camera_owner . getornull ( p_camera ) ;
ERR_FAIL_COND ( ! camera ) ;
/* SETUP CAMERA, we are ignoring type and FOV here */
float aspect = p_viewport_size . width / ( float ) p_viewport_size . height ;
CameraMatrix camera_matrix = p_interface - > get_projection_for_eye ( p_eye , aspect , camera - > znear , camera - > zfar ) ;
// We also ignore our camera position, it will have been positioned with a slightly old tracking position.
// Instead we take our origin point and have our ar/vr interface add fresh tracking data! Whoohoo!
2020-04-08 14:47:36 +00:00
Transform world_origin = XRServer : : get_singleton ( ) - > get_world_origin ( ) ;
2017-04-23 12:10:41 +00:00
Transform cam_transform = p_interface - > get_transform_for_eye ( p_eye , world_origin ) ;
2020-06-25 13:33:28 +00:00
RID environment = _render_get_environment ( p_camera , p_scenario ) ;
2018-06-11 11:51:30 +00:00
// For stereo render we only prepare for our left eye and then reuse the outcome for our right eye
2020-04-08 14:47:36 +00:00
if ( p_eye = = XRInterface : : EYE_LEFT ) {
2018-06-11 11:51:30 +00:00
// Center our transform, we assume basis is equal.
Transform mono_transform = cam_transform ;
2020-04-08 14:47:36 +00:00
Transform right_transform = p_interface - > get_transform_for_eye ( XRInterface : : EYE_RIGHT , world_origin ) ;
2018-06-11 11:51:30 +00:00
mono_transform . origin + = right_transform . origin ;
mono_transform . origin * = 0.5 ;
// We need to combine our projection frustums for culling.
// Ideally we should use our clipping planes for this and combine them,
// however our shadow map logic uses our projection matrix.
// Note: as our left and right frustums should be mirrored, we don't need our right projection matrix.
// - get some base values we need
float eye_dist = ( mono_transform . origin - cam_transform . origin ) . length ( ) ;
float z_near = camera_matrix . get_z_near ( ) ; // get our near plane
float z_far = camera_matrix . get_z_far ( ) ; // get our far plane
float width = ( 2.0 * z_near ) / camera_matrix . matrix [ 0 ] [ 0 ] ;
float x_shift = width * camera_matrix . matrix [ 2 ] [ 0 ] ;
float height = ( 2.0 * z_near ) / camera_matrix . matrix [ 1 ] [ 1 ] ;
2018-06-23 12:22:57 +00:00
float y_shift = height * camera_matrix . matrix [ 2 ] [ 1 ] ;
2018-06-11 11:51:30 +00:00
// printf("Eye_dist = %f, Near = %f, Far = %f, Width = %f, Shift = %f\n", eye_dist, z_near, z_far, width, x_shift);
// - calculate our near plane size (horizontal only, right_near is mirrored)
float left_near = - eye_dist - ( ( width - x_shift ) * 0.5 ) ;
// - calculate our far plane size (horizontal only, right_far is mirrored)
float left_far = - eye_dist - ( z_far * ( width - x_shift ) * 0.5 / z_near ) ;
float left_far_right_eye = eye_dist - ( z_far * ( width + x_shift ) * 0.5 / z_near ) ;
if ( left_far > left_far_right_eye ) {
// on displays smaller then double our iod, the right eye far frustrum can overtake the left eyes.
left_far = left_far_right_eye ;
}
// - figure out required z-shift
float slope = ( left_far - left_near ) / ( z_far - z_near ) ;
float z_shift = ( left_near / slope ) - z_near ;
// - figure out new vertical near plane size (this will be slightly oversized thanks to our z-shift)
2018-06-23 12:22:57 +00:00
float top_near = ( height - y_shift ) * 0.5 ;
top_near + = ( top_near / z_near ) * z_shift ;
float bottom_near = - ( height + y_shift ) * 0.5 ;
bottom_near + = ( bottom_near / z_near ) * z_shift ;
2018-06-11 11:51:30 +00:00
// printf("Left_near = %f, Left_far = %f, Top_near = %f, Bottom_near = %f, Z_shift = %f\n", left_near, left_far, top_near, bottom_near, z_shift);
// - generate our frustum
CameraMatrix combined_matrix ;
combined_matrix . set_frustum ( left_near , - left_near , bottom_near , top_near , z_near + z_shift , z_far + z_shift ) ;
// and finally move our camera back
Transform apply_z_shift ;
apply_z_shift . origin = Vector3 ( 0.0 , 0.0 , z_shift ) ; // z negative is forward so this moves it backwards
mono_transform * = apply_z_shift ;
// now prepare our scene with our adjusted transform projection matrix
2020-06-25 13:33:28 +00:00
_prepare_scene ( mono_transform , combined_matrix , false , false , p_render_buffers , environment , camera - > visible_layers , p_scenario , p_shadow_atlas , RID ( ) ) ;
2020-04-08 14:47:36 +00:00
} else if ( p_eye = = XRInterface : : EYE_MONO ) {
2018-06-11 11:51:30 +00:00
// For mono render, prepare as per usual
2020-06-25 13:33:28 +00:00
_prepare_scene ( cam_transform , camera_matrix , false , false , p_render_buffers , environment , camera - > visible_layers , p_scenario , p_shadow_atlas , RID ( ) ) ;
2018-06-11 11:51:30 +00:00
}
// And render our scene...
2020-06-25 13:33:28 +00:00
_render_scene ( p_render_buffers , cam_transform , camera_matrix , false , environment , camera - > effects , p_scenario , p_shadow_atlas , RID ( ) , - 1 ) ;
2017-04-23 12:10:41 +00:00
} ;
2020-06-25 13:33:28 +00:00
void RenderingServerScene : : _prepare_scene ( const Transform p_cam_transform , const CameraMatrix & p_cam_projection , bool p_cam_orthogonal , bool p_cam_vaspect , RID p_render_buffers , RID p_environment , uint32_t p_visible_layers , RID p_scenario , RID p_shadow_atlas , RID p_reflection_probe , bool p_using_shadows ) {
2018-06-11 11:51:30 +00:00
// Note, in stereo rendering:
// - p_cam_transform will be a transform in the middle of our two eyes
// - p_cam_projection is a wider frustrum that encompasses both eyes
2016-11-19 16:23:37 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
render_pass + + ;
2017-03-05 15:44:50 +00:00
uint32_t camera_layer_mask = p_visible_layers ;
2016-11-19 16:23:37 +00:00
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > set_scene_pass ( render_pass ) ;
2016-11-19 16:23:37 +00:00
2020-06-25 13:33:28 +00:00
if ( p_render_buffers . is_valid ( ) ) {
RSG : : scene_render - > sdfgi_update ( p_render_buffers , p_environment , p_cam_transform . origin ) ; //update conditions for SDFGI (whether its used or not)
}
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Frustum Culling " ) ;
2017-01-14 11:26:56 +00:00
//rasterizer->set_camera(camera->transform, camera_matrix,ortho);
2016-10-19 14:14:41 +00:00
2016-11-19 16:23:37 +00:00
Vector < Plane > planes = p_cam_projection . get_projection_planes ( p_cam_transform ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
Plane near_plane ( p_cam_transform . origin , - p_cam_transform . basis . get_axis ( 2 ) . normalized ( ) ) ;
2016-11-19 16:23:37 +00:00
float z_far = p_cam_projection . get_z_far ( ) ;
2016-10-19 14:14:41 +00:00
/* STEP 2 - CULL */
2018-06-11 11:51:30 +00:00
instance_cull_count = scenario - > octree . cull_convex ( planes , instance_cull_result , MAX_INSTANCE_CULL ) ;
2017-03-05 15:44:50 +00:00
light_cull_count = 0 ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
reflection_probe_cull_count = 0 ;
2020-04-14 03:05:21 +00:00
decal_cull_count = 0 ;
2019-10-03 20:39:08 +00:00
gi_probe_cull_count = 0 ;
2020-05-01 12:34:23 +00:00
lightmap_cull_count = 0 ;
2016-11-19 16:23:37 +00:00
2017-08-26 03:40:45 +00:00
//light_samplers_culled=0;
2016-10-19 14:14:41 +00:00
2018-08-24 07:35:07 +00:00
/*
print_line ( " OT: " + rtos ( ( OS : : get_singleton ( ) - > get_ticks_usec ( ) - t ) / 1000.0 ) ) ;
2016-10-19 14:14:41 +00:00
print_line ( " OTO: " + itos ( p_scenario - > octree . get_octant_count ( ) ) ) ;
2018-08-24 07:35:07 +00:00
print_line ( " OTE: " + itos ( p_scenario - > octree . get_elem_count ( ) ) ) ;
2016-10-19 14:14:41 +00:00
print_line ( " OTP: " + itos ( p_scenario - > octree . get_pair_count ( ) ) ) ;
2018-08-24 07:35:07 +00:00
*/
2016-10-19 14:14:41 +00:00
2017-08-26 03:40:45 +00:00
/* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
//removed, will replace with culling
2016-10-19 14:14:41 +00:00
/* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
2020-05-01 12:34:23 +00:00
uint64_t frame_number = RSG : : rasterizer - > get_frame_number ( ) ;
float lightmap_probe_update_speed = RSG : : storage - > lightmap_get_probe_capture_update_speed ( ) * RSG : : rasterizer - > get_frame_delta_time ( ) ;
2016-10-19 14:14:41 +00:00
2018-06-11 11:51:30 +00:00
for ( int i = 0 ; i < instance_cull_count ; i + + ) {
2016-10-19 14:14:41 +00:00
Instance * ins = instance_cull_result [ i ] ;
2017-03-05 15:44:50 +00:00
bool keep = false ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
if ( ( camera_layer_mask & ins - > layer_mask ) = = 0 ) {
2016-10-19 14:14:41 +00:00
//failure
2020-03-27 18:21:27 +00:00
} else if ( ins - > base_type = = RS : : INSTANCE_LIGHT & & ins - > visible ) {
2019-04-08 09:03:37 +00:00
if ( light_cull_count < MAX_LIGHTS_CULLED ) {
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( ins - > base_data ) ;
2016-10-19 14:14:41 +00:00
if ( ! light - > geometries . empty ( ) ) {
//do not add this light if no geometry is affected by it..
2017-03-05 15:44:50 +00:00
light_cull_result [ light_cull_count ] = ins ;
light_instance_cull_result [ light_cull_count ] = light - > instance ;
2020-03-27 18:21:27 +00:00
if ( p_shadow_atlas . is_valid ( ) & & RSG : : storage - > light_has_shadow ( ins - > base ) ) {
RSG : : scene_render - > light_instance_mark_visible ( light - > instance ) ; //mark it visible for shadow allocation later
2016-11-10 02:55:06 +00:00
}
2016-10-19 14:14:41 +00:00
light_cull_count + + ;
}
}
2020-03-27 18:21:27 +00:00
} else if ( ins - > base_type = = RS : : INSTANCE_REFLECTION_PROBE & & ins - > visible ) {
2019-04-08 09:03:37 +00:00
if ( reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED ) {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( ins - > base_data ) ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
if ( p_reflection_probe ! = reflection_probe - > instance ) {
2016-11-19 16:23:37 +00:00
//avoid entering The Matrix
if ( ! reflection_probe - > geometries . empty ( ) ) {
//do not add this light if no geometry is affected by it..
2020-03-27 18:21:27 +00:00
if ( reflection_probe - > reflection_dirty | | RSG : : scene_render - > reflection_probe_instance_needs_redraw ( reflection_probe - > instance ) ) {
2016-11-19 16:23:37 +00:00
if ( ! reflection_probe - > update_list . in_list ( ) ) {
2017-03-05 15:44:50 +00:00
reflection_probe - > render_step = 0 ;
2017-07-15 21:40:47 +00:00
reflection_probe_render_list . add_last ( & reflection_probe - > update_list ) ;
2016-11-19 16:23:37 +00:00
}
2017-03-05 15:44:50 +00:00
reflection_probe - > reflection_dirty = false ;
2016-11-19 16:23:37 +00:00
}
2020-03-27 18:21:27 +00:00
if ( RSG : : scene_render - > reflection_probe_instance_has_reflection ( reflection_probe - > instance ) ) {
2019-09-09 20:50:51 +00:00
reflection_probe_instance_cull_result [ reflection_probe_cull_count ] = reflection_probe - > instance ;
reflection_probe_cull_count + + ;
}
2016-11-19 16:23:37 +00:00
}
}
}
2020-04-14 03:05:21 +00:00
} else if ( ins - > base_type = = RS : : INSTANCE_DECAL & & ins - > visible ) {
if ( decal_cull_count < MAX_DECALS_CULLED ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( ins - > base_data ) ;
if ( ! decal - > geometries . empty ( ) ) {
//do not add this decal if no geometry is affected by it..
decal_instance_cull_result [ decal_cull_count ] = decal - > instance ;
decal_cull_count + + ;
}
}
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
} else if ( ins - > base_type = = RS : : INSTANCE_GI_PROBE & & ins - > visible ) {
2017-03-05 15:44:50 +00:00
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( ins - > base_data ) ;
2016-12-20 03:21:07 +00:00
if ( ! gi_probe - > update_element . in_list ( ) ) {
gi_probe_update_list . add ( & gi_probe - > update_element ) ;
}
2019-10-03 20:39:08 +00:00
if ( gi_probe_cull_count < MAX_GI_PROBES_CULLED ) {
gi_probe_instance_cull_result [ gi_probe_cull_count ] = gi_probe - > probe_instance ;
gi_probe_cull_count + + ;
}
2020-05-01 12:34:23 +00:00
} else if ( ins - > base_type = = RS : : INSTANCE_LIGHTMAP & & ins - > visible ) {
if ( lightmap_cull_count < MAX_LIGHTMAPS_CULLED ) {
lightmap_cull_result [ lightmap_cull_count ] = ins ;
lightmap_cull_count + + ;
}
2019-10-03 20:39:08 +00:00
2020-03-27 18:21:27 +00:00
} else if ( ( ( 1 < < ins - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) & & ins - > visible & & ins - > cast_shadows ! = RS : : SHADOW_CASTING_SETTING_SHADOWS_ONLY ) {
2017-03-05 15:44:50 +00:00
keep = true ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( ins - > base_data ) ;
2016-11-19 16:23:37 +00:00
2018-07-23 15:28:15 +00:00
if ( ins - > redraw_if_visible ) {
2020-03-27 18:21:27 +00:00
RenderingServerRaster : : redraw_request ( ) ;
2018-07-23 15:28:15 +00:00
}
2020-03-27 18:21:27 +00:00
if ( ins - > base_type = = RS : : INSTANCE_PARTICLES ) {
2018-07-17 14:52:13 +00:00
//particles visible? process them
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > particles_is_inactive ( ins - > base ) ) {
2018-11-19 23:34:29 +00:00
//but if nothing is going on, don't do it.
keep = false ;
} else {
2020-03-27 18:21:27 +00:00
RSG : : storage - > particles_request_process ( ins - > base ) ;
2018-11-19 23:34:29 +00:00
//particles visible? request redraw
2020-03-27 18:21:27 +00:00
RenderingServerRaster : : redraw_request ( ) ;
2018-11-19 23:34:29 +00:00
}
2017-04-07 02:36:37 +00:00
}
2016-10-19 14:14:41 +00:00
if ( geom - > lighting_dirty ) {
2017-03-05 15:44:50 +00:00
int l = 0 ;
2016-10-19 14:14:41 +00:00
//only called when lights AABB enter/exit this geometry
ins - > light_instances . resize ( geom - > lighting . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = geom - > lighting . front ( ) ; E ; E = E - > next ( ) ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > get ( ) - > base_data ) ;
2016-10-19 14:14:41 +00:00
2018-07-25 01:11:03 +00:00
ins - > light_instances . write [ l + + ] = light - > instance ;
2016-10-19 14:14:41 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > lighting_dirty = false ;
2016-10-19 14:14:41 +00:00
}
2016-11-19 16:23:37 +00:00
if ( geom - > reflection_dirty ) {
2017-03-05 15:44:50 +00:00
int l = 0 ;
2016-11-19 16:23:37 +00:00
//only called when reflection probe AABB enter/exit this geometry
ins - > reflection_probe_instances . resize ( geom - > reflection_probes . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = geom - > reflection_probes . front ( ) ; E ; E = E - > next ( ) ) {
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( E - > get ( ) - > base_data ) ;
2016-11-19 16:23:37 +00:00
2018-07-25 01:11:03 +00:00
ins - > reflection_probe_instances . write [ l + + ] = reflection_probe - > instance ;
2016-11-19 16:23:37 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > reflection_dirty = false ;
2016-11-19 16:23:37 +00:00
}
2016-12-20 03:21:07 +00:00
if ( geom - > gi_probes_dirty ) {
2017-03-05 15:44:50 +00:00
int l = 0 ;
2016-12-20 03:21:07 +00:00
//only called when reflection probe AABB enter/exit this geometry
ins - > gi_probe_instances . resize ( geom - > gi_probes . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = geom - > gi_probes . front ( ) ; E ; E = E - > next ( ) ) {
InstanceGIProbeData * gi_probe = static_cast < InstanceGIProbeData * > ( E - > get ( ) - > base_data ) ;
2016-12-20 03:21:07 +00:00
2018-07-25 01:11:03 +00:00
ins - > gi_probe_instances . write [ l + + ] = gi_probe - > probe_instance ;
2016-12-20 03:21:07 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > gi_probes_dirty = false ;
2016-12-20 03:21:07 +00:00
}
2020-05-01 12:34:23 +00:00
if ( ins - > last_frame_pass ! = frame_number & & ! ins - > lightmap_target_sh . empty ( ) & & ! ins - > lightmap_sh . empty ( ) ) {
Color * sh = ins - > lightmap_sh . ptrw ( ) ;
const Color * target_sh = ins - > lightmap_target_sh . ptr ( ) ;
for ( uint32_t j = 0 ; j < 9 ; j + + ) {
sh [ j ] = sh [ j ] . lerp ( target_sh [ j ] , MIN ( 1.0 , lightmap_probe_update_speed ) ) ;
}
}
2016-10-27 14:50:26 +00:00
ins - > depth = near_plane . distance_to ( ins - > transform . origin ) ;
2017-09-01 15:56:52 +00:00
ins - > depth_layer = CLAMP ( int ( ins - > depth * 16 / z_far ) , 0 , 15 ) ;
2016-10-19 14:14:41 +00:00
}
if ( ! keep ) {
// remove, no reason to keep
2018-06-11 11:51:30 +00:00
instance_cull_count - - ;
SWAP ( instance_cull_result [ i ] , instance_cull_result [ instance_cull_count ] ) ;
2016-10-19 14:14:41 +00:00
i - - ;
2017-03-05 15:44:50 +00:00
ins - > last_render_pass = 0 ; // make invalid
2016-10-19 14:14:41 +00:00
} else {
2017-03-05 15:44:50 +00:00
ins - > last_render_pass = render_pass ;
2016-10-19 14:14:41 +00:00
}
2020-05-01 12:34:23 +00:00
ins - > last_frame_pass = frame_number ;
2016-10-19 14:14:41 +00:00
}
/* STEP 5 - PROCESS LIGHTS */
2017-03-05 15:44:50 +00:00
RID * directional_light_ptr = & light_instance_cull_result [ light_cull_count ] ;
2018-06-11 11:51:30 +00:00
directional_light_count = 0 ;
2016-10-19 14:14:41 +00:00
// directional lights
{
2017-03-05 15:44:50 +00:00
Instance * * lights_with_shadow = ( Instance * * ) alloca ( sizeof ( Instance * ) * scenario - > directional_lights . size ( ) ) ;
int directional_shadow_count = 0 ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = scenario - > directional_lights . front ( ) ; E ; E = E - > next ( ) ) {
if ( light_cull_count + directional_light_count > = MAX_LIGHTS_CULLED ) {
2016-10-19 14:14:41 +00:00
break ;
}
2020-05-14 14:41:43 +00:00
if ( ! E - > get ( ) - > visible ) {
2016-10-19 14:14:41 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > get ( ) - > base_data ) ;
2016-10-19 14:14:41 +00:00
//check shadow..
2017-08-29 11:31:27 +00:00
if ( light ) {
2020-03-27 18:21:27 +00:00
if ( p_using_shadows & & p_shadow_atlas . is_valid ( ) & & RSG : : storage - > light_has_shadow ( E - > get ( ) - > base ) ) {
2017-08-29 11:31:27 +00:00
lights_with_shadow [ directional_shadow_count + + ] = E - > get ( ) ;
}
2017-08-25 15:14:33 +00:00
//add to list
directional_light_ptr [ directional_light_count + + ] = light - > instance ;
}
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > set_directional_shadow_count ( directional_shadow_count ) ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < directional_shadow_count ; i + + ) {
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " >Rendering Directional Light " + itos ( i ) ) ;
2020-04-08 01:51:52 +00:00
_light_instance_update_shadow ( lights_with_shadow [ i ] , p_cam_transform , p_cam_projection , p_cam_orthogonal , p_cam_vaspect , p_shadow_atlas , scenario ) ;
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " <Rendering Directional Light " + itos ( i ) ) ;
2016-10-19 14:14:41 +00:00
}
}
2019-09-09 20:50:51 +00:00
if ( p_using_shadows ) { //setup shadow maps
2016-11-10 02:55:06 +00:00
//SortArray<Instance*,_InstanceLightsort> sorter;
//sorter.sort(light_cull_result,light_cull_count);
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < light_cull_count ; i + + ) {
2016-10-19 14:14:41 +00:00
Instance * ins = light_cull_result [ i ] ;
2020-05-14 14:41:43 +00:00
if ( ! p_shadow_atlas . is_valid ( ) | | ! RSG : : storage - > light_has_shadow ( ins - > base ) ) {
2016-10-19 14:14:41 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( ins - > base_data ) ;
2016-11-10 02:55:06 +00:00
2017-09-01 20:33:39 +00:00
float coverage = 0.f ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
{ //compute coverage
2016-11-10 02:55:06 +00:00
2016-11-19 16:23:37 +00:00
Transform cam_xf = p_cam_transform ;
float zn = p_cam_projection . get_z_near ( ) ;
2017-03-05 15:44:50 +00:00
Plane p ( cam_xf . origin + cam_xf . basis . get_axis ( 2 ) * - zn , - cam_xf . basis . get_axis ( 2 ) ) ; //camera near plane
2016-11-10 02:55:06 +00:00
2020-01-21 18:39:16 +00:00
// near plane half width and height
Vector2 vp_half_extents = p_cam_projection . get_viewport_half_extents ( ) ;
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
switch ( RSG : : storage - > light_get_type ( ins - > base ) ) {
case RS : : LIGHT_OMNI : {
float radius = RSG : : storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_RANGE ) ;
2016-11-10 02:55:06 +00:00
//get two points parallel to near plane
2017-03-05 15:44:50 +00:00
Vector3 points [ 2 ] = {
2016-11-10 02:55:06 +00:00
ins - > transform . origin ,
2017-03-05 15:44:50 +00:00
ins - > transform . origin + cam_xf . basis . get_axis ( 0 ) * radius
2016-11-10 02:55:06 +00:00
} ;
2016-11-19 16:23:37 +00:00
if ( ! p_cam_orthogonal ) {
2016-11-10 02:55:06 +00:00
//if using perspetive, map them to near plane
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < 2 ; j + + ) {
if ( p . distance_to ( points [ j ] ) < 0 ) {
points [ j ] . z = - zn ; //small hack to keep size constant when hitting the screen
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
p . intersects_segment ( cam_xf . origin , points [ j ] , & points [ j ] ) ; //map to plane
2016-11-10 02:55:06 +00:00
}
}
2017-03-05 15:44:50 +00:00
float screen_diameter = points [ 0 ] . distance_to ( points [ 1 ] ) * 2 ;
2020-01-21 18:39:16 +00:00
coverage = screen_diameter / ( vp_half_extents . x + vp_half_extents . y ) ;
2016-11-10 02:55:06 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_SPOT : {
float radius = RSG : : storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_RANGE ) ;
float angle = RSG : : storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
float w = radius * Math : : sin ( Math : : deg2rad ( angle ) ) ;
float d = radius * Math : : cos ( Math : : deg2rad ( angle ) ) ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
Vector3 base = ins - > transform . origin - ins - > transform . basis . get_axis ( 2 ) . normalized ( ) * d ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
Vector3 points [ 2 ] = {
2016-11-10 02:55:06 +00:00
base ,
2017-03-05 15:44:50 +00:00
base + cam_xf . basis . get_axis ( 0 ) * w
2016-11-10 02:55:06 +00:00
} ;
2016-11-19 16:23:37 +00:00
if ( ! p_cam_orthogonal ) {
2016-11-10 02:55:06 +00:00
//if using perspetive, map them to near plane
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < 2 ; j + + ) {
if ( p . distance_to ( points [ j ] ) < 0 ) {
points [ j ] . z = - zn ; //small hack to keep size constant when hitting the screen
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
p . intersects_segment ( cam_xf . origin , points [ j ] , & points [ j ] ) ; //map to plane
2016-11-10 02:55:06 +00:00
}
}
2017-03-05 15:44:50 +00:00
float screen_diameter = points [ 0 ] . distance_to ( points [ 1 ] ) * 2 ;
2020-01-21 18:39:16 +00:00
coverage = screen_diameter / ( vp_half_extents . x + vp_half_extents . y ) ;
2016-11-10 02:55:06 +00:00
} break ;
default : {
ERR_PRINT ( " Invalid Light Type " ) ;
}
}
}
if ( light - > shadow_dirty ) {
light - > last_version + + ;
2017-03-05 15:44:50 +00:00
light - > shadow_dirty = false ;
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
bool redraw = RSG : : scene_render - > shadow_atlas_update_light ( p_shadow_atlas , light - > instance , coverage , light - > last_version ) ;
2016-11-10 02:55:06 +00:00
if ( redraw ) {
//must redraw!
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " >Rendering Light " + itos ( i ) ) ;
2020-04-08 01:51:52 +00:00
light - > shadow_dirty = _light_instance_update_shadow ( ins , p_cam_transform , p_cam_projection , p_cam_orthogonal , p_cam_vaspect , p_shadow_atlas , scenario ) ;
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " <Rendering Light " + itos ( i ) ) ;
2016-11-10 02:55:06 +00:00
}
2016-10-19 14:14:41 +00:00
}
}
2020-06-25 13:33:28 +00:00
/* UPDATE SDFGI */
if ( p_render_buffers . is_valid ( ) ) {
uint32_t cascade_index [ 8 ] ;
uint32_t cascade_sizes [ 8 ] ;
const RID * cascade_ptrs [ 8 ] ;
uint32_t cascade_count = 0 ;
uint32_t sdfgi_light_cull_count = 0 ;
uint32_t prev_cascade = 0xFFFFFFFF ;
for ( int i = 0 ; i < RSG : : scene_render - > sdfgi_get_pending_region_count ( p_render_buffers ) ; i + + ) {
AABB region = RSG : : scene_render - > sdfgi_get_pending_region_bounds ( p_render_buffers , i ) ;
uint32_t region_cascade = RSG : : scene_render - > sdfgi_get_pending_region_cascade ( p_render_buffers , i ) ;
if ( region_cascade ! = prev_cascade ) {
cascade_sizes [ cascade_count ] = 0 ;
cascade_index [ cascade_count ] = region_cascade ;
cascade_ptrs [ cascade_count ] = & sdfgi_light_cull_result [ sdfgi_light_cull_count ] ;
cascade_count + + ;
sdfgi_light_cull_pass + + ;
prev_cascade = region_cascade ;
}
uint32_t sdfgi_cull_count = scenario - > octree . cull_aabb ( region , instance_shadow_cull_result , MAX_INSTANCE_CULL ) ;
for ( uint32_t j = 0 ; j < sdfgi_cull_count ; j + + ) {
Instance * ins = instance_shadow_cull_result [ j ] ;
bool keep = false ;
if ( ins - > base_type = = RS : : INSTANCE_LIGHT & & ins - > visible ) {
InstanceLightData * instance_light = ( InstanceLightData * ) ins - > base_data ;
if ( instance_light - > bake_mode ! = RS : : LIGHT_BAKE_STATIC | | region_cascade > instance_light - > max_sdfgi_cascade ) {
continue ;
}
if ( sdfgi_light_cull_pass ! = instance_light - > sdfgi_cascade_light_pass & & sdfgi_light_cull_count < MAX_LIGHTS_CULLED ) {
instance_light - > sdfgi_cascade_light_pass = sdfgi_light_cull_pass ;
sdfgi_light_cull_result [ sdfgi_light_cull_count + + ] = instance_light - > instance ;
cascade_sizes [ cascade_count - 1 ] + + ;
}
} else if ( ( 1 < < ins - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
if ( ins - > baked_light ) {
keep = true ;
}
}
if ( ! keep ) {
// remove, no reason to keep
sdfgi_cull_count - - ;
SWAP ( instance_shadow_cull_result [ j ] , instance_shadow_cull_result [ sdfgi_cull_count ] ) ;
j - - ;
}
}
RSG : : scene_render - > render_sdfgi ( p_render_buffers , i , ( RasterizerScene : : InstanceBase * * ) instance_shadow_cull_result , sdfgi_cull_count ) ;
//have to save updated cascades, then update static lights.
}
if ( sdfgi_light_cull_count ) {
RSG : : scene_render - > render_sdfgi_static_lights ( p_render_buffers , cascade_count , cascade_index , cascade_ptrs , cascade_sizes ) ;
}
RSG : : scene_render - > sdfgi_update_probes ( p_render_buffers , p_environment , directional_light_ptr , directional_light_count , scenario - > dynamic_lights . ptr ( ) , scenario - > dynamic_lights . size ( ) ) ;
}
2018-06-11 11:51:30 +00:00
}
2020-06-25 13:33:28 +00:00
RID RenderingServerScene : : _render_get_environment ( RID p_camera , RID p_scenario ) {
Camera * camera = camera_owner . getornull ( p_camera ) ;
if ( camera & & RSG : : scene_render - > is_environment ( camera - > env ) ) {
return camera - > env ;
}
2016-11-10 02:55:06 +00:00
2020-06-25 13:33:28 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
if ( ! scenario ) {
return RID ( ) ;
}
if ( RSG : : scene_render - > is_environment ( scenario - > environment ) ) {
return scenario - > environment ;
}
2016-10-19 14:14:41 +00:00
2020-06-25 13:33:28 +00:00
if ( RSG : : scene_render - > is_environment ( scenario - > fallback_environment ) ) {
return scenario - > fallback_environment ;
2020-05-14 14:41:43 +00:00
}
2016-10-19 14:14:41 +00:00
2020-06-25 13:33:28 +00:00
return RID ( ) ;
}
void RenderingServerScene : : _render_scene ( RID p_render_buffers , const Transform p_cam_transform , const CameraMatrix & p_cam_projection , bool p_cam_orthogonal , RID p_environment , RID p_force_camera_effects , RID p_scenario , RID p_shadow_atlas , RID p_reflection_probe , int p_reflection_probe_pass ) {
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
2020-01-13 18:37:24 +00:00
RID camera_effects ;
if ( p_force_camera_effects . is_valid ( ) ) {
camera_effects = p_force_camera_effects ;
} else {
camera_effects = scenario - > camera_effects ;
}
2018-06-11 11:51:30 +00:00
/* PROCESS GEOMETRY AND DRAW SCENE */
2016-10-19 14:14:41 +00:00
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Render Scene " ) ;
2020-06-25 13:33:28 +00:00
RSG : : scene_render - > render_scene ( p_render_buffers , p_cam_transform , p_cam_projection , p_cam_orthogonal , ( RasterizerScene : : InstanceBase * * ) instance_cull_result , instance_cull_count , light_instance_cull_result , light_cull_count + directional_light_count , reflection_probe_instance_cull_result , reflection_probe_cull_count , gi_probe_instance_cull_result , gi_probe_cull_count , decal_instance_cull_result , decal_cull_count , ( RasterizerScene : : InstanceBase * * ) lightmap_cull_result , lightmap_cull_count , p_environment , camera_effects , p_shadow_atlas , p_reflection_probe . is_valid ( ) ? RID ( ) : scenario - > reflection_atlas , p_reflection_probe , p_reflection_probe_pass ) ;
2016-11-19 16:23:37 +00:00
}
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : render_empty_scene ( RID p_render_buffers , RID p_scenario , RID p_shadow_atlas ) {
2018-07-21 20:26:14 +00:00
# ifndef _3D_DISABLED
2017-06-24 11:58:27 +00:00
Scenario * scenario = scenario_owner . getornull ( p_scenario ) ;
RID environment ;
2020-05-14 14:41:43 +00:00
if ( scenario - > environment . is_valid ( ) ) {
2017-06-24 11:58:27 +00:00
environment = scenario - > environment ;
2020-05-14 14:41:43 +00:00
} else {
2017-06-24 11:58:27 +00:00
environment = scenario - > fallback_environment ;
2020-05-14 14:41:43 +00:00
}
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Render Empty Scene " ) ;
2020-05-01 12:34:23 +00:00
RSG : : scene_render - > render_scene ( p_render_buffers , Transform ( ) , CameraMatrix ( ) , true , nullptr , 0 , nullptr , 0 , nullptr , 0 , nullptr , 0 , nullptr , 0 , nullptr , 0 , environment , RID ( ) , p_shadow_atlas , scenario - > reflection_atlas , RID ( ) , 0 ) ;
2018-07-21 20:26:14 +00:00
# endif
2017-06-24 11:58:27 +00:00
}
2020-03-27 18:21:27 +00:00
bool RenderingServerScene : : _render_reflection_probe_step ( Instance * p_instance , int p_step ) {
2017-03-05 15:44:50 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( p_instance - > base_data ) ;
2016-11-19 16:23:37 +00:00
Scenario * scenario = p_instance - > scenario ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! scenario , true ) ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
RenderingServerRaster : : redraw_request ( ) ; //update, so it updates in editor
2018-09-28 19:40:20 +00:00
2017-03-05 15:44:50 +00:00
if ( p_step = = 0 ) {
2020-03-27 18:21:27 +00:00
if ( ! RSG : : scene_render - > reflection_probe_instance_begin_render ( reflection_probe - > instance , scenario - > reflection_atlas ) ) {
2019-09-09 20:50:51 +00:00
return true ; //all full
}
2016-11-19 16:23:37 +00:00
}
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
if ( p_step > = 0 & & p_step < 6 ) {
static const Vector3 view_normals [ 6 ] = {
2016-11-19 16:23:37 +00:00
Vector3 ( + 1 , 0 , 0 ) ,
2019-09-09 20:50:51 +00:00
Vector3 ( - 1 , 0 , 0 ) ,
2017-03-05 15:44:50 +00:00
Vector3 ( 0 , + 1 , 0 ) ,
2019-09-09 20:50:51 +00:00
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , 0 , - 1 )
} ;
static const Vector3 view_up [ 6 ] = {
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
2017-03-05 15:44:50 +00:00
Vector3 ( 0 , 0 , - 1 ) ,
2019-09-09 20:50:51 +00:00
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 )
2016-11-19 16:23:37 +00:00
} ;
2020-03-27 18:21:27 +00:00
Vector3 extents = RSG : : storage - > reflection_probe_get_extents ( p_instance - > base ) ;
Vector3 origin_offset = RSG : : storage - > reflection_probe_get_origin_offset ( p_instance - > base ) ;
float max_distance = RSG : : storage - > reflection_probe_get_origin_max_distance ( p_instance - > base ) ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
Vector3 edge = view_normals [ p_step ] * extents ;
float distance = ABS ( view_normals [ p_step ] . dot ( edge ) - view_normals [ p_step ] . dot ( origin_offset ) ) ; //distance from origin offset to actual view distance limit
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
max_distance = MAX ( max_distance , distance ) ;
2016-11-19 16:23:37 +00:00
//render cubemap side
CameraMatrix cm ;
2017-03-05 15:44:50 +00:00
cm . set_perspective ( 90 , 1 , 0.01 , max_distance ) ;
2016-11-19 16:23:37 +00:00
Transform local_view ;
2017-03-05 15:44:50 +00:00
local_view . set_look_at ( origin_offset , origin_offset + view_normals [ p_step ] , view_up [ p_step ] ) ;
2016-11-19 16:23:37 +00:00
Transform xform = p_instance - > transform * local_view ;
RID shadow_atlas ;
2020-03-27 18:21:27 +00:00
bool use_shadows = RSG : : storage - > reflection_probe_renders_shadows ( p_instance - > base ) ;
2019-09-09 20:50:51 +00:00
if ( use_shadows ) {
2017-03-05 15:44:50 +00:00
shadow_atlas = scenario - > reflection_probe_shadow_atlas ;
2016-10-19 14:14:41 +00:00
}
2016-11-19 16:23:37 +00:00
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Render Reflection Probe, Step " + itos ( p_step ) ) ;
2020-04-08 01:51:52 +00:00
_prepare_scene ( xform , cm , false , false , RID ( ) , RID ( ) , RSG : : storage - > reflection_probe_get_cull_mask ( p_instance - > base ) , p_instance - > scenario - > self , shadow_atlas , reflection_probe - > instance , use_shadows ) ;
2020-01-13 18:37:24 +00:00
_render_scene ( RID ( ) , xform , cm , false , RID ( ) , RID ( ) , p_instance - > scenario - > self , shadow_atlas , reflection_probe - > instance , p_step ) ;
2016-11-19 16:23:37 +00:00
} else {
2018-01-18 20:37:17 +00:00
//do roughness postprocess step until it believes it's done
2019-09-20 20:58:06 +00:00
RENDER_TIMESTAMP ( " Post-Process Reflection Probe, Step " + itos ( p_step ) ) ;
2020-03-27 18:21:27 +00:00
return RSG : : scene_render - > reflection_probe_instance_postprocess_step ( reflection_probe - > instance ) ;
2016-10-19 14:14:41 +00:00
}
2016-11-19 16:23:37 +00:00
return false ;
}
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : render_probes ( ) {
2016-12-20 03:21:07 +00:00
/* REFLECTION PROBES */
2016-10-19 14:14:41 +00:00
2016-12-20 03:21:07 +00:00
SelfList < InstanceReflectionProbeData > * ref_probe = reflection_probe_render_list . first ( ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
bool busy = false ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
while ( ref_probe ) {
SelfList < InstanceReflectionProbeData > * next = ref_probe - > next ( ) ;
2016-12-20 03:21:07 +00:00
RID base = ref_probe - > self ( ) - > owner - > base ;
2016-10-19 14:14:41 +00:00
2020-03-27 18:21:27 +00:00
switch ( RSG : : storage - > reflection_probe_get_update_mode ( base ) ) {
case RS : : REFLECTION_PROBE_UPDATE_ONCE : {
2020-05-14 14:41:43 +00:00
if ( busy ) { //already rendering something
2016-11-19 16:23:37 +00:00
break ;
2020-05-14 14:41:43 +00:00
}
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
bool done = _render_reflection_probe_step ( ref_probe - > self ( ) - > owner , ref_probe - > self ( ) - > render_step ) ;
2016-11-19 16:23:37 +00:00
if ( done ) {
2016-12-20 03:21:07 +00:00
reflection_probe_render_list . remove ( ref_probe ) ;
2016-11-19 16:23:37 +00:00
} else {
2016-12-20 03:21:07 +00:00
ref_probe - > self ( ) - > render_step + + ;
2016-11-19 16:23:37 +00:00
}
2017-03-05 15:44:50 +00:00
busy = true ; //do not render another one of this kind
2016-11-19 16:23:37 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : REFLECTION_PROBE_UPDATE_ALWAYS : {
2017-03-05 15:44:50 +00:00
int step = 0 ;
bool done = false ;
while ( ! done ) {
done = _render_reflection_probe_step ( ref_probe - > self ( ) - > owner , step ) ;
2016-11-19 16:23:37 +00:00
step + + ;
}
2016-12-20 03:21:07 +00:00
reflection_probe_render_list . remove ( ref_probe ) ;
2016-11-19 16:23:37 +00:00
} break ;
}
2017-03-05 15:44:50 +00:00
ref_probe = next ;
2016-11-19 16:23:37 +00:00
}
2016-12-20 03:21:07 +00:00
/* GI PROBES */
SelfList < InstanceGIProbeData > * gi_probe = gi_probe_update_list . first ( ) ;
2019-10-03 20:39:08 +00:00
if ( gi_probe ) {
RENDER_TIMESTAMP ( " Render GI Probes " ) ;
}
2017-03-05 15:44:50 +00:00
while ( gi_probe ) {
SelfList < InstanceGIProbeData > * next = gi_probe - > next ( ) ;
2016-12-20 03:21:07 +00:00
InstanceGIProbeData * probe = gi_probe - > self ( ) ;
2019-10-03 20:39:08 +00:00
//Instance *instance_probe = probe->owner;
2016-12-20 03:21:07 +00:00
//check if probe must be setup, but don't do if on the lighting thread
2019-10-03 20:39:08 +00:00
bool cache_dirty = false ;
int cache_count = 0 ;
{
int light_cache_size = probe - > light_cache . size ( ) ;
const InstanceGIProbeData : : LightCache * caches = probe - > light_cache . ptr ( ) ;
const RID * instance_caches = probe - > light_instances . ptr ( ) ;
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
int idx = 0 ; //must count visible lights
for ( Set < Instance * > : : Element * E = probe - > lights . front ( ) ; E ; E = E - > next ( ) ) {
Instance * instance = E - > get ( ) ;
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
if ( cache_dirty ) {
//do nothing, since idx must count all visible lights anyway
} else if ( idx > = light_cache_size ) {
cache_dirty = true ;
} else {
const InstanceGIProbeData : : LightCache * cache = & caches [ idx ] ;
if (
instance_caches [ idx ] ! = instance_light - > instance | |
2020-03-27 18:21:27 +00:00
cache - > has_shadow ! = RSG : : storage - > light_has_shadow ( instance - > base ) | |
cache - > type ! = RSG : : storage - > light_get_type ( instance - > base ) | |
2019-10-03 20:39:08 +00:00
cache - > transform ! = instance - > transform | |
2020-03-27 18:21:27 +00:00
cache - > color ! = RSG : : storage - > light_get_color ( instance - > base ) | |
cache - > energy ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) | |
cache - > bake_energy ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) | |
cache - > radius ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) | |
cache - > attenuation ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) | |
cache - > spot_angle ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) | |
cache - > spot_attenuation ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ) {
2019-10-03 20:39:08 +00:00
cache_dirty = true ;
}
}
2017-02-06 08:12:15 +00:00
2019-10-03 20:39:08 +00:00
idx + + ;
}
2017-02-06 08:12:15 +00:00
2019-10-03 20:39:08 +00:00
for ( List < Instance * > : : Element * E = probe - > owner - > scenario - > directional_lights . front ( ) ; E ; E = E - > next ( ) ) {
Instance * instance = E - > get ( ) ;
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
if ( cache_dirty ) {
//do nothing, since idx must count all visible lights anyway
} else if ( idx > = light_cache_size ) {
cache_dirty = true ;
} else {
const InstanceGIProbeData : : LightCache * cache = & caches [ idx ] ;
if (
instance_caches [ idx ] ! = instance_light - > instance | |
2020-03-27 18:21:27 +00:00
cache - > has_shadow ! = RSG : : storage - > light_has_shadow ( instance - > base ) | |
cache - > type ! = RSG : : storage - > light_get_type ( instance - > base ) | |
2019-10-03 20:39:08 +00:00
cache - > transform ! = instance - > transform | |
2020-03-27 18:21:27 +00:00
cache - > color ! = RSG : : storage - > light_get_color ( instance - > base ) | |
cache - > energy ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) | |
cache - > bake_energy ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) | |
cache - > radius ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) | |
cache - > attenuation ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) | |
cache - > spot_angle ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) | |
cache - > spot_attenuation ! = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ) {
2019-10-03 20:39:08 +00:00
cache_dirty = true ;
}
}
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
idx + + ;
}
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
if ( idx ! = light_cache_size ) {
cache_dirty = true ;
}
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
cache_count = idx ;
}
2016-12-20 03:21:07 +00:00
2020-03-27 18:21:27 +00:00
bool update_lights = RSG : : scene_render - > gi_probe_needs_update ( probe - > probe_instance ) ;
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
if ( cache_dirty ) {
probe - > light_cache . resize ( cache_count ) ;
probe - > light_instances . resize ( cache_count ) ;
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
if ( cache_count ) {
InstanceGIProbeData : : LightCache * caches = probe - > light_cache . ptrw ( ) ;
RID * instance_caches = probe - > light_instances . ptrw ( ) ;
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
int idx = 0 ; //must count visible lights
for ( Set < Instance * > : : Element * E = probe - > lights . front ( ) ; E ; E = E - > next ( ) ) {
Instance * instance = E - > get ( ) ;
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
2016-12-20 03:21:07 +00:00
}
2019-10-03 20:39:08 +00:00
InstanceGIProbeData : : LightCache * cache = & caches [ idx ] ;
instance_caches [ idx ] = instance_light - > instance ;
2020-03-27 18:21:27 +00:00
cache - > has_shadow = RSG : : storage - > light_has_shadow ( instance - > base ) ;
cache - > type = RSG : : storage - > light_get_type ( instance - > base ) ;
2019-10-03 20:39:08 +00:00
cache - > transform = instance - > transform ;
2020-03-27 18:21:27 +00:00
cache - > color = RSG : : storage - > light_get_color ( instance - > base ) ;
cache - > energy = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) ;
cache - > bake_energy = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
cache - > radius = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
cache - > attenuation = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) ;
cache - > spot_angle = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
cache - > spot_attenuation = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2019-10-03 20:39:08 +00:00
idx + + ;
}
for ( List < Instance * > : : Element * E = probe - > owner - > scenario - > directional_lights . front ( ) ; E ; E = E - > next ( ) ) {
Instance * instance = E - > get ( ) ;
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
2016-12-20 03:21:07 +00:00
2019-10-03 20:39:08 +00:00
InstanceGIProbeData : : LightCache * cache = & caches [ idx ] ;
instance_caches [ idx ] = instance_light - > instance ;
2020-03-27 18:21:27 +00:00
cache - > has_shadow = RSG : : storage - > light_has_shadow ( instance - > base ) ;
cache - > type = RSG : : storage - > light_get_type ( instance - > base ) ;
2019-10-03 20:39:08 +00:00
cache - > transform = instance - > transform ;
2020-03-27 18:21:27 +00:00
cache - > color = RSG : : storage - > light_get_color ( instance - > base ) ;
cache - > energy = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) ;
cache - > bake_energy = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
cache - > radius = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
cache - > attenuation = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) ;
cache - > spot_angle = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
cache - > spot_attenuation = RSG : : storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2019-10-03 20:39:08 +00:00
idx + + ;
}
2016-12-20 03:21:07 +00:00
}
2019-10-03 20:39:08 +00:00
2019-10-11 02:14:56 +00:00
update_lights = true ;
2019-10-03 20:39:08 +00:00
}
2019-10-11 02:14:56 +00:00
instance_cull_count = 0 ;
for ( List < InstanceGIProbeData : : PairInfo > : : Element * E = probe - > dynamic_geometries . front ( ) ; E ; E = E - > next ( ) ) {
if ( instance_cull_count < MAX_INSTANCE_CULL ) {
Instance * ins = E - > get ( ) . geometry ;
2019-10-31 22:54:21 +00:00
if ( ! ins - > visible ) {
continue ;
}
2019-10-11 02:14:56 +00:00
InstanceGeometryData * geom = ( InstanceGeometryData * ) ins - > base_data ;
if ( geom - > gi_probes_dirty ) {
//giprobes may be dirty, so update
int l = 0 ;
//only called when reflection probe AABB enter/exit this geometry
ins - > gi_probe_instances . resize ( geom - > gi_probes . size ( ) ) ;
for ( List < Instance * > : : Element * F = geom - > gi_probes . front ( ) ; F ; F = F - > next ( ) ) {
InstanceGIProbeData * gi_probe2 = static_cast < InstanceGIProbeData * > ( F - > get ( ) - > base_data ) ;
ins - > gi_probe_instances . write [ l + + ] = gi_probe2 - > probe_instance ;
}
geom - > gi_probes_dirty = false ;
}
instance_cull_result [ instance_cull_count + + ] = E - > get ( ) . geometry ;
}
2016-12-20 03:21:07 +00:00
}
2019-10-03 20:39:08 +00:00
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > gi_probe_update ( probe - > probe_instance , update_lights , probe - > light_instances , instance_cull_count , ( RasterizerScene : : InstanceBase * * ) instance_cull_result ) ;
2019-10-11 02:14:56 +00:00
2019-10-03 20:39:08 +00:00
gi_probe_update_list . remove ( gi_probe ) ;
2016-12-20 03:21:07 +00:00
2017-03-05 15:44:50 +00:00
gi_probe = next ;
2016-12-20 03:21:07 +00:00
}
2016-11-19 16:23:37 +00:00
}
2020-04-17 02:52:00 +00:00
void RenderingServerScene : : _update_instance_shader_parameters_from_material ( Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > & isparams , const Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > & existing_isparams , RID p_material ) {
List < RasterizerStorage : : InstanceShaderParam > plist ;
RSG : : storage - > material_get_instance_shader_parameters ( p_material , & plist ) ;
for ( List < RasterizerStorage : : InstanceShaderParam > : : Element * E = plist . front ( ) ; E ; E = E - > next ( ) ) {
StringName name = E - > get ( ) . info . name ;
if ( isparams . has ( name ) ) {
if ( isparams [ name ] . info . type ! = E - > get ( ) . info . type ) {
WARN_PRINT ( " More than one material in instance export the same instance shader uniform ' " + E - > get ( ) . info . name + " ', but they do it with different data types. Only the first one (in order) will display correctly. " ) ;
}
if ( isparams [ name ] . index ! = E - > get ( ) . index ) {
WARN_PRINT ( " More than one material in instance export the same instance shader uniform ' " + E - > get ( ) . info . name + " ', but they do it with different indices. Only the first one (in order) will display correctly. " ) ;
}
continue ; //first one found always has priority
}
RasterizerScene : : InstanceBase : : InstanceShaderParameter isp ;
isp . index = E - > get ( ) . index ;
isp . info = E - > get ( ) . info ;
isp . default_value = E - > get ( ) . default_value ;
if ( existing_isparams . has ( name ) ) {
isp . value = existing_isparams [ name ] . value ;
} else {
isp . value = E - > get ( ) . default_value ;
}
isparams [ name ] = isp ;
}
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : _update_dirty_instance ( Instance * p_instance ) {
2017-08-18 18:11:16 +00:00
if ( p_instance - > update_aabb ) {
2016-10-19 14:14:41 +00:00
_update_instance_aabb ( p_instance ) ;
2017-08-18 18:11:16 +00:00
}
2016-10-19 14:14:41 +00:00
2019-07-21 14:31:30 +00:00
if ( p_instance - > update_dependencies ) {
p_instance - > instance_increase_version ( ) ;
if ( p_instance - > base . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
RSG : : storage - > base_update_dependency ( p_instance - > base , p_instance ) ;
2019-07-21 14:31:30 +00:00
}
if ( p_instance - > material_override . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
RSG : : storage - > material_update_dependency ( p_instance - > material_override , p_instance ) ;
2019-07-21 14:31:30 +00:00
}
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_MESH ) {
2016-11-10 02:55:06 +00:00
//remove materials no longer used and un-own them
2020-03-27 18:21:27 +00:00
int new_mat_count = RSG : : storage - > mesh_get_surface_count ( p_instance - > base ) ;
2016-11-10 02:55:06 +00:00
p_instance - > materials . resize ( new_mat_count ) ;
2016-11-24 23:46:55 +00:00
2020-03-27 18:21:27 +00:00
int new_blend_shape_count = RSG : : storage - > mesh_get_blend_shape_count ( p_instance - > base ) ;
2017-03-05 15:44:50 +00:00
if ( new_blend_shape_count ! = p_instance - > blend_values . size ( ) ) {
2017-01-12 11:34:00 +00:00
p_instance - > blend_values . resize ( new_blend_shape_count ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < new_blend_shape_count ; i + + ) {
2018-07-25 01:11:03 +00:00
p_instance - > blend_values . write [ i ] = 0 ;
2016-11-24 23:46:55 +00:00
}
}
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
2017-03-05 15:44:50 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
bool can_cast_shadows = true ;
2018-11-14 12:32:39 +00:00
bool is_animated = false ;
2020-04-17 02:52:00 +00:00
Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > isparams ;
2016-11-10 02:55:06 +00:00
2020-03-27 18:21:27 +00:00
if ( p_instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_OFF ) {
2017-03-05 15:44:50 +00:00
can_cast_shadows = false ;
2020-04-17 02:52:00 +00:00
}
if ( p_instance - > material_override . is_valid ( ) ) {
if ( ! RSG : : storage - > material_casts_shadows ( p_instance - > material_override ) ) {
can_cast_shadows = false ;
}
2020-03-27 18:21:27 +00:00
is_animated = RSG : : storage - > material_is_animated ( p_instance - > material_override ) ;
2020-04-17 02:52:00 +00:00
_update_instance_shader_parameters_from_material ( isparams , p_instance - > instance_shader_parameters , p_instance - > material_override ) ;
2016-11-10 02:55:06 +00:00
} else {
2020-03-27 18:21:27 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_MESH ) {
2017-03-05 15:44:50 +00:00
RID mesh = p_instance - > base ;
2016-11-10 02:55:06 +00:00
2016-11-22 04:26:56 +00:00
if ( mesh . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
bool cast_shadows = false ;
2016-11-10 02:55:06 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < p_instance - > materials . size ( ) ; i + + ) {
2020-03-27 18:21:27 +00:00
RID mat = p_instance - > materials [ i ] . is_valid ( ) ? p_instance - > materials [ i ] : RSG : : storage - > mesh_surface_get_material ( mesh , i ) ;
2016-11-10 02:55:06 +00:00
2016-11-22 04:26:56 +00:00
if ( ! mat . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
cast_shadows = true ;
2018-11-14 12:32:39 +00:00
} else {
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_casts_shadows ( mat ) ) {
2018-11-14 12:32:39 +00:00
cast_shadows = true ;
}
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_is_animated ( mat ) ) {
2018-11-14 12:32:39 +00:00
is_animated = true ;
}
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
_update_instance_shader_parameters_from_material ( isparams , p_instance - > instance_shader_parameters , mat ) ;
2020-03-27 18:21:27 +00:00
RSG : : storage - > material_update_dependency ( mat , p_instance ) ;
2016-11-22 04:26:56 +00:00
}
2016-11-10 02:55:06 +00:00
}
2016-11-22 04:26:56 +00:00
if ( ! cast_shadows ) {
2017-03-05 15:44:50 +00:00
can_cast_shadows = false ;
2016-11-10 02:55:06 +00:00
}
}
2020-03-27 18:21:27 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_MULTIMESH ) {
RID mesh = RSG : : storage - > multimesh_get_mesh ( p_instance - > base ) ;
2016-11-22 04:26:56 +00:00
if ( mesh . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
bool cast_shadows = false ;
2016-11-22 04:26:56 +00:00
2020-03-27 18:21:27 +00:00
int sc = RSG : : storage - > mesh_get_surface_count ( mesh ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < sc ; i + + ) {
2020-03-27 18:21:27 +00:00
RID mat = RSG : : storage - > mesh_surface_get_material ( mesh , i ) ;
2016-11-22 04:26:56 +00:00
if ( ! mat . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
cast_shadows = true ;
2016-11-22 04:26:56 +00:00
2018-11-14 12:32:39 +00:00
} else {
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_casts_shadows ( mat ) ) {
2018-11-14 12:32:39 +00:00
cast_shadows = true ;
}
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_is_animated ( mat ) ) {
2018-11-14 12:32:39 +00:00
is_animated = true ;
}
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
_update_instance_shader_parameters_from_material ( isparams , p_instance - > instance_shader_parameters , mat ) ;
2020-03-27 18:21:27 +00:00
RSG : : storage - > material_update_dependency ( mat , p_instance ) ;
2016-11-22 04:26:56 +00:00
}
}
if ( ! cast_shadows ) {
2017-03-05 15:44:50 +00:00
can_cast_shadows = false ;
2016-11-22 04:26:56 +00:00
}
2019-08-18 22:40:52 +00:00
2020-03-27 18:21:27 +00:00
RSG : : storage - > base_update_dependency ( mesh , p_instance ) ;
2016-11-10 02:55:06 +00:00
}
2020-03-27 18:21:27 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_IMMEDIATE ) {
RID mat = RSG : : storage - > immediate_get_material ( p_instance - > base ) ;
2016-11-23 10:04:55 +00:00
2020-04-17 02:52:00 +00:00
if ( ! ( ! mat . is_valid ( ) | | RSG : : storage - > material_casts_shadows ( mat ) ) ) {
can_cast_shadows = false ;
}
2018-11-14 12:32:39 +00:00
2020-03-27 18:21:27 +00:00
if ( mat . is_valid ( ) & & RSG : : storage - > material_is_animated ( mat ) ) {
2018-11-14 12:32:39 +00:00
is_animated = true ;
}
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
if ( mat . is_valid ( ) ) {
_update_instance_shader_parameters_from_material ( isparams , p_instance - > instance_shader_parameters , mat ) ;
}
2019-07-21 14:31:30 +00:00
if ( mat . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
RSG : : storage - > material_update_dependency ( mat , p_instance ) ;
2019-07-21 14:31:30 +00:00
}
2020-03-27 18:21:27 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
2017-06-17 10:32:49 +00:00
bool cast_shadows = false ;
2020-03-27 18:21:27 +00:00
int dp = RSG : : storage - > particles_get_draw_passes ( p_instance - > base ) ;
2017-06-17 10:32:49 +00:00
for ( int i = 0 ; i < dp ; i + + ) {
2020-03-27 18:21:27 +00:00
RID mesh = RSG : : storage - > particles_get_draw_pass_mesh ( p_instance - > base , i ) ;
2020-05-14 14:41:43 +00:00
if ( ! mesh . is_valid ( ) ) {
2017-07-15 04:23:10 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2017-06-17 10:32:49 +00:00
2020-03-27 18:21:27 +00:00
int sc = RSG : : storage - > mesh_get_surface_count ( mesh ) ;
2017-06-17 10:32:49 +00:00
for ( int j = 0 ; j < sc ; j + + ) {
2020-03-27 18:21:27 +00:00
RID mat = RSG : : storage - > mesh_surface_get_material ( mesh , j ) ;
2017-06-17 10:32:49 +00:00
if ( ! mat . is_valid ( ) ) {
cast_shadows = true ;
2018-11-14 12:32:39 +00:00
} else {
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_casts_shadows ( mat ) ) {
2018-11-14 12:32:39 +00:00
cast_shadows = true ;
}
2020-03-27 18:21:27 +00:00
if ( RSG : : storage - > material_is_animated ( mat ) ) {
2018-11-14 12:32:39 +00:00
is_animated = true ;
}
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
_update_instance_shader_parameters_from_material ( isparams , p_instance - > instance_shader_parameters , mat ) ;
2020-03-27 18:21:27 +00:00
RSG : : storage - > material_update_dependency ( mat , p_instance ) ;
2017-06-17 10:32:49 +00:00
}
}
}
if ( ! cast_shadows ) {
can_cast_shadows = false ;
}
2016-11-10 02:55:06 +00:00
}
}
2017-03-05 15:44:50 +00:00
if ( can_cast_shadows ! = geom - > can_cast_shadows ) {
2016-11-10 02:55:06 +00:00
//ability to cast shadows change, let lights now
2017-03-05 15:44:50 +00:00
for ( List < Instance * > : : Element * E = geom - > lighting . front ( ) ; E ; E = E - > next ( ) ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > get ( ) - > base_data ) ;
light - > shadow_dirty = true ;
2016-11-10 02:55:06 +00:00
}
2017-03-05 15:44:50 +00:00
geom - > can_cast_shadows = can_cast_shadows ;
2016-11-10 02:55:06 +00:00
}
2018-11-14 12:32:39 +00:00
geom - > material_is_animated = is_animated ;
2020-04-17 02:52:00 +00:00
p_instance - > instance_shader_parameters = isparams ;
if ( p_instance - > instance_allocated_shader_parameters ! = ( p_instance - > instance_shader_parameters . size ( ) > 0 ) ) {
p_instance - > instance_allocated_shader_parameters = ( p_instance - > instance_shader_parameters . size ( ) > 0 ) ;
if ( p_instance - > instance_allocated_shader_parameters ) {
p_instance - > instance_allocated_shader_parameters_offset = RSG : : storage - > global_variables_instance_allocate ( p_instance - > self ) ;
for ( Map < StringName , RasterizerScene : : InstanceBase : : InstanceShaderParameter > : : Element * E = p_instance - > instance_shader_parameters . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) . value . get_type ( ) ! = Variant : : NIL ) {
RSG : : storage - > global_variables_instance_update ( p_instance - > self , E - > get ( ) . index , E - > get ( ) . value ) ;
}
}
} else {
RSG : : storage - > global_variables_instance_free ( p_instance - > self ) ;
p_instance - > instance_allocated_shader_parameters_offset = - 1 ;
}
}
2016-10-19 14:14:41 +00:00
}
2019-07-21 14:31:30 +00:00
if ( p_instance - > skeleton . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
RSG : : storage - > skeleton_update_dependency ( p_instance - > skeleton , p_instance ) ;
2019-07-21 14:31:30 +00:00
}
p_instance - > clean_up_dependencies ( ) ;
2016-10-19 14:14:41 +00:00
}
2017-12-14 11:59:46 +00:00
_instance_update_list . remove ( & p_instance - > update_item ) ;
2016-10-19 14:14:41 +00:00
_update_instance ( p_instance ) ;
2017-03-05 15:44:50 +00:00
p_instance - > update_aabb = false ;
2019-07-21 14:31:30 +00:00
p_instance - > update_dependencies = false ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
void RenderingServerScene : : update_dirty_instances ( ) {
RSG : : storage - > update_dirty_resources ( ) ;
2017-02-16 11:55:11 +00:00
2017-03-05 15:44:50 +00:00
while ( _instance_update_list . first ( ) ) {
_update_dirty_instance ( _instance_update_list . first ( ) - > self ( ) ) ;
2016-10-19 14:14:41 +00:00
}
}
2020-03-27 18:21:27 +00:00
bool RenderingServerScene : : free ( RID p_rid ) {
2016-10-19 14:14:41 +00:00
if ( camera_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Camera * camera = camera_owner . getornull ( p_rid ) ;
2016-10-19 14:14:41 +00:00
camera_owner . free ( p_rid ) ;
memdelete ( camera ) ;
} else if ( scenario_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Scenario * scenario = scenario_owner . getornull ( p_rid ) ;
2016-10-19 14:14:41 +00:00
2017-03-05 15:44:50 +00:00
while ( scenario - > instances . first ( ) ) {
instance_set_scenario ( scenario - > instances . first ( ) - > self ( ) - > self , RID ( ) ) ;
2016-10-19 14:14:41 +00:00
}
2020-03-27 18:21:27 +00:00
RSG : : scene_render - > free ( scenario - > reflection_probe_shadow_atlas ) ;
RSG : : scene_render - > free ( scenario - > reflection_atlas ) ;
2016-10-19 14:14:41 +00:00
scenario_owner . free ( p_rid ) ;
memdelete ( scenario ) ;
} else if ( instance_owner . owns ( p_rid ) ) {
// delete the instance
update_dirty_instances ( ) ;
2019-06-10 15:38:51 +00:00
Instance * instance = instance_owner . getornull ( p_rid ) ;
2016-10-19 14:14:41 +00:00
2020-05-01 12:34:23 +00:00
instance_geometry_set_lightmap ( p_rid , RID ( ) , Rect2 ( ) , 0 ) ;
2017-03-05 15:44:50 +00:00
instance_set_scenario ( p_rid , RID ( ) ) ;
instance_set_base ( p_rid , RID ( ) ) ;
instance_geometry_set_material_override ( p_rid , RID ( ) ) ;
instance_attach_skeleton ( p_rid , RID ( ) ) ;
2016-10-19 14:14:41 +00:00
2020-04-17 02:52:00 +00:00
if ( instance - > instance_allocated_shader_parameters ) {
//free the used shader parameters
RSG : : storage - > global_variables_instance_free ( instance - > self ) ;
}
2016-11-22 04:26:56 +00:00
update_dirty_instances ( ) ; //in case something changed this
2016-10-19 14:14:41 +00:00
instance_owner . free ( p_rid ) ;
memdelete ( instance ) ;
} else {
return false ;
}
return true ;
}
2020-05-01 12:34:23 +00:00
TypedArray < Image > RenderingServerScene : : bake_render_uv2 ( RID p_base , const Vector < RID > & p_material_overrides , const Size2i & p_image_size ) {
return RSG : : scene_render - > bake_render_uv2 ( p_base , p_material_overrides , p_image_size ) ;
}
2020-04-01 23:20:12 +00:00
RenderingServerScene * RenderingServerScene : : singleton = nullptr ;
2016-12-20 03:21:07 +00:00
2020-03-27 18:21:27 +00:00
RenderingServerScene : : RenderingServerScene ( ) {
2017-03-05 15:44:50 +00:00
render_pass = 1 ;
singleton = this ;
2016-10-19 14:14:41 +00:00
}
2016-12-20 03:21:07 +00:00
2020-03-27 18:21:27 +00:00
RenderingServerScene : : ~ RenderingServerScene ( ) {
2016-12-20 03:21:07 +00:00
}