2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* visual_server_scene.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-07-25 12:33:41 +02:00
|
|
|
#ifndef VISUAL_SERVER_SCENE_H
|
|
|
|
#define VISUAL_SERVER_SCENE_H
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
#include "servers/visual/rasterizer.h"
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
#include "core/math/bvh.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/geometry.h"
|
|
|
|
#include "core/math/octree.h"
|
|
|
|
#include "core/os/semaphore.h"
|
|
|
|
#include "core/os/thread.h"
|
2021-01-31 13:34:42 +01:00
|
|
|
#include "core/safe_refcount.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/self_list.h"
|
2021-02-04 11:43:08 +01:00
|
|
|
#include "portals/portal_renderer.h"
|
2017-04-23 14:10:41 +02:00
|
|
|
#include "servers/arvr/arvr_interface.h"
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2019-11-02 21:00:24 +01:00
|
|
|
class VisualServerLightCuller;
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
class VisualServerScene {
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MAX_INSTANCE_CULL = 65536,
|
|
|
|
MAX_LIGHTS_CULLED = 4096,
|
|
|
|
MAX_REFLECTION_PROBES_CULLED = 4096,
|
|
|
|
MAX_ROOM_CULL = 32,
|
|
|
|
MAX_EXTERIOR_PORTALS = 128,
|
2016-10-19 16:14:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t render_pass;
|
|
|
|
static VisualServerScene *singleton;
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2022-04-04 16:18:44 +02:00
|
|
|
/* EVENT QUEUING */
|
|
|
|
|
|
|
|
void tick();
|
|
|
|
void pre_draw(bool p_will_draw);
|
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
/* CAMERA API */
|
2021-09-15 13:30:45 +02:00
|
|
|
struct Scenario;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
struct Camera : public RID_Data {
|
2016-10-19 16:14:41 +02:00
|
|
|
enum Type {
|
|
|
|
PERSPECTIVE,
|
2019-02-19 17:17:02 +01:00
|
|
|
ORTHOGONAL,
|
|
|
|
FRUSTUM
|
2016-10-19 16:14:41 +02:00
|
|
|
};
|
|
|
|
Type type;
|
|
|
|
float fov;
|
2017-03-05 16:44:50 +01:00
|
|
|
float znear, zfar;
|
2016-10-19 16:14:41 +02:00
|
|
|
float size;
|
2019-02-19 17:17:02 +01:00
|
|
|
Vector2 offset;
|
2016-10-19 16:14:41 +02:00
|
|
|
uint32_t visible_layers;
|
|
|
|
RID env;
|
|
|
|
|
2021-09-15 13:30:45 +02:00
|
|
|
// transform_prev is only used when using fixed timestep interpolation
|
2016-10-19 16:14:41 +02:00
|
|
|
Transform transform;
|
2021-09-15 13:30:45 +02:00
|
|
|
Transform transform_prev;
|
|
|
|
|
|
|
|
bool interpolated : 1;
|
|
|
|
bool on_interpolate_transform_list : 1;
|
|
|
|
|
|
|
|
bool vaspect : 1;
|
|
|
|
TransformInterpolator::Method interpolation_method : 3;
|
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
int32_t previous_room_id_hint;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2022-04-04 16:18:44 +02:00
|
|
|
Transform get_transform_interpolated() const;
|
2021-09-15 13:30:45 +02:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
Camera() {
|
2017-03-05 16:44:50 +01:00
|
|
|
visible_layers = 0xFFFFFFFF;
|
2017-11-25 23:48:30 +01:00
|
|
|
fov = 70;
|
2017-03-05 16:44:50 +01:00
|
|
|
type = PERSPECTIVE;
|
2017-11-25 23:48:30 +01:00
|
|
|
znear = 0.05;
|
2017-03-05 16:44:50 +01:00
|
|
|
zfar = 100;
|
|
|
|
size = 1.0;
|
2019-02-19 17:17:02 +01:00
|
|
|
offset = Vector2();
|
2017-03-05 16:44:50 +01:00
|
|
|
vaspect = false;
|
2021-02-04 11:43:08 +01:00
|
|
|
previous_room_id_hint = -1;
|
2021-09-15 13:30:45 +02:00
|
|
|
interpolated = true;
|
|
|
|
on_interpolate_transform_list = false;
|
|
|
|
interpolation_method = TransformInterpolator::INTERP_LERP;
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable RID_Owner<Camera> camera_owner;
|
|
|
|
|
|
|
|
virtual RID camera_create();
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
|
|
|
|
virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
|
2019-02-19 17:17:02 +01:00
|
|
|
virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void camera_set_transform(RID p_camera, const Transform &p_transform);
|
2021-09-15 13:30:45 +02:00
|
|
|
virtual void camera_set_interpolated(RID p_camera, bool p_interpolated);
|
|
|
|
virtual void camera_reset_physics_interpolation(RID p_camera);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
|
|
|
|
virtual void camera_set_environment(RID p_camera, RID p_env);
|
|
|
|
virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
/* SCENARIO API */
|
|
|
|
|
|
|
|
struct Instance;
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
// common interface for all spatial partitioning schemes
|
|
|
|
// this is a bit excessive boilerplatewise but can be removed if we decide to stick with one method
|
|
|
|
|
|
|
|
// note this is actually the BVH id +1, so that visual server can test against zero
|
|
|
|
// for validity to maintain compatibility with octree (where 0 indicates invalid)
|
|
|
|
typedef uint32_t SpatialPartitionID;
|
|
|
|
|
|
|
|
class SpatialPartitioningScene {
|
|
|
|
public:
|
2023-02-01 13:37:52 +01:00
|
|
|
virtual SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t pairable_mask) = 0;
|
2020-10-21 14:13:20 +02:00
|
|
|
virtual void erase(SpatialPartitionID p_handle) = 0;
|
|
|
|
virtual void move(SpatialPartitionID p_handle, const AABB &p_aabb) = 0;
|
2021-01-29 18:47:40 +01:00
|
|
|
virtual void activate(SpatialPartitionID p_handle, const AABB &p_aabb) {}
|
|
|
|
virtual void deactivate(SpatialPartitionID p_handle) {}
|
2021-02-13 19:53:32 +01:00
|
|
|
virtual void force_collision_check(SpatialPartitionID p_handle) {}
|
2020-10-21 14:13:20 +02:00
|
|
|
virtual void update() {}
|
2021-01-19 12:18:55 +01:00
|
|
|
virtual void update_collisions() {}
|
2021-12-03 12:36:39 +01:00
|
|
|
virtual void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) = 0;
|
2020-10-21 14:13:20 +02:00
|
|
|
virtual int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF) = 0;
|
|
|
|
virtual int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF) = 0;
|
|
|
|
virtual int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF) = 0;
|
|
|
|
|
|
|
|
typedef void *(*PairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int);
|
|
|
|
typedef void (*UnpairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int, void *);
|
|
|
|
|
|
|
|
virtual void set_pair_callback(PairCallback p_callback, void *p_userdata) = 0;
|
|
|
|
virtual void set_unpair_callback(UnpairCallback p_callback, void *p_userdata) = 0;
|
|
|
|
|
|
|
|
// bvh specific
|
|
|
|
virtual void params_set_node_expansion(real_t p_value) {}
|
|
|
|
virtual void params_set_pairing_expansion(real_t p_value) {}
|
|
|
|
|
|
|
|
// octree specific
|
|
|
|
virtual void set_balance(float p_balance) {}
|
|
|
|
|
|
|
|
virtual ~SpatialPartitioningScene() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SpatialPartitioningScene_Octree : public SpatialPartitioningScene {
|
|
|
|
Octree_CL<Instance, true> _octree;
|
|
|
|
|
|
|
|
public:
|
2023-02-01 13:37:52 +01:00
|
|
|
SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t pairable_mask);
|
2020-10-21 14:13:20 +02:00
|
|
|
void erase(SpatialPartitionID p_handle);
|
|
|
|
void move(SpatialPartitionID p_handle, const AABB &p_aabb);
|
2021-12-03 12:36:39 +01:00
|
|
|
void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
|
2020-10-21 14:13:20 +02:00
|
|
|
int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
void set_pair_callback(PairCallback p_callback, void *p_userdata);
|
|
|
|
void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
|
|
|
|
void set_balance(float p_balance);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SpatialPartitioningScene_BVH : public SpatialPartitioningScene {
|
2021-12-03 12:36:39 +01:00
|
|
|
template <class T>
|
|
|
|
class UserPairTestFunction {
|
|
|
|
public:
|
|
|
|
static bool user_pair_check(const T *p_a, const T *p_b) {
|
|
|
|
// return false if no collision, decided by masks etc
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class UserCullTestFunction {
|
|
|
|
// write this logic once for use in all routines
|
|
|
|
// double check this as a possible source of bugs in future.
|
|
|
|
static bool _cull_pairing_mask_test_hit(uint32_t p_maskA, uint32_t p_typeA, uint32_t p_maskB, uint32_t p_typeB) {
|
|
|
|
// double check this as a possible source of bugs in future.
|
|
|
|
bool A_match_B = p_maskA & p_typeB;
|
|
|
|
|
|
|
|
if (!A_match_B) {
|
|
|
|
bool B_match_A = p_maskB & p_typeA;
|
|
|
|
if (!B_match_A) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool user_cull_check(const T *p_a, const T *p_b) {
|
|
|
|
DEV_ASSERT(p_a);
|
|
|
|
DEV_ASSERT(p_b);
|
|
|
|
|
|
|
|
uint32_t a_mask = p_a->bvh_pairable_mask;
|
|
|
|
uint32_t a_type = p_a->bvh_pairable_type;
|
|
|
|
uint32_t b_mask = p_b->bvh_pairable_mask;
|
|
|
|
uint32_t b_type = p_b->bvh_pairable_type;
|
|
|
|
|
|
|
|
if (!_cull_pairing_mask_test_hit(a_mask, a_type, b_mask, b_type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2020-10-21 14:13:20 +02:00
|
|
|
// Note that SpatialPartitionIDs are +1 based when stored in visual server, to enable 0 to indicate invalid ID.
|
2021-12-03 12:36:39 +01:00
|
|
|
BVH_Manager<Instance, 2, true, 256, UserPairTestFunction<Instance>, UserCullTestFunction<Instance>> _bvh;
|
|
|
|
Instance *_dummy_cull_object;
|
2020-10-21 14:13:20 +02:00
|
|
|
|
2023-02-01 13:37:52 +01:00
|
|
|
uint32_t find_tree_id_and_collision_mask(bool p_pairable, uint32_t &r_tree_collision_mask) const {
|
|
|
|
// "pairable" (lights etc) can pair with geometry (non pairable) or other pairables.
|
|
|
|
// Geometry never pairs with other geometry, so we can eliminate geometry - geometry collision checks.
|
|
|
|
|
|
|
|
// Additionally, when lights are made invisible their p_pairable_mask is set to zero to stop their collisions.
|
|
|
|
// We could potentially choose `tree_collision_mask` based on whether p_pairable_mask is zero,
|
|
|
|
// in order to catch invisible lights, but in practice these instances will already have been deactivated within
|
|
|
|
// the BVH so this step is unnecessary. So we can keep the simpler logic of geometry collides with pairable,
|
|
|
|
// pairable collides with everything.
|
|
|
|
r_tree_collision_mask = !p_pairable ? 2 : 3;
|
|
|
|
|
|
|
|
// Returns tree_id.
|
|
|
|
return p_pairable ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
public:
|
2021-05-18 21:36:25 +02:00
|
|
|
SpatialPartitioningScene_BVH();
|
2021-12-03 12:36:39 +01:00
|
|
|
~SpatialPartitioningScene_BVH();
|
2023-02-01 13:37:52 +01:00
|
|
|
SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
|
2020-10-21 14:13:20 +02:00
|
|
|
void erase(SpatialPartitionID p_handle);
|
|
|
|
void move(SpatialPartitionID p_handle, const AABB &p_aabb);
|
2021-01-29 18:47:40 +01:00
|
|
|
void activate(SpatialPartitionID p_handle, const AABB &p_aabb);
|
|
|
|
void deactivate(SpatialPartitionID p_handle);
|
2021-02-13 19:53:32 +01:00
|
|
|
void force_collision_check(SpatialPartitionID p_handle);
|
2020-10-21 14:13:20 +02:00
|
|
|
void update();
|
2021-01-19 12:18:55 +01:00
|
|
|
void update_collisions();
|
2021-12-03 12:36:39 +01:00
|
|
|
void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
|
2020-10-21 14:13:20 +02:00
|
|
|
int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
|
|
|
|
void set_pair_callback(PairCallback p_callback, void *p_userdata);
|
|
|
|
void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
|
|
|
|
|
|
|
|
void params_set_node_expansion(real_t p_value) { _bvh.params_set_node_expansion(p_value); }
|
|
|
|
void params_set_pairing_expansion(real_t p_value) { _bvh.params_set_pairing_expansion(p_value); }
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
struct Scenario : RID_Data {
|
2016-10-19 16:14:41 +02:00
|
|
|
VS::ScenarioDebugMode debug;
|
|
|
|
RID self;
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
SpatialPartitioningScene *sps;
|
2021-02-04 11:43:08 +01:00
|
|
|
PortalRenderer _portal_renderer;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *> directional_lights;
|
2016-10-19 16:14:41 +02:00
|
|
|
RID environment;
|
|
|
|
RID fallback_environment;
|
2016-11-19 17:23:37 +01:00
|
|
|
RID reflection_probe_shadow_atlas;
|
|
|
|
RID reflection_atlas;
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
SelfList<Instance>::List instances;
|
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
Scenario();
|
|
|
|
~Scenario() { memdelete(sps); }
|
2016-10-19 16:14:41 +02:00
|
|
|
};
|
|
|
|
|
2016-10-30 01:48:09 +02:00
|
|
|
mutable RID_Owner<Scenario> scenario_owner;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2020-10-21 14:13:20 +02:00
|
|
|
static void *_instance_pair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int);
|
|
|
|
static void _instance_unpair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int, void *);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
virtual RID scenario_create();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void scenario_set_debug(RID p_scenario, VS::ScenarioDebugMode p_debug_mode);
|
2016-10-19 16:14:41 +02:00
|
|
|
virtual void scenario_set_environment(RID p_scenario, RID p_environment);
|
|
|
|
virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_size, int p_subdiv);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
/* INSTANCING API */
|
|
|
|
|
|
|
|
struct InstanceBaseData {
|
|
|
|
virtual ~InstanceBaseData() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Instance : RasterizerScene::InstanceBase {
|
|
|
|
RID self;
|
|
|
|
//scenario stuff
|
2020-10-21 14:13:20 +02:00
|
|
|
SpatialPartitionID spatial_partition_id;
|
2021-02-04 11:43:08 +01:00
|
|
|
|
|
|
|
// rooms & portals
|
|
|
|
OcclusionHandle occlusion_handle; // handle of instance in occlusion system (or 0)
|
|
|
|
VisualServer::InstancePortalMode portal_mode;
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
Scenario *scenario;
|
|
|
|
SelfList<Instance> scenario_item;
|
|
|
|
|
|
|
|
//aabb stuff
|
|
|
|
bool update_aabb;
|
|
|
|
bool update_materials;
|
2016-11-10 03:55:06 +01:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
SelfList<Instance> update_item;
|
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB aabb;
|
|
|
|
AABB transformed_aabb;
|
2017-11-20 20:54:26 +01:00
|
|
|
AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
|
2022-07-13 20:06:25 +02:00
|
|
|
float sorting_offset;
|
|
|
|
bool use_aabb_center;
|
2016-10-19 16:14:41 +02:00
|
|
|
float extra_margin;
|
2019-05-09 11:21:49 +02:00
|
|
|
uint32_t object_id;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
float lod_begin;
|
|
|
|
float lod_end;
|
|
|
|
float lod_begin_hysteresis;
|
|
|
|
float lod_end_hysteresis;
|
|
|
|
RID lod_instance;
|
|
|
|
|
2021-12-03 12:36:39 +01:00
|
|
|
// These are used for the user cull testing function
|
|
|
|
// in the BVH, this is precached rather than recalculated each time.
|
|
|
|
uint32_t bvh_pairable_mask;
|
|
|
|
uint32_t bvh_pairable_type;
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
uint64_t last_render_pass;
|
|
|
|
uint64_t last_frame_pass;
|
|
|
|
|
|
|
|
uint64_t version; // changes to this, and changes to base increase version
|
|
|
|
|
|
|
|
InstanceBaseData *base_data;
|
|
|
|
|
|
|
|
virtual void base_removed() {
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton->instance_set_base(self, RID());
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:39 +01:00
|
|
|
virtual void base_changed(bool p_aabb, bool p_materials) {
|
|
|
|
singleton->_instance_queue_update(this, p_aabb, p_materials);
|
2016-11-10 03:55:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
Instance() :
|
|
|
|
scenario_item(this),
|
|
|
|
update_item(this) {
|
2020-10-21 14:13:20 +02:00
|
|
|
spatial_partition_id = 0;
|
2021-05-04 16:00:45 +02:00
|
|
|
scenario = nullptr;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
update_aabb = false;
|
|
|
|
update_materials = false;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
extra_margin = 0;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2019-05-09 11:21:49 +02:00
|
|
|
object_id = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
visible = true;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
occlusion_handle = 0;
|
|
|
|
portal_mode = VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_STATIC;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
lod_begin = 0;
|
|
|
|
lod_end = 0;
|
|
|
|
lod_begin_hysteresis = 0;
|
|
|
|
lod_end_hysteresis = 0;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2021-12-03 12:36:39 +01:00
|
|
|
bvh_pairable_mask = 0;
|
|
|
|
bvh_pairable_type = 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
last_render_pass = 0;
|
|
|
|
last_frame_pass = 0;
|
|
|
|
version = 1;
|
2021-05-04 16:00:45 +02:00
|
|
|
base_data = nullptr;
|
2017-11-20 20:54:26 +01:00
|
|
|
|
2021-05-04 16:00:45 +02:00
|
|
|
custom_aabb = nullptr;
|
2022-07-13 20:06:25 +02:00
|
|
|
sorting_offset = 0.0f;
|
2022-10-11 18:53:59 +02:00
|
|
|
use_aabb_center = true;
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Instance() {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (base_data) {
|
2016-10-19 16:14:41 +02:00
|
|
|
memdelete(base_data);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
|
|
|
if (custom_aabb) {
|
2017-11-20 20:54:26 +01:00
|
|
|
memdelete(custom_aabb);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelfList<Instance>::List _instance_update_list;
|
2021-09-15 13:30:45 +02:00
|
|
|
|
2022-04-04 16:18:44 +02:00
|
|
|
// fixed timestep interpolation
|
|
|
|
virtual void set_physics_interpolation_enabled(bool p_enabled);
|
|
|
|
|
|
|
|
struct InterpolationData {
|
|
|
|
void notify_free_camera(RID p_rid, Camera &r_camera);
|
|
|
|
void notify_free_instance(RID p_rid, Instance &r_instance);
|
|
|
|
LocalVector<RID> instance_interpolate_update_list;
|
|
|
|
LocalVector<RID> instance_transform_update_lists[2];
|
|
|
|
LocalVector<RID> *instance_transform_update_list_curr = &instance_transform_update_lists[0];
|
|
|
|
LocalVector<RID> *instance_transform_update_list_prev = &instance_transform_update_lists[1];
|
|
|
|
LocalVector<RID> instance_teleport_list;
|
|
|
|
|
|
|
|
LocalVector<RID> camera_transform_update_lists[2];
|
|
|
|
LocalVector<RID> *camera_transform_update_list_curr = &camera_transform_update_lists[0];
|
|
|
|
LocalVector<RID> *camera_transform_update_list_prev = &camera_transform_update_lists[1];
|
|
|
|
LocalVector<RID> camera_teleport_list;
|
|
|
|
|
|
|
|
bool interpolation_enabled = false;
|
|
|
|
} _interpolation_data;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials = false);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
struct InstanceGeometryData : public InstanceBaseData {
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *> lighting;
|
2016-10-19 16:14:41 +02:00
|
|
|
bool lighting_dirty;
|
2016-11-10 03:55:06 +01:00
|
|
|
bool can_cast_shadows;
|
2018-11-14 13:32:39 +01:00
|
|
|
bool material_is_animated;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *> reflection_probes;
|
2016-11-19 17:23:37 +01:00
|
|
|
bool reflection_dirty;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *> gi_probes;
|
2016-12-20 04:21:07 +01:00
|
|
|
bool gi_probes_dirty;
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
List<Instance *> lightmap_captures;
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
InstanceGeometryData() {
|
2021-12-11 12:27:21 +01:00
|
|
|
lighting_dirty = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
reflection_dirty = true;
|
|
|
|
can_cast_shadows = true;
|
2018-11-14 13:32:39 +01:00
|
|
|
material_is_animated = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
gi_probes_dirty = true;
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-19 17:23:37 +01:00
|
|
|
struct InstanceReflectionProbeData : public InstanceBaseData {
|
|
|
|
Instance *owner;
|
|
|
|
|
|
|
|
struct PairInfo {
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *>::Element *L; //reflection iterator in geometry
|
2016-11-19 17:23:37 +01:00
|
|
|
Instance *geometry;
|
|
|
|
};
|
|
|
|
List<PairInfo> geometries;
|
|
|
|
|
|
|
|
RID instance;
|
|
|
|
bool reflection_dirty;
|
|
|
|
SelfList<InstanceReflectionProbeData> update_list;
|
|
|
|
|
|
|
|
int render_step;
|
2021-02-04 11:43:08 +01:00
|
|
|
int32_t previous_room_id_hint;
|
2016-11-19 17:23:37 +01:00
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
InstanceReflectionProbeData() :
|
|
|
|
update_list(this) {
|
2017-03-05 16:44:50 +01:00
|
|
|
reflection_dirty = true;
|
|
|
|
render_step = -1;
|
2021-02-04 11:43:08 +01:00
|
|
|
previous_room_id_hint = -1;
|
2016-11-19 17:23:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
struct InstanceLightData : public InstanceBaseData {
|
|
|
|
struct PairInfo {
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *>::Element *L; //light iterator in geometry
|
2016-10-19 16:14:41 +02:00
|
|
|
Instance *geometry;
|
|
|
|
};
|
|
|
|
|
|
|
|
RID instance;
|
2016-11-10 03:55:06 +01:00
|
|
|
uint64_t last_version;
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *>::Element *D; // directional light in scenario
|
2016-10-19 16:14:41 +02:00
|
|
|
List<PairInfo> geometries;
|
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
Instance *baked_light;
|
2021-02-04 11:43:08 +01:00
|
|
|
int32_t previous_room_id_hint;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2019-11-02 21:00:24 +01:00
|
|
|
private:
|
|
|
|
// Instead of a single dirty flag, we maintain a count
|
|
|
|
// so that we can detect lights that are being made dirty
|
|
|
|
// each frame, and switch on tighter caster culling.
|
|
|
|
int32_t shadow_dirty_count;
|
|
|
|
|
|
|
|
uint32_t light_update_frame_id;
|
|
|
|
bool light_intersects_multiple_cameras;
|
|
|
|
uint32_t light_intersects_multiple_cameras_timeout_frame_id;
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool is_shadow_dirty() const { return shadow_dirty_count != 0; }
|
|
|
|
void make_shadow_dirty() { shadow_dirty_count = light_intersects_multiple_cameras ? 1 : 2; }
|
|
|
|
void detect_light_intersects_multiple_cameras(uint32_t p_frame_id) {
|
|
|
|
// We need to detect the case where shadow updates are occurring
|
|
|
|
// more than once per frame. In this case, we need to turn off
|
|
|
|
// tighter caster culling, so situation reverts to one full shadow update
|
|
|
|
// per frame (light_intersects_multiple_cameras is set).
|
|
|
|
if (p_frame_id == light_update_frame_id) {
|
|
|
|
light_intersects_multiple_cameras = true;
|
|
|
|
light_intersects_multiple_cameras_timeout_frame_id = p_frame_id + 60;
|
|
|
|
} else {
|
|
|
|
// When shadow_volume_intersects_multiple_cameras is set, we
|
|
|
|
// want to detect the situation this is no longer the case, via a timeout.
|
|
|
|
// The system can go back to tighter caster culling in this situation.
|
|
|
|
// Having a long-ish timeout prevents rapid cycling.
|
|
|
|
if (light_intersects_multiple_cameras && (p_frame_id >= light_intersects_multiple_cameras_timeout_frame_id)) {
|
|
|
|
light_intersects_multiple_cameras = false;
|
|
|
|
light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
light_update_frame_id = p_frame_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void decrement_shadow_dirty() {
|
|
|
|
shadow_dirty_count--;
|
|
|
|
DEV_ASSERT(shadow_dirty_count >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shadow updates can either full (everything in the shadow volume)
|
|
|
|
// or closely culled to the camera frustum.
|
|
|
|
bool is_shadow_update_full() const { return shadow_dirty_count == 0; }
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
InstanceLightData() {
|
2019-11-02 21:00:24 +01:00
|
|
|
shadow_dirty_count = 1;
|
|
|
|
light_update_frame_id = UINT32_MAX;
|
|
|
|
light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
|
|
|
|
light_intersects_multiple_cameras = false;
|
|
|
|
|
2021-05-04 16:00:45 +02:00
|
|
|
D = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
last_version = 0;
|
2021-05-04 16:00:45 +02:00
|
|
|
baked_light = nullptr;
|
2021-02-04 11:43:08 +01:00
|
|
|
previous_room_id_hint = -1;
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
struct InstanceGIProbeData : public InstanceBaseData {
|
|
|
|
Instance *owner;
|
|
|
|
|
|
|
|
struct PairInfo {
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *>::Element *L; //gi probe iterator in geometry
|
2016-12-20 04:21:07 +01:00
|
|
|
Instance *geometry;
|
|
|
|
};
|
|
|
|
|
|
|
|
List<PairInfo> geometries;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Set<Instance *> lights;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
|
|
|
struct LightCache {
|
|
|
|
VS::LightType type;
|
|
|
|
Transform transform;
|
|
|
|
Color color;
|
|
|
|
float energy;
|
|
|
|
float radius;
|
|
|
|
float attenuation;
|
|
|
|
float spot_angle;
|
|
|
|
float spot_attenuation;
|
2017-10-04 10:55:57 +02:00
|
|
|
bool visible;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool operator==(const LightCache &p_cache) {
|
|
|
|
return (type == p_cache.type &&
|
|
|
|
transform == p_cache.transform &&
|
|
|
|
color == p_cache.color &&
|
|
|
|
energy == p_cache.energy &&
|
|
|
|
radius == p_cache.radius &&
|
|
|
|
attenuation == p_cache.attenuation &&
|
|
|
|
spot_angle == p_cache.spot_angle &&
|
2017-10-04 10:55:57 +02:00
|
|
|
spot_attenuation == p_cache.spot_attenuation &&
|
|
|
|
visible == p_cache.visible);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2018-10-06 22:20:41 +02:00
|
|
|
bool operator!=(const LightCache &p_cache) {
|
|
|
|
return !operator==(p_cache);
|
|
|
|
}
|
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
LightCache() {
|
2017-03-05 16:44:50 +01:00
|
|
|
type = VS::LIGHT_DIRECTIONAL;
|
|
|
|
energy = 1.0;
|
|
|
|
radius = 1.0;
|
|
|
|
attenuation = 1.0;
|
|
|
|
spot_angle = 1.0;
|
|
|
|
spot_attenuation = 1.0;
|
2017-10-04 10:55:57 +02:00
|
|
|
visible = true;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LocalData {
|
|
|
|
uint16_t pos[3];
|
|
|
|
uint16_t energy[3]; //using 0..1024 for float range 0..1. integer is needed for deterministic add/remove of lights
|
|
|
|
};
|
|
|
|
|
2016-12-30 12:35:54 +01:00
|
|
|
struct CompBlockS3TC {
|
|
|
|
uint32_t offset; //offset in mipmap
|
|
|
|
uint32_t source_count; //sources
|
|
|
|
uint32_t sources[16]; //id for each source
|
|
|
|
uint8_t alpha[8]; //alpha block is pre-computed
|
|
|
|
};
|
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
struct Dynamic {
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<RID, LightCache> light_cache;
|
|
|
|
Map<RID, LightCache> light_cache_changes;
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> light_data;
|
|
|
|
PoolVector<LocalData> local_data;
|
2021-05-04 14:20:36 +02:00
|
|
|
Vector<Vector<uint32_t>> level_cell_lists;
|
2016-12-20 04:21:07 +01:00
|
|
|
RID probe_data;
|
|
|
|
bool enabled;
|
2016-12-22 14:00:15 +01:00
|
|
|
int bake_dynamic_range;
|
2016-12-30 12:35:54 +01:00
|
|
|
RasterizerStorage::GIProbeCompression compression;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-05-04 14:20:36 +02:00
|
|
|
Vector<PoolVector<uint8_t>> mipmaps_3d;
|
|
|
|
Vector<PoolVector<CompBlockS3TC>> mipmaps_s3tc; //for s3tc
|
2016-12-20 04:21:07 +01:00
|
|
|
|
|
|
|
int updating_stage;
|
2017-02-06 09:12:15 +01:00
|
|
|
float propagate;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
|
|
|
int grid_size[3];
|
|
|
|
|
|
|
|
Transform light_to_cell_xform;
|
|
|
|
|
|
|
|
} dynamic;
|
|
|
|
|
|
|
|
RID probe_instance;
|
|
|
|
|
|
|
|
bool invalid;
|
|
|
|
uint32_t base_version;
|
|
|
|
|
|
|
|
SelfList<InstanceGIProbeData> update_element;
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
InstanceGIProbeData() :
|
|
|
|
update_element(this) {
|
2017-03-05 16:44:50 +01:00
|
|
|
invalid = true;
|
|
|
|
base_version = 0;
|
2017-12-06 22:06:34 +01:00
|
|
|
dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelfList<InstanceGIProbeData>::List gi_probe_update_list;
|
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
struct InstanceLightmapCaptureData : public InstanceBaseData {
|
|
|
|
struct PairInfo {
|
|
|
|
List<Instance *>::Element *L; //iterator in geometry
|
|
|
|
Instance *geometry;
|
|
|
|
};
|
|
|
|
List<PairInfo> geometries;
|
|
|
|
|
|
|
|
Set<Instance *> users;
|
|
|
|
|
|
|
|
InstanceLightmapCaptureData() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-11 13:51:30 +02:00
|
|
|
int instance_cull_count;
|
2016-10-19 16:14:41 +02:00
|
|
|
Instance *instance_cull_result[MAX_INSTANCE_CULL];
|
|
|
|
Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
|
|
|
|
Instance *light_cull_result[MAX_LIGHTS_CULLED];
|
|
|
|
RID light_instance_cull_result[MAX_LIGHTS_CULLED];
|
|
|
|
int light_cull_count;
|
2018-06-11 13:51:30 +02:00
|
|
|
int directional_light_count;
|
2019-11-02 21:00:24 +01:00
|
|
|
VisualServerLightCuller *light_culler;
|
2016-11-19 17:23:37 +01:00
|
|
|
RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
|
|
|
|
int reflection_probe_cull_count;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
RID_Owner<Instance> instance_owner;
|
|
|
|
|
2020-01-21 21:32:27 +01:00
|
|
|
virtual RID instance_create();
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2020-01-21 21:32:27 +01:00
|
|
|
virtual void instance_set_base(RID p_instance, RID p_base);
|
|
|
|
virtual void instance_set_scenario(RID p_instance, RID p_scenario);
|
2016-10-19 16:14:41 +02:00
|
|
|
virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
|
2022-07-13 20:06:25 +02:00
|
|
|
virtual void instance_set_pivot_data(RID p_instance, float p_sorting_offset, bool p_use_aabb_center);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
|
2021-09-15 13:30:45 +02:00
|
|
|
virtual void instance_set_interpolated(RID p_instance, bool p_interpolated);
|
|
|
|
virtual void instance_reset_physics_interpolation(RID p_instance);
|
2019-05-09 11:21:49 +02:00
|
|
|
virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
|
|
|
|
virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
|
|
|
|
virtual void instance_set_visible(RID p_instance, bool p_visible);
|
2020-12-19 18:17:22 +01:00
|
|
|
virtual void instance_set_use_lightmap(RID p_instance, RID p_lightmap_instance, RID p_lightmap, int p_lightmap_slice, const Rect2 &p_lightmap_uv_rect);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2018-02-01 09:57:10 +01:00
|
|
|
virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
|
2017-11-20 20:54:26 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
|
|
|
|
virtual void instance_set_exterior(RID p_instance, bool p_enabled);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
// Portals
|
|
|
|
virtual void instance_set_portal_mode(RID p_instance, VisualServer::InstancePortalMode p_mode);
|
|
|
|
bool _instance_get_transformed_aabb(RID p_instance, AABB &r_aabb);
|
2021-08-05 16:44:43 +02:00
|
|
|
bool _instance_get_transformed_aabb_for_occlusion(VSInstance *p_instance, AABB &r_aabb) const {
|
|
|
|
r_aabb = ((Instance *)p_instance)->transformed_aabb;
|
|
|
|
return ((Instance *)p_instance)->portal_mode != VisualServer::INSTANCE_PORTAL_MODE_GLOBAL;
|
|
|
|
}
|
2021-02-04 11:43:08 +01:00
|
|
|
void *_instance_get_from_rid(RID p_instance);
|
|
|
|
bool _instance_cull_check(VSInstance *p_instance, uint32_t p_cull_mask) const {
|
|
|
|
uint32_t pairable_type = 1 << ((Instance *)p_instance)->base_type;
|
|
|
|
return pairable_type & p_cull_mask;
|
|
|
|
}
|
|
|
|
ObjectID _instance_get_object_ID(VSInstance *p_instance) const {
|
|
|
|
if (p_instance) {
|
|
|
|
return ((Instance *)p_instance)->object_id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _instance_create_occlusion_rep(Instance *p_instance);
|
|
|
|
void _instance_destroy_occlusion_rep(Instance *p_instance);
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct Ghost : RID_Data {
|
2022-02-10 12:00:11 +01:00
|
|
|
// all interactions with actual ghosts are indirect, as the ghost is part of the scenario
|
2021-02-04 11:43:08 +01:00
|
|
|
Scenario *scenario = nullptr;
|
|
|
|
uint32_t object_id = 0;
|
|
|
|
RGhostHandle rghost_handle = 0; // handle in occlusion system (or 0)
|
|
|
|
AABB aabb;
|
|
|
|
virtual ~Ghost() {
|
|
|
|
if (scenario) {
|
|
|
|
if (rghost_handle) {
|
|
|
|
scenario->_portal_renderer.rghost_destroy(rghost_handle);
|
|
|
|
rghost_handle = 0;
|
|
|
|
}
|
|
|
|
scenario = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RID_Owner<Ghost> ghost_owner;
|
|
|
|
|
|
|
|
virtual RID ghost_create();
|
|
|
|
virtual void ghost_set_scenario(RID p_ghost, RID p_scenario, ObjectID p_id, const AABB &p_aabb);
|
|
|
|
virtual void ghost_update(RID p_ghost, const AABB &p_aabb);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _ghost_create_occlusion_rep(Ghost *p_ghost);
|
|
|
|
void _ghost_destroy_occlusion_rep(Ghost *p_ghost);
|
|
|
|
|
|
|
|
public:
|
2022-04-04 16:18:44 +02:00
|
|
|
/* PORTALS API */
|
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
struct Portal : RID_Data {
|
2022-02-10 12:00:11 +01:00
|
|
|
// all interactions with actual portals are indirect, as the portal is part of the scenario
|
2021-02-04 11:43:08 +01:00
|
|
|
uint32_t scenario_portal_id = 0;
|
|
|
|
Scenario *scenario = nullptr;
|
|
|
|
virtual ~Portal() {
|
|
|
|
if (scenario) {
|
|
|
|
scenario->_portal_renderer.portal_destroy(scenario_portal_id);
|
|
|
|
scenario = nullptr;
|
|
|
|
scenario_portal_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RID_Owner<Portal> portal_owner;
|
|
|
|
|
|
|
|
virtual RID portal_create();
|
|
|
|
virtual void portal_set_scenario(RID p_portal, RID p_scenario);
|
2021-07-29 12:57:32 +02:00
|
|
|
virtual void portal_set_geometry(RID p_portal, const Vector<Vector3> &p_points, real_t p_margin);
|
2021-02-04 11:43:08 +01:00
|
|
|
virtual void portal_link(RID p_portal, RID p_room_from, RID p_room_to, bool p_two_way);
|
|
|
|
virtual void portal_set_active(RID p_portal, bool p_active);
|
|
|
|
|
2022-04-04 16:18:44 +02:00
|
|
|
/* ROOMGROUPS API */
|
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
struct RoomGroup : RID_Data {
|
2022-02-10 12:00:11 +01:00
|
|
|
// all interactions with actual roomgroups are indirect, as the roomgroup is part of the scenario
|
2021-02-04 11:43:08 +01:00
|
|
|
uint32_t scenario_roomgroup_id = 0;
|
|
|
|
Scenario *scenario = nullptr;
|
|
|
|
virtual ~RoomGroup() {
|
|
|
|
if (scenario) {
|
|
|
|
scenario->_portal_renderer.roomgroup_destroy(scenario_roomgroup_id);
|
|
|
|
scenario = nullptr;
|
|
|
|
scenario_roomgroup_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RID_Owner<RoomGroup> roomgroup_owner;
|
|
|
|
|
|
|
|
virtual RID roomgroup_create();
|
|
|
|
virtual void roomgroup_prepare(RID p_roomgroup, ObjectID p_roomgroup_object_id);
|
|
|
|
virtual void roomgroup_set_scenario(RID p_roomgroup, RID p_scenario);
|
|
|
|
virtual void roomgroup_add_room(RID p_roomgroup, RID p_room);
|
|
|
|
|
2022-04-04 16:18:44 +02:00
|
|
|
/* OCCLUDERS API */
|
|
|
|
|
2022-02-11 15:36:02 +01:00
|
|
|
struct OccluderInstance : RID_Data {
|
2021-08-05 16:44:43 +02:00
|
|
|
uint32_t scenario_occluder_id = 0;
|
|
|
|
Scenario *scenario = nullptr;
|
2022-02-11 15:36:02 +01:00
|
|
|
virtual ~OccluderInstance() {
|
2021-08-05 16:44:43 +02:00
|
|
|
if (scenario) {
|
2022-02-11 15:36:02 +01:00
|
|
|
scenario->_portal_renderer.occluder_instance_destroy(scenario_occluder_id);
|
2021-08-05 16:44:43 +02:00
|
|
|
scenario = nullptr;
|
|
|
|
scenario_occluder_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-02-11 15:36:02 +01:00
|
|
|
RID_Owner<OccluderInstance> occluder_instance_owner;
|
|
|
|
|
|
|
|
struct OccluderResource : RID_Data {
|
|
|
|
uint32_t occluder_resource_id = 0;
|
|
|
|
void destroy(PortalResources &r_portal_resources) {
|
|
|
|
r_portal_resources.occluder_resource_destroy(occluder_resource_id);
|
|
|
|
occluder_resource_id = 0;
|
|
|
|
}
|
|
|
|
virtual ~OccluderResource() {
|
|
|
|
DEV_ASSERT(occluder_resource_id == 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RID_Owner<OccluderResource> occluder_resource_owner;
|
|
|
|
|
|
|
|
virtual RID occluder_instance_create();
|
|
|
|
virtual void occluder_instance_set_scenario(RID p_occluder_instance, RID p_scenario);
|
|
|
|
virtual void occluder_instance_link_resource(RID p_occluder_instance, RID p_occluder_resource);
|
|
|
|
virtual void occluder_instance_set_transform(RID p_occluder_instance, const Transform &p_xform);
|
|
|
|
virtual void occluder_instance_set_active(RID p_occluder_instance, bool p_active);
|
|
|
|
|
|
|
|
virtual RID occluder_resource_create();
|
|
|
|
virtual void occluder_resource_prepare(RID p_occluder_resource, VisualServer::OccluderType p_type);
|
|
|
|
virtual void occluder_resource_spheres_update(RID p_occluder_resource, const Vector<Plane> &p_spheres);
|
|
|
|
virtual void occluder_resource_mesh_update(RID p_occluder_resource, const Geometry::OccluderMeshData &p_mesh_data);
|
2021-08-05 16:44:43 +02:00
|
|
|
virtual void set_use_occlusion_culling(bool p_enable);
|
|
|
|
|
2021-08-17 14:40:39 +02:00
|
|
|
// editor only .. slow
|
|
|
|
virtual Geometry::MeshData occlusion_debug_get_current_polys(RID p_scenario) const;
|
2022-04-04 16:18:44 +02:00
|
|
|
const PortalResources &get_portal_resources() const {
|
|
|
|
return _portal_resources;
|
|
|
|
}
|
|
|
|
PortalResources &get_portal_resources() {
|
|
|
|
return _portal_resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ROOMS API */
|
2021-08-17 14:40:39 +02:00
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
struct Room : RID_Data {
|
2022-02-10 12:00:11 +01:00
|
|
|
// all interactions with actual rooms are indirect, as the room is part of the scenario
|
2021-02-04 11:43:08 +01:00
|
|
|
uint32_t scenario_room_id = 0;
|
|
|
|
Scenario *scenario = nullptr;
|
|
|
|
virtual ~Room() {
|
|
|
|
if (scenario) {
|
|
|
|
scenario->_portal_renderer.room_destroy(scenario_room_id);
|
|
|
|
scenario = nullptr;
|
|
|
|
scenario_room_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RID_Owner<Room> room_owner;
|
|
|
|
|
|
|
|
virtual RID room_create();
|
|
|
|
virtual void room_set_scenario(RID p_room, RID p_scenario);
|
|
|
|
virtual void room_add_instance(RID p_room, RID p_instance, const AABB &p_aabb, const Vector<Vector3> &p_object_pts);
|
|
|
|
virtual void room_add_ghost(RID p_room, ObjectID p_object_id, const AABB &p_aabb);
|
|
|
|
virtual void room_set_bound(RID p_room, ObjectID p_room_object_id, const Vector<Plane> &p_convex, const AABB &p_aabb, const Vector<Vector3> &p_verts);
|
|
|
|
virtual void room_prepare(RID p_room, int32_t p_priority);
|
|
|
|
virtual void rooms_and_portals_clear(RID p_scenario);
|
2021-09-24 15:00:24 +02:00
|
|
|
virtual void rooms_unload(RID p_scenario, String p_reason);
|
2021-08-08 20:31:02 +02:00
|
|
|
virtual void rooms_finalize(RID p_scenario, bool p_generate_pvs, bool p_cull_using_pvs, bool p_use_secondary_pvs, bool p_use_signals, String p_pvs_filename, bool p_use_simple_pvs, bool p_log_pvs_generation);
|
2021-02-04 11:43:08 +01:00
|
|
|
virtual void rooms_override_camera(RID p_scenario, bool p_override, const Vector3 &p_point, const Vector<Plane> *p_convex);
|
|
|
|
virtual void rooms_set_active(RID p_scenario, bool p_active);
|
2021-11-12 16:32:23 +01:00
|
|
|
virtual void rooms_set_params(RID p_scenario, int p_portal_depth_limit, real_t p_roaming_expansion_margin);
|
2021-02-04 11:43:08 +01:00
|
|
|
virtual void rooms_set_debug_feature(RID p_scenario, VisualServer::RoomsDebugFeature p_feature, bool p_active);
|
|
|
|
virtual void rooms_update_gameplay_monitor(RID p_scenario, const Vector<Vector3> &p_camera_positions);
|
|
|
|
|
2021-08-01 09:56:26 +02:00
|
|
|
// don't use this in a game
|
|
|
|
virtual bool rooms_is_loaded(RID p_scenario) const;
|
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
virtual void callbacks_register(VisualServerCallbacks *p_callbacks);
|
2022-04-04 16:18:44 +02:00
|
|
|
VisualServerCallbacks *get_callbacks() const {
|
|
|
|
return _visual_server_callbacks;
|
|
|
|
}
|
2021-02-04 11:43:08 +01:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
// don't use these in a game!
|
2017-11-17 03:09:00 +01:00
|
|
|
virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
|
|
|
|
virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
// internal (uses portals when available)
|
2021-08-17 14:40:39 +02:00
|
|
|
int _cull_convex_from_point(Scenario *p_scenario, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, int32_t &r_previous_room_id_hint, uint32_t p_mask = 0xFFFFFFFF);
|
2021-02-04 11:43:08 +01:00
|
|
|
void _rooms_instance_update(Instance *p_instance, const AABB &p_aabb);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled);
|
2016-10-19 16:14:41 +02:00
|
|
|
virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting);
|
|
|
|
virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
|
2021-07-24 22:45:51 +02:00
|
|
|
virtual void instance_geometry_set_material_overlay(RID p_instance, RID p_material);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
|
|
|
|
virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void _update_instance(Instance *p_instance);
|
|
|
|
_FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
|
|
|
|
_FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
|
2017-12-14 12:59:46 +01:00
|
|
|
_FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2022-06-09 09:02:43 +02:00
|
|
|
_FORCE_INLINE_ bool _light_instance_update_shadow(Instance *p_instance, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_shadow_atlas, Scenario *p_scenario, uint32_t p_visible_layers = 0xFFFFFF);
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
void _prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_force_environment, uint32_t p_visible_layers, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int32_t &r_previous_room_id_hint);
|
2021-04-19 05:32:23 +02:00
|
|
|
void _render_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, const int p_eye, bool p_cam_orthogonal, RID p_force_environment, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
|
2017-06-24 13:58:27 +02:00
|
|
|
void render_empty_scene(RID p_scenario, RID p_shadow_atlas);
|
2016-11-19 17:23:37 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
|
2017-04-23 14:10:41 +02:00
|
|
|
void render_camera(Ref<ARVRInterface> &p_interface, ARVRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
|
2016-10-19 16:14:41 +02:00
|
|
|
void update_dirty_instances();
|
2016-11-19 17:23:37 +01:00
|
|
|
|
2021-09-15 13:30:45 +02:00
|
|
|
// interpolation
|
2022-04-04 16:18:44 +02:00
|
|
|
void update_interpolation_tick(bool p_process = true);
|
|
|
|
void update_interpolation_frame(bool p_process = true);
|
2021-09-15 13:30:45 +02:00
|
|
|
|
2016-12-20 04:21:07 +01:00
|
|
|
//probes
|
|
|
|
struct GIProbeDataHeader {
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t cell_subdiv;
|
|
|
|
uint32_t width;
|
|
|
|
uint32_t height;
|
|
|
|
uint32_t depth;
|
|
|
|
uint32_t cell_count;
|
|
|
|
uint32_t leaf_cell_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GIProbeDataCell {
|
|
|
|
uint32_t children[8];
|
|
|
|
uint32_t albedo;
|
|
|
|
uint32_t emission;
|
2016-12-22 14:00:15 +01:00
|
|
|
uint32_t normal;
|
2016-12-30 12:35:54 +01:00
|
|
|
uint32_t level_alpha;
|
2016-12-20 04:21:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
GI_UPDATE_STAGE_CHECK,
|
|
|
|
GI_UPDATE_STAGE_LIGHTING,
|
|
|
|
GI_UPDATE_STAGE_UPLOADING,
|
|
|
|
};
|
|
|
|
|
|
|
|
void _gi_probe_bake_thread();
|
2017-03-05 16:44:50 +01:00
|
|
|
static void _gi_probe_bake_threads(void *);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-01-31 13:34:42 +01:00
|
|
|
bool probe_bake_thread_exit;
|
2021-01-27 20:10:10 +01:00
|
|
|
Thread probe_bake_thread;
|
2021-01-27 13:41:10 +01:00
|
|
|
Semaphore probe_bake_sem;
|
2021-01-27 10:43:02 +01:00
|
|
|
Mutex probe_bake_mutex;
|
2017-03-05 16:44:50 +01:00
|
|
|
List<Instance *> probe_bake_list;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool _render_reflection_probe_step(Instance *p_instance, int p_step);
|
|
|
|
void _gi_probe_fill_local_data(int p_idx, int p_level, int p_x, int p_y, int p_z, const GIProbeDataCell *p_cell, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, Vector<uint32_t> *prev_cell);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ uint32_t _gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv);
|
|
|
|
void _bake_gi_downscale_light(int p_idx, int p_level, const GIProbeDataCell *p_cells, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, float p_propagate);
|
|
|
|
void _bake_gi_probe_light(const GIProbeDataHeader *header, const GIProbeDataCell *cells, InstanceGIProbeData::LocalData *local_data, const uint32_t *leaves, int p_leaf_count, const InstanceGIProbeData::LightCache &light_cache, int p_sign);
|
2017-08-11 21:10:05 +02:00
|
|
|
void _bake_gi_probe(Instance *p_gi_probe);
|
2016-12-20 04:21:07 +01:00
|
|
|
bool _check_gi_probe(Instance *p_gi_probe);
|
|
|
|
void _setup_gi_probe(Instance *p_instance);
|
|
|
|
|
2016-11-19 17:23:37 +01:00
|
|
|
void render_probes();
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
bool free(RID p_rid);
|
|
|
|
|
2021-02-04 11:43:08 +01:00
|
|
|
private:
|
|
|
|
bool _use_bvh;
|
|
|
|
VisualServerCallbacks *_visual_server_callbacks;
|
2022-02-11 15:36:02 +01:00
|
|
|
PortalResources _portal_resources;
|
2021-02-04 11:43:08 +01:00
|
|
|
|
|
|
|
public:
|
2016-10-19 16:14:41 +02:00
|
|
|
VisualServerScene();
|
2018-10-02 12:07:44 +02:00
|
|
|
virtual ~VisualServerScene();
|
2016-10-19 16:14:41 +02:00
|
|
|
};
|
|
|
|
|
2022-07-25 12:33:41 +02:00
|
|
|
#endif // VISUAL_SERVER_SCENE_H
|