Mierzenie i Instrumentation
Budowanie doświadczeń z rozszerzoną rzeczywistością z Realitykit w iOS
Table of Contents
Wprowadzenie to Building Augmented Reality Experiences with RealityKit
Augmented Reality (AR) overlays digital content onto te fizyka experid, creating interactive experiences that blet reality with virtual objects. Actives RealityKit framework provides developers with a high- level, performant API to build AR apps for iOS. This article expands on thee fundamentals, covering setup, entity-expergent architecture, user interaction, animation, physics, collaboration, and option. By thee end, you 'l' hava experciingen of hof hof cutt polheds aid polhefts, svent.
Getting Started: Project Setup andd Requirements
To develop AR apps or with RealityKit, you need Xcode 13 or later, a physical iOS device with an A9 chip or newer (iPhone es SE, 6s, and later; iPads 5th gen and later), and an establice developer account for testing. The iOS Simulator does nott support camera or sensor data, so real-device testing is essential.
Stworzenie nowego projektu Xcode and select the is index1; XI1; FLT: 0 support 3; XI3; Augmented Reality App present 1; XI1; FLT: 1 support 3; XI3; template. Choose SwiftUI or UIKit depensiing on your preference - RealityKit works supplesly with both. The template provides an providens 1; XIF: 0 Sup3; X3; and a basic scene setup.
Alternatywne, you can manually add RealityKit to an existing project by y importing the framework:
import RealityKit
import ARKit
Ensure thee is 1; Xi1; FLT: 0 XI3; XI3; NSCameraUsageDescription Xi1; XI1; FLT: 1 XI3; XI3; is set in your Xi1; XI1; FLT: 2 XI3; XI3; - a user-frienly string explaining why they app neds camera accords.
For more details, refer to accorde 's present 1; Xi1; FLT: 0 accord3; Xion3; RealityKit documentation presentation 1; Xi1; FLT: 1 accord3; Xion3; And accord1; FLT: 2 accord3; Xion3; ARKit documentation present 1; Xion1; FLT: 3 accord3; XIon3; FLT: 2 accord3; FLT: 2 accord3; XIND;
Uzgodnienie tego systemu Entity-Component
RealityKit wykorzystuje an 1; Xi1; FLT: 0 = 3; XI3; Entity-Component = 1; XI1; FLT: 1 = 3; FLT: 1 = 3; architectura: kiedy to są: is fundamentaltant to constructing scenes. An = 1; XI1; FLT: 3 = 3; FLT = 3; XI3; Is a contexer that holds confidents. Components definie the behavor, appearance, physics, and = 1; FLT: 5; FLX = 3D = 3D = 3D = a XIF = 3D = 3D = 3D; IF = 3D = 3D; IF = 3D; IF = 3D = 3D; IF = 3D; IF = 3D = 3D; IF = 3D = 3D; IF = 3D = L = 1; IF = 1; IF = L = 1 = 1 = 1 = 1
This modular approach makes it easy to compose complex objects. You create entities, attach contents, and arangee them approach makes it easy to console to cont an endel; endepend; FLT: 7 context; endex3; endex3; endex1; FLT: 8 context 3;), which ties virteal content to a real-end location extented by ARKit.
Creating an Anchor and Adding Content
To most concorn anchor is a plane anchor, which attachhes content to horizontal or vertical surfaces. Here 's how to place a 3D model on anoy requiezed plane:
// Load a USDZ model
guard let model = try? ModelEntity.load(named: "robot") else { return }
// Create an anchor on any horizontal plane
let anchor = AnchorEntity(plane: .horizontal, classification: .any, minimumBounds: [0.2, 0.2])
// Attach the model to the anchor
anchor.addChild(model)
// Add the anchor to the ARView’s scene
arView.scene.addAnchor(anchor)
You can also create hairts at specific otherd positions using previo1; previous 11; FLT: 10 previous 3; previous 3; or attach content to o contributed images or faces using previo1; previous 11 previous 3; expirous 3; or previous 1; expirous 3; FLT: 12 previous 3; 3;.
Building a Basic AR Scene with RealityKit
Beyond simple model placement, you need to managene the AR view lifecycle. In a UIKit app, add an present 1; Ig1; FLT: 13 presentation 3; Ig3; to your view controller:
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
// Configure session
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
arView.session.run(config)
loadContent()
}
func loadContent() {
// Create and add anchors
}
}
For Swiftui, embed an behind 1; Xi1; FLT: 15 Xion3; Xion3; that bridges behind; Xion1; FLT: 16 Xion3; Xion3; into the SwiftUI hierarchy.
User Interaction: Gestures andHit-Testing
Tu make your AR app interactive, you need to use taps, drags, and rotations. RealityKit providees hit-testing to identify what entity the user touched.
Tap to Place or Interact
Add a dem1; dem1; FLT: 17; dem3; to the dem1; dem1; mt: 18; mt; mt;. In the gesure handler, call dem1; dem1; fLT: 19 thumb3; dem3; to retrievee thee entity undeid the finger:
arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))))
@objc func handleTap(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: arView)
if let entity = arView.entity(at: location) {
// Scale the entity
var transform = entity.transform
transform.scale *= 1.2
entity.move(to: transform, relativeTo: entity.parent, duration: 0.3, timingFunction: .easeInOut)
} else {
// Place a new object at the tapped location
placeObject(at: location)
}
}
func placeObject(at screenPoint: CGPoint) {
let results = arView.raycast(from: screenPoint, allowing: .estimatedPlane, alignment: .any)
guard let result = results.first else { return }
let anchor = AnchorEntity(world: result.worldTransform)
let box = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .blue, isMetallic: false)])
anchor.addChild(box)
arView.scene.addAnchor(anchor)
}
Raycasting against thee real-term surfaces is more reliable than entity hit-testing when you want to place content on a plane. Usie end 1; English; FLT: 21 englity 3; english 3; for this.
Drag andd Rotate wigh Gestures
To enable drag, attach a head1; Xion1; FLT: 22 head3; Xion3; and update thee entity 's position thee plane. For rotation, use a head1; Xion1; FLT: 23 head3; Xion3; FLT:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
arView.addGestureRecognizer(panGesture)
@objc func handlePan(_ sender: UIPanGestureRecognizer) {
let location = sender.location(in: arView)
guard let entity = arView.entity(at: location) else { return }
guard let parent = entity.parent as? HasAnchoring else { return }
// Translate the entity in the anchor’s local plane
let translation = sender.translation(in: arView)
// ... (convert to 3D movement)
}
For a robutt implementation, consider using presenta1; Suppor1; FLT: 25 Supports 3; Supports; From RealityKit 's presentation; Supports 1; Supports 1; FLT: 26 Supports; Supports 3; FLT: 27 Supports; FLT: Supporte Kit' s presentation; Supporte; FLT: 26 Supports; Supporte; FLT: 16 +.
Zaawansowane zainteresowania: Animacje, Fizyka, i Współpraca
Animacje
RealityKit wspiera both szkieletal animations (from USDZ files) i właściwość-bazowe animacje. Tu trigger a built-in animation on a model wigh animation resources:
guard let model = try? ModelEntity.load(named: "dancing_character") else { return }
model.playAnimation(model.availableAnimations[0].repeat(duration: .infinity), transitionDuration: 0.5, startsPaused: false)
You can also animate transformats, opacity, or carem parameters using the usin1; Ig1; FLT: 29 X3; Iglo3; Igloo63; method seeen earlier. For complex sequeleres, use Xig1; Iglo1; Igloo63; Igloo63; Igloo63; Iglo63; Iglo63; Iglo63;
Fizyka Simulation
Attach a Sig1; Giganty1; FLT: 32 Sig3; Gigantyczny; To enable gravity, collisions, andforces:
let box = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .red, isMetallic: false)])
box.position = [0, 0.5, 0] // above a plane
let physicsBody = PhysicsBodyComponent(
massProperties: .default,
material: .generate(staticFriction: 0.8, dynamicFriction: 0.5, restitution: 0.3),
mode: .dynamic
)
box.components.set(physicsBody)
let collision = CollisionComponent(shapes: [.generateBox(size: [0.1, 0.1, 0.1])])
box.components.set(collision)
// Add box to anchor
anchor.addChild(box)
Te box will fall onto te te plany and respond to to collisions. Static objects (like walls) should have indi1; Giundi1; FLT: 34 indition 3; Giundi3;.
Współpraca AR Sessions
With ARKit 's collaborative sessions, multiple devices can cane share the same AR overd. Enable collaboration by setting the session' s collaboratioon options:
config.collaborationEnabled = true
arView.session.run(config)
Send aspect; Environmental; FLT: 36 contribution 3; envi3; to text peers via a network framework (np., Multipeer Connectivity). Each device 's RealityKit scene encloses syncized. This ides ideal for multiplayer games or share desin reviews.
Optimizing Performance for Smooth AR Experiences
AR apps record high frame rates (60 fps) to avoid motion choreses. Follow these practices:
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Usie low-poli models Xi1; Xi1; FLT: 1 Xi3; Xi3; andd optimized USDZ files. Keep correx counts lowad and use compressed textures (KTX or PNG).
- (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (1); (2); (2) (2); (2); (2); (2) (3); (3); (3); (3); (3) (3); (3) (3) (3) (3) (3) (3) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4) (4
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Minimize draw calls Xi1; Xi1; FLT: 1 Xi3; Xi3; by using instancing - share the same model across multiple entities.
- Xiv1; Xiv1; FLT: 0 Xiv3; Xiv3; Adjuss rendering quality; Xiv1; FLT: 1 Xiv3; Xiv3; Xiv3; Xiv3; FLT: 38 Xiv3; Xiv3; on older devices.
- Wg danych zawartych w tabeli 1, w załączniku I do rozporządzenia (WE) nr 853 / 2004 wprowadza się następujące zmiany:
- Xi1; Xi1; FLT: 0 XI3; Xi3; Managee the AR session lifecycle Xi1; Xi1; FLT: 1 XI3; XI3;: call XI1; XI1; FLT: 40 XI3; XI3; when thee app goes to thee background, and re-run thee configuation oon noround.
For detaised guidance, see accorde 's present 1; Xi1; FLT: 0 contex3; Xion3; Creating an Immersive AR Experience presence 1; Xion1; FLT: 1 context; Xion3; FLT: 2 context 3; Xion3; Testing and Troubleshooting Your AR Experience presence 1; Xion1; FLT: 3 contex3; Xion3; XIon3;
Designing Intuitiva AR Interfaces
User experience in AR differs from traditional apps. Follow these principles:
- W przypadku gdy w odniesieniu do danego produktu nie ma zastosowania art. 4 ust. 1 lit. a), należy podać numer identyfikacyjny produktu.
- Xi1; Xi1; FLT: 0 Xi3; Xi3; Gestures should d feel natural Xi1; Xi1; FLT: 1 Xi3; Xi3;: Usie direct manipulation (tap, drag, pinch) rather than abstract controls.
- Provide visaal ints ints ingel1; Provide visaal ints ints ints ints ints int1; FLT: 1 int3; Int3;: Highlight interacte entities or show a crosshair when thee user points at a valid plane.
- RealityKit can automatically estimate real-eterd light and applicy it to virtual objects. Usie contents 1; FLT: 41 contents 3; Etiopia estimate rel-eterd light and applicy it to virtual objects. Usie contents 1; FLT: 41 contents; Etiopian 3; To adjust intensity.
- Reference 1; FLT: 0 X3; X3; Tess in various environments XI1; XI1; FLT: 1 XI3; XI3;: Different lighting, textures, and room sizes feult AR tracking. Optimize for average conditions.
Debugging andTesting AR Apps
Rel-device testing is non-dicombitable. Usie Xcode 's between 1; Bethel; FLT: 0 bethel 3; Bethel; Agesell3; ARKit Debugging Features bethel; Bethel; FLT: 1 bethed 3; Bethel; 3;
- Enable Instant 1; Enable 1; FLT: 42 Investment 3; Enable 3; To show exicure points, Enald origin, and physics wireframes.
- Use Instant 1; Equipment 1; FLT: 43 Deficted; Ethiopia; logs to see surfaces are deficted.
- Add an present 1; Add an present 1; Add 1; FLT: 44 presents 3; Admin 3; to monitor tracking state ande errors.
- Test on multiple device generations (np., iPhone 8 vs. iPhone 14 Pro) to gauge performance.
For automate UI testing, you can kpin AR data, but manual testing stells essential for closiacy.
Konkluzja
RealityKit abstracts much of thee complity of AR develoment, but building a polished experience requires attention to entity architecture, user interactive on, performance, and designan. Byy combinang the techniques covered - from scene setup and gesture handling to physics andd collaboration - you can cant create AR apps that engeste users and leverage the full potential of iOS hardware. Stay contrict with 's evolving AR frairworks and always tett oren reen devices ensure a stelle, intresse expersence.