Rename quadrants to octants in the vertex lights 3d module.

This commit is contained in:
Relintai 2024-03-27 16:13:58 +01:00
parent fd1f8aa5d7
commit 4785d29a16
4 changed files with 77 additions and 77 deletions

View File

@ -9,7 +9,7 @@
After adding lights, you can use [member sample_light] to sample lighting at a specified point in the map's space, then use the resulting [Color] to either further modifty the albedo color in your shader, or you can bake vertex colors of your meshes. After adding lights, you can use [member sample_light] to sample lighting at a specified point in the map's space, then use the resulting [Color] to either further modifty the albedo color in your shader, or you can bake vertex colors of your meshes.
The drawback of this method is that it's more expensive change lights. The drawback of this method is that it's more expensive change lights.
Note that you can use the normal 3D lighting system on top of this if you need dynamic lights (like [OmniLight]). That system is better suited for that purpose. Note that you can use the normal 3D lighting system on top of this if you need dynamic lights (like [OmniLight]). That system is better suited for that purpose.
Currently this class stores lights inside quadrants. Their optimal size should be a bit more than the radius of the biggest light you want to have. Currently this class stores lights inside octants. Their optimal size should be a bit more than the radius of the biggest light you want to have.
</description> </description>
<tutorials> <tutorials>
</tutorials> </tutorials>
@ -27,10 +27,10 @@
Frees an allocated resource. Frees an allocated resource.
</description> </description>
</method> </method>
<method name="get_default_quadrant_size" qualifiers="const"> <method name="get_default_octant_size" qualifiers="const">
<return type="Vector3i" /> <return type="Vector3i" />
<description> <description>
Get the default internal quadrant size. Get the default internal octant size.
</description> </description>
</method> </method>
<method name="light_create"> <method name="light_create">
@ -186,11 +186,11 @@
Returns all lights in a map. Returns all lights in a map.
</description> </description>
</method> </method>
<method name="map_get_quadrant_size" qualifiers="const"> <method name="map_get_octant_size" qualifiers="const">
<return type="Vector3i" /> <return type="Vector3i" />
<argument index="0" name="map" type="RID" /> <argument index="0" name="map" type="RID" />
<description> <description>
Returns a map's quadrant size. Returns a map's octant size.
</description> </description>
</method> </method>
<method name="map_set_base_color"> <method name="map_set_base_color">
@ -201,7 +201,7 @@
Sets the base color of a map. Sets the base color of a map.
</description> </description>
</method> </method>
<method name="map_set_quadrant_size"> <method name="map_set_octant_size">
<return type="void" /> <return type="void" />
<argument index="0" name="map" type="RID" /> <argument index="0" name="map" type="RID" />
<argument index="1" name="size" type="Vector3i" /> <argument index="1" name="size" type="Vector3i" />
@ -227,7 +227,7 @@
Sample the lighing information at a specific position. Sample the lighing information at a specific position.
</description> </description>
</method> </method>
<method name="set_default_quadrant_size"> <method name="set_default_octant_size">
<return type="void" /> <return type="void" />
<argument index="0" name="size" type="Vector3i" /> <argument index="0" name="size" type="Vector3i" />
<description> <description>

View File

@ -150,15 +150,15 @@ Color VertexLights3DServer::VertexLightQuadrant3D::sample_light(const Color &p_c
//VertexLightMap3D //VertexLightMap3D
void VertexLights3DServer::VertexLightMap3D::recreate_quadrants() { void VertexLights3DServer::VertexLightMap3D::recreate_octants() {
List<VertexLightData3D *> lights; List<VertexLightData3D *> lights;
get_lights(&lights); get_lights(&lights);
for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = quadrants.front(); E; E = E->next) { for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = octants.front(); E; E = E->next) {
memdelete(E->value()); memdelete(E->value());
} }
quadrants.clear(); octants.clear();
for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) { for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) {
VertexLightData3D *l = E->get(); VertexLightData3D *l = E->get();
@ -167,48 +167,48 @@ void VertexLights3DServer::VertexLightMap3D::recreate_quadrants() {
} }
void VertexLights3DServer::VertexLightMap3D::get_lights(List<VertexLightData3D *> *p_lights) { void VertexLights3DServer::VertexLightMap3D::get_lights(List<VertexLightData3D *> *p_lights) {
for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = quadrants.front(); E; E = E->next) { for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = octants.front(); E; E = E->next) {
E->value()->get_lights(p_lights); E->value()->get_lights(p_lights);
} }
} }
void VertexLights3DServer::VertexLightMap3D::add_light(VertexLightData3D *p_light) { void VertexLights3DServer::VertexLightMap3D::add_light(VertexLightData3D *p_light) {
VertexLightQuadrant3D *quadrant = get_quadrant_for_position(p_light->position); VertexLightQuadrant3D *octant = get_octant_for_position(p_light->position);
p_light->map = this; p_light->map = this;
p_light->quadrant = quadrant; p_light->octant = octant;
quadrant->lights.push_back(p_light); octant->lights.push_back(p_light);
} }
void VertexLights3DServer::VertexLightMap3D::remove_light(VertexLightData3D *p_light) { void VertexLights3DServer::VertexLightMap3D::remove_light(VertexLightData3D *p_light) {
p_light->map = NULL; p_light->map = NULL;
VertexLightQuadrant3D *quadrant = p_light->quadrant; VertexLightQuadrant3D *octant = p_light->octant;
// Quadrant wan not updated properly somewhere! // Quadrant wan not updated properly somewhere!
ERR_FAIL_NULL(quadrant); ERR_FAIL_NULL(octant);
quadrant->lights.erase(p_light); octant->lights.erase(p_light);
p_light->quadrant = NULL; p_light->octant = NULL;
if (quadrant->lights.size() == 0) { if (octant->lights.size() == 0) {
quadrants.erase(quadrant->position); octants.erase(octant->position);
memdelete(quadrant); memdelete(octant);
} }
} }
VertexLights3DServer::VertexLightQuadrant3D *VertexLights3DServer::VertexLightMap3D::get_quadrant_for_position(const Vector3 &p_position) { VertexLights3DServer::VertexLightQuadrant3D *VertexLights3DServer::VertexLightMap3D::get_octant_for_position(const Vector3 &p_position) {
Vector3i quadrant_position = to_quadrant_position(p_position); Vector3i octant_position = to_octant_position(p_position);
if (!quadrants.has(quadrant_position)) { if (!octants.has(octant_position)) {
VertexLightQuadrant3D *quadrant = memnew(VertexLightQuadrant3D); VertexLightQuadrant3D *octant = memnew(VertexLightQuadrant3D);
quadrant->position = quadrant_position; octant->position = octant_position;
quadrants[quadrant_position] = quadrant; octants[octant_position] = octant;
return quadrant; return octant;
} }
return quadrants[quadrant_position]; return octants[octant_position];
} }
void VertexLights3DServer::VertexLightMap3D::set_light_position(VertexLightData3D *p_light, const Vector3 &p_position) { void VertexLights3DServer::VertexLightMap3D::set_light_position(VertexLightData3D *p_light, const Vector3 &p_position) {
@ -221,32 +221,32 @@ void VertexLights3DServer::VertexLightMap3D::clear() {
List<VertexLightData3D *> lights; List<VertexLightData3D *> lights;
get_lights(&lights); get_lights(&lights);
for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = quadrants.front(); E; E = E->next) { for (HashMap<Vector3i, VertexLightQuadrant3D *>::Element *E = octants.front(); E; E = E->next) {
memdelete(E->value()); memdelete(E->value());
} }
quadrants.clear(); octants.clear();
for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) { for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) {
VertexLightData3D *l = E->get(); VertexLightData3D *l = E->get();
l->map = NULL; l->map = NULL;
l->quadrant = NULL; l->octant = NULL;
} }
} }
Color VertexLights3DServer::VertexLightMap3D::sample_light_value(const Vector3 &p_position, const int p_item_cull_mask) { Color VertexLights3DServer::VertexLightMap3D::sample_light_value(const Vector3 &p_position, const int p_item_cull_mask) {
Color c = base_color; Color c = base_color;
Vector3i quadrant_position = to_quadrant_position(p_position); Vector3i octant_position = to_octant_position(p_position);
for (int x = quadrant_position.x - 1; x <= quadrant_position.x + 1; ++x) { for (int x = octant_position.x - 1; x <= octant_position.x + 1; ++x) {
for (int y = quadrant_position.y - 1; y <= quadrant_position.y + 1; ++y) { for (int y = octant_position.y - 1; y <= octant_position.y + 1; ++y) {
for (int z = quadrant_position.z - 1; z <= quadrant_position.z + 1; ++z) { for (int z = octant_position.z - 1; z <= octant_position.z + 1; ++z) {
Vector3i qp = Vector3i(x, y, z); Vector3i qp = Vector3i(x, y, z);
if (quadrants.has(qp)) { if (octants.has(qp)) {
VertexLightQuadrant3D *q = quadrants[qp]; VertexLightQuadrant3D *q = octants[qp];
c = q->sample_light_value(c, p_position, p_item_cull_mask); c = q->sample_light_value(c, p_position, p_item_cull_mask);
} }
@ -260,15 +260,15 @@ Color VertexLights3DServer::VertexLightMap3D::sample_light_value(const Vector3 &
Color VertexLights3DServer::VertexLightMap3D::sample_light(const Vector3 &p_position, const Vector3 &p_normal, const int p_item_cull_mask) { Color VertexLights3DServer::VertexLightMap3D::sample_light(const Vector3 &p_position, const Vector3 &p_normal, const int p_item_cull_mask) {
Color c = base_color; Color c = base_color;
Vector3i quadrant_position = to_quadrant_position(p_position); Vector3i octant_position = to_octant_position(p_position);
for (int x = quadrant_position.x - 1; x <= quadrant_position.x + 1; ++x) { for (int x = octant_position.x - 1; x <= octant_position.x + 1; ++x) {
for (int y = quadrant_position.y - 1; y <= quadrant_position.y + 1; ++y) { for (int y = octant_position.y - 1; y <= octant_position.y + 1; ++y) {
for (int z = quadrant_position.z - 1; z <= quadrant_position.z + 1; ++z) { for (int z = octant_position.z - 1; z <= octant_position.z + 1; ++z) {
Vector3i qp = Vector3i(x, y, z); Vector3i qp = Vector3i(x, y, z);
if (quadrants.has(qp)) { if (octants.has(qp)) {
VertexLightQuadrant3D *q = quadrants[qp]; VertexLightQuadrant3D *q = octants[qp];
c = q->sample_light(c, p_position, p_normal, p_item_cull_mask); c = q->sample_light(c, p_position, p_normal, p_item_cull_mask);
} }

View File

@ -36,11 +36,11 @@
#include "scene/main/scene_tree.h" #include "scene/main/scene_tree.h"
// Defaults // Defaults
Vector3i VertexLights3DServer::get_default_quadrant_size() const { Vector3i VertexLights3DServer::get_default_octant_size() const {
return _default_quadrant_size; return _default_octant_size;
} }
void VertexLights3DServer::set_default_quadrant_size(const Vector3i &p_size) { void VertexLights3DServer::set_default_octant_size(const Vector3i &p_size) {
_default_quadrant_size = p_size; _default_octant_size = p_size;
} }
// Maps // Maps
@ -48,22 +48,22 @@ RID VertexLights3DServer::map_create() {
VertexLightMap3D *map = memnew(VertexLightMap3D); VertexLightMap3D *map = memnew(VertexLightMap3D);
RID rid = map_owner.make_rid(map); RID rid = map_owner.make_rid(map);
map->self = rid; map->self = rid;
map->quadrant_size = _default_quadrant_size; map->octant_size = _default_octant_size;
return rid; return rid;
} }
Vector3i VertexLights3DServer::map_get_quadrant_size(RID p_map) const { Vector3i VertexLights3DServer::map_get_octant_size(RID p_map) const {
const VertexLightMap3D *map = map_owner.getornull(p_map); const VertexLightMap3D *map = map_owner.getornull(p_map);
ERR_FAIL_COND_V(map == NULL, Vector3i()); ERR_FAIL_COND_V(map == NULL, Vector3i());
return map->quadrant_size; return map->octant_size;
} }
void VertexLights3DServer::map_set_quadrant_size(RID p_map, const Vector3i &p_size) { void VertexLights3DServer::map_set_octant_size(RID p_map, const Vector3i &p_size) {
VertexLightMap3D *map = map_owner.getornull(p_map); VertexLightMap3D *map = map_owner.getornull(p_map);
ERR_FAIL_COND(map == NULL); ERR_FAIL_COND(map == NULL);
map->quadrant_size = p_size; map->octant_size = p_size;
map->recreate_quadrants(); map->recreate_octants();
_map_changed(map); _map_changed(map);
} }
@ -176,7 +176,7 @@ void VertexLights3DServer::light_set_position(RID p_light, const Vector3 &p_posi
ERR_FAIL_COND(light == NULL); ERR_FAIL_COND(light == NULL);
if (light->map) { if (light->map) {
// This ensure the light gets moved to the proper quadrant // This ensure the light gets moved to the proper octant
light->map->set_light_position(light, p_position); light->map->set_light_position(light, p_position);
_light_changed(light); _light_changed(light);
return; return;
@ -345,8 +345,8 @@ VertexLights3DServer::VertexLights3DServer() {
_self = this; _self = this;
GLOBAL_DEF("vertex_lights_3d/default_quadrant_size", Vector3i(32, 32, 32)); GLOBAL_DEF("vertex_lights_3d/default_octant_size", Vector3i(32, 32, 32));
_default_quadrant_size = GLOBAL_GET("vertex_lights_3d/default_quadrant_size"); _default_octant_size = GLOBAL_GET("vertex_lights_3d/default_octant_size");
_map_changed_name = "map_changed"; _map_changed_name = "map_changed";
} }
@ -358,13 +358,13 @@ VertexLights3DServer::~VertexLights3DServer() {
void VertexLights3DServer::_bind_methods() { void VertexLights3DServer::_bind_methods() {
ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map"))); ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
ClassDB::bind_method(D_METHOD("get_default_quadrant_size"), &VertexLights3DServer::get_default_quadrant_size); ClassDB::bind_method(D_METHOD("get_default_octant_size"), &VertexLights3DServer::get_default_octant_size);
ClassDB::bind_method(D_METHOD("set_default_quadrant_size", "size"), &VertexLights3DServer::set_default_quadrant_size); ClassDB::bind_method(D_METHOD("set_default_octant_size", "size"), &VertexLights3DServer::set_default_octant_size);
ClassDB::bind_method(D_METHOD("map_create"), &VertexLights3DServer::map_create); ClassDB::bind_method(D_METHOD("map_create"), &VertexLights3DServer::map_create);
ClassDB::bind_method(D_METHOD("map_get_quadrant_size", "map"), &VertexLights3DServer::map_get_quadrant_size); ClassDB::bind_method(D_METHOD("map_get_octant_size", "map"), &VertexLights3DServer::map_get_octant_size);
ClassDB::bind_method(D_METHOD("map_set_quadrant_size", "map", "size"), &VertexLights3DServer::map_set_quadrant_size); ClassDB::bind_method(D_METHOD("map_set_octant_size", "map", "size"), &VertexLights3DServer::map_set_octant_size);
ClassDB::bind_method(D_METHOD("map_get_base_color", "map"), &VertexLights3DServer::map_get_base_color); ClassDB::bind_method(D_METHOD("map_get_base_color", "map"), &VertexLights3DServer::map_get_base_color);
ClassDB::bind_method(D_METHOD("map_set_base_color", "map", "base_color"), &VertexLights3DServer::map_set_base_color); ClassDB::bind_method(D_METHOD("map_set_base_color", "map", "base_color"), &VertexLights3DServer::map_set_base_color);

View File

@ -55,15 +55,15 @@ public:
// Defaults // Defaults
Vector3i get_default_quadrant_size() const; Vector3i get_default_octant_size() const;
void set_default_quadrant_size(const Vector3i &p_size); void set_default_octant_size(const Vector3i &p_size);
// Maps // Maps
RID map_create(); RID map_create();
Vector3i map_get_quadrant_size(RID p_map) const; Vector3i map_get_octant_size(RID p_map) const;
void map_set_quadrant_size(RID p_map, const Vector3i &p_size); void map_set_octant_size(RID p_map, const Vector3i &p_size);
Color map_get_base_color(RID p_map) const; Color map_get_base_color(RID p_map) const;
void map_set_base_color(RID p_map, const Color &p_base_color); void map_set_base_color(RID p_map, const Color &p_base_color);
@ -140,13 +140,13 @@ protected:
int item_cull_mask; int item_cull_mask;
VertexLightMap3D *map; VertexLightMap3D *map;
VertexLightQuadrant3D *quadrant; VertexLightQuadrant3D *octant;
RID self; RID self;
VertexLightData3D() { VertexLightData3D() {
map = NULL; map = NULL;
quadrant = NULL; octant = NULL;
enabled = true; enabled = true;
range = 5; range = 5;
@ -176,20 +176,20 @@ protected:
class VertexLightMap3D : public RID_Data { class VertexLightMap3D : public RID_Data {
public: public:
HashMap<Vector3i, VertexLightQuadrant3D *> quadrants; HashMap<Vector3i, VertexLightQuadrant3D *> octants;
Vector3i quadrant_size; Vector3i octant_size;
Color base_color; Color base_color;
RID self; RID self;
void recreate_quadrants(); void recreate_octants();
void get_lights(List<VertexLightData3D *> *p_lights); void get_lights(List<VertexLightData3D *> *p_lights);
void add_light(VertexLightData3D *p_light); void add_light(VertexLightData3D *p_light);
void remove_light(VertexLightData3D *p_light); void remove_light(VertexLightData3D *p_light);
VertexLightQuadrant3D *get_quadrant_for_position(const Vector3 &p_position); VertexLightQuadrant3D *get_octant_for_position(const Vector3 &p_position);
void set_light_position(VertexLightData3D *p_light, const Vector3 &p_position); void set_light_position(VertexLightData3D *p_light, const Vector3 &p_position);
@ -198,12 +198,12 @@ protected:
Color sample_light_value(const Vector3 &p_position, const int p_item_cull_mask); Color sample_light_value(const Vector3 &p_position, const int p_item_cull_mask);
Color sample_light(const Vector3 &p_position, const Vector3 &p_normal, const int p_item_cull_mask); Color sample_light(const Vector3 &p_position, const Vector3 &p_normal, const int p_item_cull_mask);
_FORCE_INLINE_ Vector3i to_quadrant_position(const Vector3 &p_position) { _FORCE_INLINE_ Vector3i to_octant_position(const Vector3 &p_position) {
return Vector3i(p_position.x / quadrant_size.x, p_position.y / quadrant_size.y, p_position.z / quadrant_size.z); return Vector3i(p_position.x / octant_size.x, p_position.y / octant_size.y, p_position.z / octant_size.z);
} }
_FORCE_INLINE_ Vector3 to_position(const Vector3i &p_quadrant_position) { _FORCE_INLINE_ Vector3 to_position(const Vector3i &p_octant_position) {
return Vector3(p_quadrant_position.x * quadrant_size.x, p_quadrant_position.y * quadrant_size.y, p_quadrant_position.z * quadrant_size.z); return Vector3(p_octant_position.x * octant_size.x, p_octant_position.y * octant_size.y, p_octant_position.z * octant_size.z);
} }
}; };
@ -228,9 +228,9 @@ protected:
mutable RID_Owner<VertexLightMap3D> map_owner; mutable RID_Owner<VertexLightMap3D> map_owner;
mutable RID_Owner<VertexLightData3D> light_owner; mutable RID_Owner<VertexLightData3D> light_owner;
Vector3i _default_quadrant_size; Vector3i _default_octant_size;
// Maybe an api could be adde that's per quadrant // Maybe an api could be adde that's per octant
mutable HashSet<RID> _changed_maps; mutable HashSet<RID> _changed_maps;
StringName _map_changed_name; StringName _map_changed_name;