0 votes

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): https://ibb.co/k1JcpD9

Image of Mesh from Code (for some reason not used in baked light): https://ibb.co/5RgNbKC

Thanks in advance

asked Jul 20 in Engine by SRugina (19 points)
edited Jul 21 by SRugina

Are you running GI baking AFTER setting use_in_baked_light?

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()

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).

1 Answer

0 votes
Best answer

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

See the issue here: https://github.com/godotengine/godot/issues/28508

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 = $"."
answered Jul 22 by SRugina (19 points)
selected Jul 22 by SRugina
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.