VoxelGI data serialization after baking from GDScript

Godot Version

Godot 4.4

Question

For a given RandomNumberGenerator seed, I managed to make procedural rooms with their own VoxelGI baked lightning at run-time from GDScript but I would like to save the baked data to the disk to reload them later, avoiding the cost of rebaking each time the game is restarted.

Is this technically possible ? I’m aware there is some binary serialization methods but does this work for this use case ?

You could save the VoxelGIData located in VoxelGI.data to disk with ResourceSaver.save() after baking it and load it later before trying to bake it again. If you are going to do it at runtime it would be better if you save it in the user:// path.

This was indeed my first approach but surprisingly, VoxelGIData, once saved to disk, doesn’t seem to hold baked light information. Indeed the file is then less than 500 bytes. It feel like it only save programmer parameters, like energy, bias and those sort of things.

Seems to work fine on my end:

extends Node

const PATH = "user://voxelgi.res"

func _ready() -> void:
	var voxelgi = VoxelGI.new()
	add_child(voxelgi)
	if FileAccess.file_exists(PATH):
		voxelgi.data = load(PATH)
		print("Loaded from file")
	else:
		voxelgi.bake()
		ResourceSaver.save(voxelgi.data, PATH)
		print("Baked")

I didn’t try to create the scene at runtime but make sure to follow what the VoxelGI.bake() description says:

Note: GeometryInstance3Ds and Light3Ds must be fully ready before bake() is called. If you are procedurally creating those and some meshes or lights are missing from your baked VoxelGI, use call_deferred(“bake”) instead of calling bake() directly.

1 Like

Thank you very much, you are right, I messed up with my code about when and where the bake() call is made.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.