Last active
January 20, 2025 20:51
-
-
Save Oppodelldog/4a612c8b31172ed5e65d98373c2babce to your computer and use it in GitHub Desktop.
Godot 4.3: list mesh details of the actual scene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node3D | |
func _ready(): | |
get_tree().create_timer(2).timeout.connect(func(): | |
print("Listing meshes in the current scene ordered by vertex count (descending)...") | |
var meshes_data = collect_meshes(get_tree().root) | |
meshes_data.sort_custom(_sort_by_vertex_count_descending) | |
for data in meshes_data: | |
print("Vertex Count: %d (%d), Instances: %s, Node: %s, Mesh: %s" % [data["gpu_vertex_count"],data["vertex_count"], data["instance_count"], data["node_name"], data["mesh_path"]]) | |
) | |
func collect_meshes(node: Node) -> Array: | |
var meshes_data = [] | |
if node is MeshInstance3D: | |
var mesh = node.mesh | |
if mesh: | |
var vertex_count = get_vertex_count(mesh) | |
var gpu_vertex_count=get_gpu_vertex_count(mesh) | |
meshes_data.append({ | |
"node_name": node.name, | |
"mesh_path": mesh.resource_path if mesh.resource_path else "Embedded", | |
"vertex_count": vertex_count, | |
"gpu_vertex_count": gpu_vertex_count, | |
"instance_count": 1 | |
}) | |
if node is MultiMeshInstance3D: | |
var mesh:MultiMesh = node.multimesh | |
if mesh: | |
var vertex_count = get_vertex_count(mesh.mesh) | |
var gpu_vertex_count=get_gpu_vertex_count(mesh.mesh) | |
meshes_data.append({ | |
"node_name": node.name, | |
"mesh_path": mesh.mesh.resource_path if mesh.resource_path else "Embedded", | |
"vertex_count": vertex_count, | |
"gpu_vertex_count": gpu_vertex_count, | |
"instance_count": mesh.instance_count | |
}) | |
for child in node.get_children(): | |
meshes_data += collect_meshes(child) | |
return meshes_data | |
func get_vertex_count(mesh: Mesh) -> int: | |
var total_vertices = 0 | |
for surface in range(mesh.get_surface_count()): | |
var arrays = mesh.surface_get_arrays(surface) | |
if arrays.size() > Mesh.ARRAY_VERTEX: | |
total_vertices += arrays[Mesh.ARRAY_VERTEX].size() | |
return total_vertices | |
func get_gpu_vertex_count(mesh: Mesh) -> int: | |
var total_vertices = 0 | |
for surface in range(mesh.get_surface_count()): | |
var index_array = mesh.surface_get_arrays(surface)[Mesh.ARRAY_INDEX] | |
if index_array.size()==0: | |
total_vertices += mesh.surface_get_arrays(surface)[Mesh.ARRAY_VERTEX].size() | |
else: | |
total_vertices += index_array.size() | |
return total_vertices | |
func _sort_by_vertex_count_descending(a: Dictionary, b: Dictionary) ->bool: return b["vertex_count"] > a["vertex_count"] | |
func _sort_by_vertex_count_ascending(a: Dictionary, b: Dictionary) ->bool: return b["vertex_count"] < a["vertex_count"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment