From 15a5bea7279bf89f23fc99adaf1d0f93d4676079 Mon Sep 17 00:00:00 2001
From: Relintai <relintai@protonmail.com>
Date: Thu, 11 Aug 2022 18:09:33 +0200
Subject: [PATCH] Clear surfaces in ImmediateMesh before adding a new one.

---
 scene/resources/immediate_mesh.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/scene/resources/immediate_mesh.cpp b/scene/resources/immediate_mesh.cpp
index b086d829b..887525eb4 100644
--- a/scene/resources/immediate_mesh.cpp
+++ b/scene/resources/immediate_mesh.cpp
@@ -256,6 +256,12 @@ void ImmediateMesh::surface_end() {
 		}
 	}
 
+	int sc = VisualServer::get_singleton()->mesh_get_surface_count(mesh);
+
+	for (int i = 0; i < sc; ++i) {
+		VisualServer::get_singleton()->mesh_remove_surface(mesh, 0);
+	}
+
 	VisualServer::get_singleton()->mesh_add_surface(mesh, format, static_cast<VS::PrimitiveType>(active_surface_data.primitive), surface_array_create_cache, vertices.size(), PoolByteArray(), 0, aabb);
 
 	if (active_surface_data.material.is_valid()) {