Created
June 2, 2025 04:43
-
-
Save Matt54/e0435fa8fe4899eaae51b1679702a080 to your computer and use it in GitHub Desktop.
RealityKit centering extruded text
This file contains hidden or 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
import RealityKit | |
import SwiftUI | |
struct CenterExtrudedTextView: View { | |
let entity: Entity = try! getEntity() | |
var body: some View { | |
RealityView { content in | |
content.add(entity) | |
} | |
} | |
static var attributedString: AttributedString { | |
var textString = AttributedString("NOW\nTHAT'S WHAT I CALL\nREALITYKIT") | |
textString.font = .systemFont(ofSize: 1.5) | |
if let range = textString.range(of: "NOW") { | |
textString[range].font = .systemFont(ofSize: 5.0) | |
} | |
if let range = textString.range(of: "REALITYKIT") { | |
textString[range].font = .systemFont(ofSize: 3.0) | |
} | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.alignment = .center | |
let centerAttributes = AttributeContainer([.paragraphStyle: paragraphStyle]) | |
textString.mergeAttributes(centerAttributes) | |
return textString | |
} | |
static func getMeshResource() throws -> MeshResource { | |
var extrusionOptions = MeshResource.ShapeExtrusionOptions() | |
extrusionOptions.extrusionMethod = .linear(depth: 0.4) | |
extrusionOptions.materialAssignment = .init(front: 0, back: 0, extrusion: 1, frontChamfer: 1, backChamfer: 1) | |
extrusionOptions.chamferRadius = 0.01 | |
return try MeshResource(extruding: attributedString, extrusionOptions: extrusionOptions) | |
} | |
static func getMaterials() -> [RealityKit.Material] { | |
var material = PhysicallyBasedMaterial() | |
material.baseColor.tint = .init(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0) | |
material.roughness.scale = 0.0 | |
material.metallic.scale = 1.0 | |
material.faceCulling = .none | |
var material2 = UnlitMaterial() | |
material2.color.tint = .init(red: 0.125, green: 0.0, blue: 0.25, alpha: 1.0) | |
return [material, material2] | |
} | |
static func getEntity() throws -> Entity { | |
let resource = try getMeshResource() | |
let modelComponent = ModelComponent(mesh: resource, materials: getMaterials()) | |
let entity = ModelEntity() | |
entity.components.set(modelComponent) | |
centerTextEntity(entity) | |
return entity | |
} | |
static func centerTextEntity(_ entity: ModelEntity) { | |
guard let bounds = entity.model?.mesh.bounds else { return } | |
let minX = bounds.min.x | |
let maxX = bounds.max.x | |
let minY = bounds.min.y | |
let maxY = bounds.max.y | |
let minZ = bounds.min.z | |
let maxZ = bounds.max.z | |
entity.transform.translation.x -= (maxX-minX) * 0.5 | |
entity.transform.translation.y -= (maxY-minY) * 0.5 | |
entity.transform.translation.z -= (maxZ-minZ) * 0.5 | |
} | |
} | |
#Preview { | |
CenterExtrudedTextView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment