How to make glb material unique(with demo)

Godot Version

Godot4.4 dev3

Question

instantiate a glb model,bug the material is auto load,how to make it unique,so i can change the material not affects other instance mateiral.
and can make the material unique by import?

if i drag the glb to scene, there’s no way to make it unique.
if by code like this:

item_scene = scene.instantiate()
add_child(item_scene)
var scene_mesh=item_scene.mesh
# 获取第一个 surface 的材质
var mat =scene_mesh.surface_get_material(0)
if mat:
mat = mat.duplicate()
mat.set_shader_parameter(“mahjong_texture”, res_item.texture)
scene_mesh.surface_set_material(0,mat)

it still affacts other instance. i’m confused,anyone can help me?what’s the best way to make it not shared?
ps:i also tried the local to scene in the material,and get the same problem

===============more demo==========
github:GitHub - jinruozai/unique_material_test: bug test
in the demo,i’ve check both the material and shade file resource_local_to_scene.
and in the root script, i use the duplicate(true). i tried all i can try,but not find the right way:

extends Node3D

@onready var plane1:MeshInstance3D = $Plane1/Plane
@onready var plane2:MeshInstance3D = $Plane2/Plane

const ICON_1 = preload(“res://icon.png”)
const ICON_2 = preload(“res://icon2.png”)

func _ready():
set_plane_mat(plane1,ICON_1)
set_plane_mat(plane2,ICON_2)

func set_plane_mat(tag,tex):
var mat:ShaderMaterial = tag.mesh.surface_get_material(0)
if mat:
var newmat=mat.duplicate(true)
newmat.set_shader_parameter(“material_icon”, ICON_1)
tag.mesh.surface_set_material(0,newmat)

just go where you provide material ( on mesh model ) in inspector tab and right click on it and select make it unique.

1 Like

it’s a glb file. in the glb inspector, click make unique of the material will get a noti “changes may be lost”
if inherited the glb,"make unique "button is gray and can’t click.

Try changing

scene_mesh.surface_set_material(0, mat)

to

scene_mesh.set_surface_override_material(0, mat)
1 Like

maybe you accidentally made the mesh unique and not the material? idk

this may work,but i need to use the override material for other effects.

i’ve try a lot. not only the mesh unique,but the material and the shader are all unique,but not work. i am really confuseed wthe the share logic of the material.
and why after mat.duplicate() and mat.duplicate(true) also affects each other

How can you use it on top of using an original material? I am of the belief that it replaces the material in that slot? I don’t believe it works like a decal. So if you were using it for another effect then wouldn’t the original change no longer matter anyway, because you won’t be able to see the material being overridden? If you have other slots they won’t be affected so you could have one change on one slot and use another slot for “another effect”? Or if your other effect is temporary then you could try storing it’s value in a variable and then reapplying it when you want the “original” value back?

In regard to duplicating the material, are you sure you’re not confusing changes to the meshes material being propagated across rather than changes to the material being propagated across? E.g. are you sure your not using the same mesh and continually applying different materials to it, rather than different materials all getting affected by the same change?

1 Like

Thank you so much for your detailed explanation! I now realize that I misunderstood material_override as something like material_overlay. With your clarification, the issue is perfectly resolved. I see now that I shouldn’t have focused so much on modifying the original material—this was entirely my mistake. Thanks again for your help!

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