mesh_instance.use_in_baked_light = true does not work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SRugina

Generating a mesh instance from code looks different than one made in the editor, after further testing I noticed that by disabling the use in baked light checkbox in the editor it looked the same as the mesh from code, suggesting that the mesh_instance.use_in_baked_light = true line in my code is not working. What is the correct piece of code to use?

This is the code I’m using:

func generate_water():
	var plane_mesh := PlaneMesh.new()
	plane_mesh.size = Vector2(400, 400)
	plane_mesh.subdivide_depth = 200
	plane_mesh.subdivide_width = 200
	
	var mesh_instance := MeshInstance.new()
    mesh_instance.name = "water"
	mesh_instance.mesh = plane_mesh
	mesh_instance.set_surface_material(0, preload("res://water.material"))
	mesh_instance.use_in_baked_light = true
	add_child(mesh_instance)

(the mesh being generated from code is the blue water, not the green cube)
Image of Mesh from Editor (with use in baked light enabled): mesh-from-editor hosted at ImgBB — ImgBB

Image of Mesh from Code (for some reason not used in baked light): mesh-from-code hosted at ImgBB — ImgBB

Thanks in advance

Are you running GI baking AFTER setting use_in_baked_light?

Zylann | 2019-07-20 21:37

Yes, my _ready function and gi baking functions look like this, the gi probe is made after the water mesh is generated:

func _ready():
	generate_water()
	generate_gi_probe()

func generate_gi_probe():
	var gi_probe := GIProbe.new()
	gi_probe.set_extents(Vector3(400.0, 400.0, 400.0))
	add_child(gi_probe)
	gi_probe.bake()

SRugina | 2019-07-21 13:02

After further debugging, I’ve discovered something peculiar. If I change the line gi_probe.bake() in the code to gi_probe.bake($water), (telling the gi_probe to bake from the water node and below, in this case nothing so bake only the water node), the water does get baked! However, this means no other meshes in the scene will be included in the baked lighting.

Trying to change the line to gi_probe.bake($".") functioned the same as gi_probe.bake() (i.e. did not work).

Does anyone know what is causing this behaviour? (ignoring meshInstances that were generated from code).

SRugina | 2019-07-21 23:54

:bust_in_silhouette: Reply From: SRugina

Found an issue on github that is the same as this post, turns out the issue is not with the use_in_baked_light flag, but with the bake function of the giprobe.

See the issue here: Bake() Method of GIProbe has no effect on runtime · Issue #28508 · godotengine/godot · GitHub

Quick Fix (also commented on the issue): set the mesh instance owner after doing add_child()
e.g.

extends Spatial

func _ready():
	generate_plane()
	generate_gi_probe()

func generate_gi_probe():
	var gi_probe := GIProbe.new()
	gi_probe.name = "gi_probe"
	gi_probe.set_extents(Vector3(400.0, 400.0, 400.0))
	add_child(gi_probe)
	gi_probe.bake()

func generate_plane():
	var plane_mesh := PlaneMesh.new()
	plane_mesh.size = Vector2(10, 10)
	
	var mesh_instance := MeshInstance.new()
	mesh_instance.name = "plane"
	mesh_instance.mesh = plane_mesh
	mesh_instance.use_in_baked_light = true
	add_child(mesh_instance)
	mesh_instance.owner = $"."