How to Create UV2 with C#?
Asked Answered
A

10

0

I have a GDScript to generate UV2:

tool

extends Spatial

export var prepare_for_baking = false setget toggle_prepare

func toggle_prepare(condition):
	if condition == true:
		for node in get_children():
			if node is MeshInstance:
				if node.mesh is ArrayMesh:
					node.mesh = node.mesh.duplicate()
					node.mesh.lightmap_unwrap(node.global_transform, 0.05)
					node.use_in_baked_light = true

I converted it to C#:

using Godot;
using System;

[Tool]
public class assets : Spatial
{
    private bool _prepareForBaking;

    [Export]
    private bool PrepareForBaking
    {
        get { return _prepareForBaking; }
        set
        {
            _prepareForBaking = value;
            if(_prepareForBaking)
            {
                foreach(Node node in GetChildren())
                {
                    if(node is MeshInstance)
                    {
                        MeshInstance meshInstance = (MeshInstance)node;

                        if(meshInstance.Mesh is ArrayMesh)
                        {
                            ArrayMesh arrayMesh = (ArrayMesh)meshInstance.Mesh;

                            // Make the mesh unique:
                            arrayMesh = (ArrayMesh)arrayMesh.Duplicate();

                            // Make UV2:
                            arrayMesh.LightmapUnwrap(meshInstance.GlobalTransform, 0.05f);

                            // Check the Use In Baked Light checkbox:
                            meshInstance.UseInBakedLight = true; 
                        }
                    }
                }
            }
        }
    }
}

But the C# script cannot generate UV2. How to generate UV2 with C# script?

Abduct answered 30/10, 2023 at 11:45 Comment(0)
A
1

Abduct Your code makes a copy of the array mesh, stores it into local variable, then does the unwrap on this mesh but never assigns it back to the mesh instance. This duplicate will be deleted as soon as your code leaves the local scope it was declared in.

Apyretic answered 30/10, 2023 at 23:58 Comment(0)
A
0

Abduct cannot

What do you mean by that. Do you get any errors?

Please format your code properly using ~~~ tags.

Apyretic answered 30/10, 2023 at 12:14 Comment(0)
A
0

Apyretic Sorry, I have just formatted the code properly. The C# script doesn't generate any errors, but it also doesn't generate UV2 for the geometries. Did I translate the GDScript wrongly to C# script?

Abduct answered 30/10, 2023 at 13:10 Comment(0)
A
0

Abduct Check if your setter is getting called at all by putting a print statement or a breakpoint into it.

Apyretic answered 30/10, 2023 at 13:15 Comment(0)
A
0

Apyretic The setter gets called. If I put a print statement, I can see the output. The "meshInstance.UseInBakedLight = true;" line works, but the "arrayMesh.LightmapUnwrap(meshInstance.GlobalTransform, 0.05f);" line doesn't.

Abduct answered 30/10, 2023 at 13:27 Comment(0)
A
0

Abduct Check the return value of LightmapUnwrap. It will return an error code if something went wrong.

Apyretic answered 30/10, 2023 at 16:54 Comment(0)
A
0

Apyretic The return value of the LightmapUnwrap is an Error with "Ok" message, but the UV2s of the geometries aren't generated.

Abduct answered 30/10, 2023 at 23:29 Comment(0)
A
0

Abduct but the UV2s of the geometries aren't generated

How did you determine that?

Apyretic answered 30/10, 2023 at 23:45 Comment(0)
A
0

Apyretic I viewed the UV2s of some geometries by clicking the Mesh button > View UV2 button. I can see the UV2s if I use the GDScript. But if I use the C# script, there will be an alert "Mesh has no UV in layer 2"

Abduct answered 30/10, 2023 at 23:52 Comment(0)
A
1

Abduct Your code makes a copy of the array mesh, stores it into local variable, then does the unwrap on this mesh but never assigns it back to the mesh instance. This duplicate will be deleted as soon as your code leaves the local scope it was declared in.

Apyretic answered 30/10, 2023 at 23:58 Comment(0)
A
0

Apyretic Thanks, it works now.

Abduct answered 31/10, 2023 at 0:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.