diff --git a/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift b/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift index bdeca33..6132096 100644 --- a/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift +++ b/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift @@ -181,10 +181,19 @@ extension Entity { } if element.attributeBoolean(for: "generateCollisionShapes") { - self.generateCollisionShapes( - recursive: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "recursive")), - static: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "static")) - ) + if element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "convex")) { + // convex mesh collision shapes + self.generateConvexCollisionShapes( + recursive: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "recursive")), + static: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "static")) + ) + } else { + // simple box collision shapes + self.generateCollisionShapes( + recursive: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "recursive")), + static: element.attributeBoolean(for: .init(namespace: "generateCollisionShapes", name: "static")) + ) + } } } @@ -245,3 +254,19 @@ extension Entity { } } } + +extension Entity { + func generateConvexCollisionShapes( + recursive: Bool, + static isStatic: Bool + ) { + if let mesh = self.components[ModelComponent.self]?.mesh { + self.components.set(CollisionComponent(shapes: [.generateConvex(from: mesh)], isStatic: isStatic)) + } + if recursive { + for child in children { + child.generateConvexCollisionShapes(recursive: recursive, static: isStatic) + } + } + } +}