Browse Source

Merge pull request #94726 from BastiaanOlij/primitive_texel_size_update

Make primitive meshes react to texel size change
Rémi Verschelde 1 year ago
parent
commit
5960555f9b
2 changed files with 30 additions and 17 deletions
  1. 25 17
      scene/resources/3d/primitive_meshes.cpp
  2. 5 0
      scene/resources/3d/primitive_meshes.h

+ 25 - 17
scene/resources/3d/primitive_meshes.cpp

@@ -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));
 }
 
 /**
@@ -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
@@ -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);
@@ -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;
@@ -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);
@@ -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;
@@ -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);
@@ -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);
@@ -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.
@@ -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;
@@ -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;
@@ -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);
@@ -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;
@@ -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;

+ 5 - 0
scene/resources/3d/primitive_meshes.h

@@ -67,6 +67,9 @@ protected:
 	// 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 {}
@@ -76,6 +79,8 @@ protected:
 	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;