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.
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.
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>
<tutorials>
</tutorials>
@ -27,10 +27,10 @@
Frees an allocated resource.
</description>
</method>
<method name="get_default_quadrant_size" qualifiers="const">
<method name="get_default_octant_size" qualifiers="const">
<return type="Vector3i" />
<description>
Get the default internal quadrant size.
Get the default internal octant size.
</description>
</method>
<method name="light_create">
@ -186,11 +186,11 @@
Returns all lights in a map.
</description>
</method>
<method name="map_get_quadrant_size" qualifiers="const">
<method name="map_get_octant_size" qualifiers="const">
<return type="Vector3i" />
<argument index="0" name="map" type="RID" />
<description>
Returns a map's quadrant size.
Returns a map's octant size.
</description>
</method>
<method name="map_set_base_color">
@ -201,7 +201,7 @@
Sets the base color of a map.
</description>
</method>
<method name="map_set_quadrant_size">
<method name="map_set_octant_size">
<return type="void" />
<argument index="0" name="map" type="RID" />
<argument index="1" name="size" type="Vector3i" />
@ -227,7 +227,7 @@
Sample the lighing information at a specific position.
</description>
</method>
<method name="set_default_quadrant_size">
<method name="set_default_octant_size">
<return type="void" />
<argument index="0" name="size" type="Vector3i" />
<description>

View File

@ -150,15 +150,15 @@ Color VertexLights3DServer::VertexLightQuadrant3D::sample_light(const Color &p_c
//VertexLightMap3D
void VertexLights3DServer::VertexLightMap3D::recreate_quadrants() {
void VertexLights3DServer::VertexLightMap3D::recreate_octants() {
List<VertexLightData3D *> 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());
}
quadrants.clear();
octants.clear();
for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) {
VertexLightData3D *l = E->get();
@ -167,48 +167,48 @@ void VertexLights3DServer::VertexLightMap3D::recreate_quadrants() {
}
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);
}
}
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->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) {
p_light->map = NULL;
VertexLightQuadrant3D *quadrant = p_light->quadrant;
VertexLightQuadrant3D *octant = p_light->octant;
// Quadrant wan not updated properly somewhere!
ERR_FAIL_NULL(quadrant);
ERR_FAIL_NULL(octant);
quadrant->lights.erase(p_light);
p_light->quadrant = NULL;
octant->lights.erase(p_light);
p_light->octant = NULL;
if (quadrant->lights.size() == 0) {
quadrants.erase(quadrant->position);
if (octant->lights.size() == 0) {
octants.erase(octant->position);
memdelete(quadrant);
memdelete(octant);
}
}
VertexLights3DServer::VertexLightQuadrant3D *VertexLights3DServer::VertexLightMap3D::get_quadrant_for_position(const Vector3 &p_position) {
Vector3i quadrant_position = to_quadrant_position(p_position);
VertexLights3DServer::VertexLightQuadrant3D *VertexLights3DServer::VertexLightMap3D::get_octant_for_position(const Vector3 &p_position) {
Vector3i octant_position = to_octant_position(p_position);
if (!quadrants.has(quadrant_position)) {
VertexLightQuadrant3D *quadrant = memnew(VertexLightQuadrant3D);
quadrant->position = quadrant_position;
quadrants[quadrant_position] = quadrant;
return quadrant;
if (!octants.has(octant_position)) {
VertexLightQuadrant3D *octant = memnew(VertexLightQuadrant3D);
octant->position = octant_position;
octants[octant_position] = octant;
return octant;
}
return quadrants[quadrant_position];
return octants[octant_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;
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());
}
quadrants.clear();
octants.clear();
for (List<VertexLightData3D *>::Element *E = lights.front(); E; E = E->next()) {
VertexLightData3D *l = E->get();
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 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 y = quadrant_position.y - 1; y <= quadrant_position.y + 1; ++y) {
for (int z = quadrant_position.z - 1; z <= quadrant_position.z + 1; ++z) {
for (int x = octant_position.x - 1; x <= octant_position.x + 1; ++x) {
for (int y = octant_position.y - 1; y <= octant_position.y + 1; ++y) {
for (int z = octant_position.z - 1; z <= octant_position.z + 1; ++z) {
Vector3i qp = Vector3i(x, y, z);
if (quadrants.has(qp)) {
VertexLightQuadrant3D *q = quadrants[qp];
if (octants.has(qp)) {
VertexLightQuadrant3D *q = octants[qp];
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 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 y = quadrant_position.y - 1; y <= quadrant_position.y + 1; ++y) {
for (int z = quadrant_position.z - 1; z <= quadrant_position.z + 1; ++z) {
for (int x = octant_position.x - 1; x <= octant_position.x + 1; ++x) {
for (int y = octant_position.y - 1; y <= octant_position.y + 1; ++y) {
for (int z = octant_position.z - 1; z <= octant_position.z + 1; ++z) {
Vector3i qp = Vector3i(x, y, z);
if (quadrants.has(qp)) {
VertexLightQuadrant3D *q = quadrants[qp];
if (octants.has(qp)) {
VertexLightQuadrant3D *q = octants[qp];
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"
// Defaults
Vector3i VertexLights3DServer::get_default_quadrant_size() const {
return _default_quadrant_size;
Vector3i VertexLights3DServer::get_default_octant_size() const {
return _default_octant_size;
}
void VertexLights3DServer::set_default_quadrant_size(const Vector3i &p_size) {
_default_quadrant_size = p_size;
void VertexLights3DServer::set_default_octant_size(const Vector3i &p_size) {
_default_octant_size = p_size;
}
// Maps
@ -48,22 +48,22 @@ RID VertexLights3DServer::map_create() {
VertexLightMap3D *map = memnew(VertexLightMap3D);
RID rid = map_owner.make_rid(map);
map->self = rid;
map->quadrant_size = _default_quadrant_size;
map->octant_size = _default_octant_size;
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);
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);
ERR_FAIL_COND(map == NULL);
map->quadrant_size = p_size;
map->recreate_quadrants();
map->octant_size = p_size;
map->recreate_octants();
_map_changed(map);
}
@ -176,7 +176,7 @@ void VertexLights3DServer::light_set_position(RID p_light, const Vector3 &p_posi
ERR_FAIL_COND(light == NULL);
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_changed(light);
return;
@ -345,8 +345,8 @@ VertexLights3DServer::VertexLights3DServer() {
_self = this;
GLOBAL_DEF("vertex_lights_3d/default_quadrant_size", Vector3i(32, 32, 32));
_default_quadrant_size = GLOBAL_GET("vertex_lights_3d/default_quadrant_size");
GLOBAL_DEF("vertex_lights_3d/default_octant_size", Vector3i(32, 32, 32));
_default_octant_size = GLOBAL_GET("vertex_lights_3d/default_octant_size");
_map_changed_name = "map_changed";
}
@ -358,13 +358,13 @@ VertexLights3DServer::~VertexLights3DServer() {
void VertexLights3DServer::_bind_methods() {
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("set_default_quadrant_size", "size"), &VertexLights3DServer::set_default_quadrant_size);
ClassDB::bind_method(D_METHOD("get_default_octant_size"), &VertexLights3DServer::get_default_octant_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_get_quadrant_size", "map"), &VertexLights3DServer::map_get_quadrant_size);
ClassDB::bind_method(D_METHOD("map_set_quadrant_size", "map", "size"), &VertexLights3DServer::map_set_quadrant_size);
ClassDB::bind_method(D_METHOD("map_get_octant_size", "map"), &VertexLights3DServer::map_get_octant_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_set_base_color", "map", "base_color"), &VertexLights3DServer::map_set_base_color);

View File

@ -55,15 +55,15 @@ public:
// Defaults
Vector3i get_default_quadrant_size() const;
void set_default_quadrant_size(const Vector3i &p_size);
Vector3i get_default_octant_size() const;
void set_default_octant_size(const Vector3i &p_size);
// Maps
RID map_create();
Vector3i map_get_quadrant_size(RID p_map) const;
void map_set_quadrant_size(RID p_map, const Vector3i &p_size);
Vector3i map_get_octant_size(RID p_map) const;
void map_set_octant_size(RID p_map, const Vector3i &p_size);
Color map_get_base_color(RID p_map) const;
void map_set_base_color(RID p_map, const Color &p_base_color);
@ -140,13 +140,13 @@ protected:
int item_cull_mask;
VertexLightMap3D *map;
VertexLightQuadrant3D *quadrant;
VertexLightQuadrant3D *octant;
RID self;
VertexLightData3D() {
map = NULL;
quadrant = NULL;
octant = NULL;
enabled = true;
range = 5;
@ -176,20 +176,20 @@ protected:
class VertexLightMap3D : public RID_Data {
public:
HashMap<Vector3i, VertexLightQuadrant3D *> quadrants;
Vector3i quadrant_size;
HashMap<Vector3i, VertexLightQuadrant3D *> octants;
Vector3i octant_size;
Color base_color;
RID self;
void recreate_quadrants();
void recreate_octants();
void get_lights(List<VertexLightData3D *> *p_lights);
void add_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);
@ -198,12 +198,12 @@ protected:
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);
_FORCE_INLINE_ Vector3i to_quadrant_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);
_FORCE_INLINE_ Vector3i to_octant_position(const Vector3 &p_position) {
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) {
return Vector3(p_quadrant_position.x * quadrant_size.x, p_quadrant_position.y * quadrant_size.y, p_quadrant_position.z * quadrant_size.z);
_FORCE_INLINE_ Vector3 to_position(const Vector3i &p_octant_position) {
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<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;
StringName _map_changed_name;