# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends GLTFDocumentExtension
class_name GLTFApplyNodeVisibility

# Since Godot 4.5 the KHR_node_visibility extension is
# required which is not yet supported in Blender, so we
# pre-apply them by removing meshes from hidden nodes.
func _export_post(state: GLTFState) -> Error:
	state.json["extensionsRequired"].erase("KHR_node_visibility")
	var nodes = state.json["nodes"]
	for node in nodes:
		var visible = node.get("extensions", {}).get("KHR_node_visibility", {}).get("visible", true)
		if not visible: hide_node(nodes, node)
	return Error.OK

func hide_node(nodes: Array, node: Dictionary):
	if node.has("mesh"): print("clear mesh")
	node.erase("mesh")
	for child_index in node.get("children", []):
		print("clear child child_index")
		hide_node(nodes, nodes[child_index])
