# SPDX-License-Identifier: Apache-2.0
@tool
extends EditorScenePostImport

# Applied only to assets installed with `gt polyfork add`.
# Keep geometry and source GLBs unchanged; save corrected imported materials.
func _post_import(scene: Node) -> Object:
	_fix_materials(scene)
	return scene

func _fix_materials(node: Node) -> void:
	if node is MeshInstance3D and node.mesh != null:
		for surface in range(node.mesh.get_surface_count()):
			var arrays = node.mesh.surface_get_arrays(surface)
			var colors = arrays[Mesh.ARRAY_COLOR]
			if colors == null or colors.is_empty():
				continue
			var material = node.get_active_material(surface)
			if material is BaseMaterial3D:
				var corrected = material.duplicate() as BaseMaterial3D
				corrected.vertex_color_use_as_albedo = true
				node.set_surface_override_material(surface, corrected)
	for child in node.get_children():
		_fix_materials(child)
