1

Подскажите, у меня в ресурсах сцена есть, как в ней поменять нодам материалы? Вот мой код, который не работает (он делает из fbx сцены, но не присваивает моделям материалы с текстурами, хотя должен):

extends Node


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
    import()


func textureAll(node:Node):
    print("Texturing parts...")
    if node is MeshInstance:
        node = texture(node)
    for part in node.get_children():
        textureAll(part)

func toon(mat:SpatialMaterial):
    var mat2 : SpatialMaterial = SpatialMaterial.new()
    mat2.albedo_color = Color.black
    mat2.flags_unshaded = true
    mat2.render_priority = -1
    mat2.params_grow = true
    mat2.params_grow_amount = 0.5
    mat.set_next_pass(mat2)
    return mat

func texture(node : MeshInstance):
    # add material to model
    print ("Adding material to "+node.name+"...")
    var mat : SpatialMaterial = SpatialMaterial.new() 
    # add toon effect
    mat = toon(mat)
    # add textures to material 
    print ("Adding textures to "+node.name+"...")
    var texPath = "res://models/textures/"
    var texFiles = get_filelist(texPath)
    for t in texFiles:
        var n = t
        print(n)
        if node.name+"_" in n && !("import" in n)&& !("meta" in n):
            var pic = load(n)
            print(n)
            if "Albedo" in n:
                mat.albedo_texture = pic

            elif "Height in n":
                mat.depth_enabled = true
                mat.depth_texture = t

            elif "Metallic" in n:
                mat.metallic = 0.5
                mat.metallic_texture = pic

            elif "Normal" in n:
                mat.normal_enabled = true
                mat.normal_texture = pic

            elif "Occlusion" in n:
                mat.ao_enabled = true
                mat.ao_light_affect = 0.5
                mat.ao_texture = pic

            elif "Specular" in n:
                mat.roughness = 0.5
                mat.roughness_texture = pic

            elif "Emission" in n:
                mat.emission_enabled = true
                mat.emission_texture = pic

    node.set_surface_material(0, mat)
    return node

func get_filelist(scan_dir : String) -> Array:
    var my_files : Array = []
    var dir := Directory.new()
    if dir.open(scan_dir) != OK:
        printerr("Warning: could not open directory: ", scan_dir)
        return []

    if dir.list_dir_begin(true, true) != OK:
        printerr("Warning: could not list contents of: ", scan_dir)
        return []

    var file_name := dir.get_next()
    while file_name != "":
        if dir.current_is_dir():
            my_files += get_filelist(dir.get_current_dir() + "/" + file_name)
        else:
            my_files.append(dir.get_current_dir() + "/" + file_name)

        file_name = dir.get_next()

    return my_files

func import():
    for i in get_parent().get_node("fbx").get_children():
        print("Importing "+i.name+"...")
        var my_scene : PackedScene = PackedScene.new()
        var result = my_scene.pack(i)

        print(result)
        if result == OK:
            print("Texturing scene or model...")


            print ("Saving object in resources...")
            var p = "res://models/tscn/"+i.name+".tscn"

# warning-ignore:return_value_discarded
            ResourceSaver.save(p, my_scene)
            var ready_scene = load(p).instance()
            textureAll(ready_scene)
            print("Save textures...")

            print("Adding to scene...")
            get_parent().get_node("tscn").add_child(ready_scene)
# warning-ignore:return_value_discarded
            my_scene.pack(ready_scene)
            p="res://models/tscn/textured_models/"+i.name+".tscn"
            printerr(ResourceSaver.save(p, my_scene))
  • Нашел кучу ошибок в своем коде... не привожу новую версию здесь т.к. она уж слишком под мою конкретную задачу заточенной получилась. И все ровно делаю много работы вручную, без скрипта. Вопрос исчерпан, иду другим путем – Bohdan Z. Apr 07 '20 at 11:15

0 Answers0