Looks like it is a known issue known as “Lazy shader compilation” that makes the shaders to be compiled in run time at the moment they are used the first time. It is solved in some cases but not for others:
# Create a Scene with a Node
# Add this script to it
# Add the Scene to the Autoloads
# Fulfill the material Array with all the Materials that are causing freeze
# preload_materials.gd
extends Node
# Load the array through Inspector
@export var materials: Array[Material]
func _ready():
_load_materials()
func _load_materials():
var sprites = []
for material in materials:
var sprite = Sprite2D.new()
sprite.texture = PlaceholderTexture2D.new()
sprite.material = material
add_child(sprite)
sprites.append(sprite)
# Remove the sprites after being rendered
await get_tree().create_timer(0.2).timeout
for sprite in sprites:
sprite.queue_free()
Fulfill the Array[Materials] through the inspector
It also works from Material from a ParticlesSystem that was also causing lag in my game.
I have seen other solutions, in case my one is not working for you: