How to extract individual characters from 3D Text Mesh

32 Views Asked by At

I am showing 3D Text using RealityKit in SwiftUI. I am generating a 3D text mesh using MeshResource.generateText.

How can I separate the mesh accessing the individual character to apply physics/collision to each character within the mesh e.g. to achieve some explosion effect?

struct ContentView : View {
    var body: some View {
            ARViewContainer().edgesIgnoringSafeArea(.all)
                .onTapGesture(perform: {
                // explode text
            }
}
struct ARVariables{
  static var arView: ARView!
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        ARVariables.arView = ARView(frame: .zero)

        let modelA  = generateModelFromText(translateY: 0, color: .white)
        let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
        anchor.children.append(modelA)

        ARVariables.arView.scene.anchors.append(anchor)
        ARVariables.arView.installGestures(.all, for: modelA)
        
        return ARVariables.arView        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
    
}
func generateModelFromText(translateY:Float=0.05, color:UIColor = .gray)->ModelEntity {
       
    let font:UIFont = .systemFont(ofSize: 0.3, weight: UIFont.Weight.heavy, width: .condensed)
    let mesh = MeshResource.generateText(
        "That Text",
        extrusionDepth: 0.08,
        font: font
    )
    
    let material = SimpleMaterial(color: color, roughness: 0.3, isMetallic: true)
    let model = ModelEntity(mesh: mesh, materials: [material])
    
    model.transform.translation.y = -0.08
    model.transform.translation.x = -1 * (mesh.bounds.max.x / 2)
    
    model.generateCollisionShapes(recursive: true)
    
    return model
}

Text mesh

0

There are 0 best solutions below