Skip to content

Instantly share code, notes, and snippets.

@JackieMaHatesDogEyes
Last active April 5, 2024 04:28
Show Gist options
  • Save JackieMaHatesDogEyes/9cfcaf1a4073b4bc5a2965a958ce0df8 to your computer and use it in GitHub Desktop.
Save JackieMaHatesDogEyes/9cfcaf1a4073b4bc5a2965a958ce0df8 to your computer and use it in GitHub Desktop.
Static Mesh Collision Generator for Godot 4
@tool
extends Node
class_name StaticMeshCollisionGenerator
enum CollisionType {
CONVEX,
COMPLEX
}
@export var runScript : bool = false : set = Run
@export var collision_type : CollisionType = CollisionType.COMPLEX
@export var output_filepath = "res://output.tscn"
@export var root : Node
func Run(a):
runScript = false
if !root:
root = self.get_parent()
# Create a Copy of root
var copy = PackedScene.new()
copy.pack(root)
# Instantiate that copy, effectively making a duplicate (with children).
root = copy.instantiate()
addCollision(root)
# Create a PackedScene
var scene = PackedScene.new()
scene.pack(root)
# Export PackedScene to specified output path.
ResourceSaver.save(scene, output_filepath)
func addCollision(node: Node):
# Go through children, looking for MeshInstance, then add collision if it isn't in the "no_collision" group
if node is MeshInstance3D and !node.is_in_group("no_collision"):
match collision_type:
CollisionType.CONVEX:
node.create_convex_collision()
print("Created Convex Collision for: ", node)
CollisionType.COMPLEX:
node.create_trimesh_collision()
print("Created Complex Collision for: ", node)
# Recursively call addCollision on all children.
for child in node.get_children():
addCollision(child)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment