Skip to content

Commit

Permalink
Make primitive meshes react to texel size change
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jul 26, 2024
1 parent f0e20ef commit b9790ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
42 changes: 25 additions & 17 deletions scene/resources/3d/primitive_meshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,43 @@ Vector2 PrimitiveMesh::get_uv2_scale(Vector2 p_margin_scale) const {
}

float PrimitiveMesh::get_lightmap_texel_size() const {
float texel_size = GLOBAL_GET("rendering/lightmapping/primitive_meshes/texel_size");
return texel_size;
}

if (texel_size <= 0.0) {
texel_size = 0.2;
void PrimitiveMesh::_on_settings_changed() {
float new_texel_size = float(GLOBAL_GET("rendering/lightmapping/primitive_meshes/texel_size"));
if (new_texel_size <= 0.0) {
new_texel_size = 0.2;
}
if (texel_size == new_texel_size) {
return;
}

return texel_size;
texel_size = new_texel_size;
_update_lightmap_size();
request_update();
}

PrimitiveMesh::PrimitiveMesh() {
ERR_FAIL_NULL(RenderingServer::get_singleton());
mesh = RenderingServer::get_singleton()->mesh_create();

ERR_FAIL_NULL(ProjectSettings::get_singleton());
texel_size = float(GLOBAL_GET("rendering/lightmapping/primitive_meshes/texel_size"));
if (texel_size <= 0.0) {
texel_size = 0.2;
}
ProjectSettings *project_settings = ProjectSettings::get_singleton();
project_settings->connect("settings_changed", callable_mp(this, &PrimitiveMesh::_on_settings_changed));
}

PrimitiveMesh::~PrimitiveMesh() {
ERR_FAIL_NULL(RenderingServer::get_singleton());
RenderingServer::get_singleton()->free(mesh);

ERR_FAIL_NULL(ProjectSettings::get_singleton());
ProjectSettings *project_settings = ProjectSettings::get_singleton();
project_settings->disconnect("settings_changed", callable_mp(this, &PrimitiveMesh::_on_settings_changed));
}

/**
Expand All @@ -350,7 +371,6 @@ void CapsuleMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

float radial_length = radius * Math_PI * 0.5; // circumference of 90 degree bend
Expand All @@ -365,7 +385,6 @@ void CapsuleMesh::_update_lightmap_size() {

void CapsuleMesh::_create_mesh_array(Array &p_arr) const {
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

create_mesh_array(p_arr, radius, height, radial_segments, rings, _add_uv2, _uv2_padding);
Expand Down Expand Up @@ -613,7 +632,6 @@ void BoxMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

float width = (size.x + size.z) / texel_size;
Expand All @@ -632,7 +650,6 @@ void BoxMesh::_create_mesh_array(Array &p_arr) const {
// With 3 faces along the width and 2 along the height of the texture we need to adjust our scale
// accordingly.
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

BoxMesh::create_mesh_array(p_arr, size, subdivide_w, subdivide_h, subdivide_d, _add_uv2, _uv2_padding);
Expand Down Expand Up @@ -937,7 +954,6 @@ void CylinderMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

float top_circumference = top_radius * Math_PI * 2.0;
Expand All @@ -957,7 +973,6 @@ void CylinderMesh::_update_lightmap_size() {

void CylinderMesh::_create_mesh_array(Array &p_arr) const {
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

create_mesh_array(p_arr, top_radius, bottom_radius, height, radial_segments, rings, cap_top, cap_bottom, _add_uv2, _uv2_padding);
Expand Down Expand Up @@ -1244,7 +1259,6 @@ void PlaneMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

_lightmap_size_hint.x = MAX(1.0, (size.x / texel_size) + padding);
Expand Down Expand Up @@ -1416,7 +1430,6 @@ void PrismMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

// left_to_right does not effect the surface area of the prism so we ignore that.
Expand All @@ -1440,7 +1453,6 @@ void PrismMesh::_create_mesh_array(Array &p_arr) const {

// Only used if we calculate UV2
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

float horizontal_total = size.x + size.z + 2.0 * _uv2_padding;
Expand Down Expand Up @@ -1762,7 +1774,6 @@ void SphereMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

float _width = radius * Math_TAU;
Expand All @@ -1776,7 +1787,6 @@ void SphereMesh::_update_lightmap_size() {

void SphereMesh::_create_mesh_array(Array &p_arr) const {
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

create_mesh_array(p_arr, radius, height, radial_segments, rings, is_hemisphere, _add_uv2, _uv2_padding);
Expand Down Expand Up @@ -1950,7 +1960,6 @@ void TorusMesh::_update_lightmap_size() {
if (get_add_uv2()) {
// size must have changed, update lightmap size hint
Size2i _lightmap_size_hint;
float texel_size = get_lightmap_texel_size();
float padding = get_uv2_padding();

float min_radius = inner_radius;
Expand Down Expand Up @@ -2000,7 +2009,6 @@ void TorusMesh::_create_mesh_array(Array &p_arr) const {

// Only used if we calculate UV2
bool _add_uv2 = get_add_uv2();
float texel_size = get_lightmap_texel_size();
float _uv2_padding = get_uv2_padding() * texel_size;

float horizontal_total = max_radius * Math_TAU + _uv2_padding;
Expand Down
5 changes: 5 additions & 0 deletions scene/resources/3d/primitive_meshes.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class PrimitiveMesh : public Mesh {
// assume primitive triangles as the type, correct for all but one and it will change this :)
Mesh::PrimitiveType primitive_type = Mesh::PRIMITIVE_TRIANGLES;

// Copy of our texel_size project setting.
float texel_size = 0.2;

static void _bind_methods();

virtual void _create_mesh_array(Array &p_arr) const {}
Expand All @@ -76,6 +79,8 @@ class PrimitiveMesh : public Mesh {
float get_lightmap_texel_size() const;
virtual void _update_lightmap_size(){};

void _on_settings_changed();

public:
virtual int get_surface_count() const override;
virtual int surface_get_array_len(int p_idx) const override;
Expand Down

0 comments on commit b9790ce

Please sign in to comment.