mcpField
A
axiom-realitykit-ref

axiom-realitykit-ref

github.com/charleswiltgen开发1.2k136 次安装今天更新
代码仓库

关于 axiom-realitykit-ref

RealityKit API 分类参考,面向开发 RealityKit 应用的开发者,可查阅实体、组件、系统、材质、动画和音频等接口细节。

主要功能

  1. 01实体与组件 API
  2. 02碰撞和物理参数
  3. 03锚定与平面分类
  4. 04系统查询和场景事件
  5. 05材质、动画与音频接口

适用场景

  1. 01查找 API 签名和属性
  2. 02选择 AR 锚点类型
  3. 03配置材质或物理实体
  4. 04核对 API 平台可用性

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/charleswiltgen/axiom --skill axiom-realitykit-ref

安装后在 Claude Code 中输入/axiom-realitykit-ref确认已加载。

详细介绍

RealityKit API 参考

按类别整理的完整 RealityKit API 参考。

何时使用此参考

在以下情况下使用此参考:

  • 查询特定的 RealityKit API 签名或属性
  • 查看可用的组件类型
  • 为 AR 体验查找合适的锚点类型
  • 浏览材质属性和选项
  • 设置物理刚体参数
  • 查询动画或音频 API 详情
  • 查看特定 API 的平台可用性

第 1 部分:实体 API

Entity
// Creation
let entity = Entity()
let entity = Entity(components: [TransformComponent(), ModelComponent(...)])

// Async loading
let entity = try await Entity(named: "scene", in: .main)
let entity = try await Entity(contentsOf: url)

// Clone
let clone = entity.clone(recursive: true)
Entity 属性
属性类型描述
nameString用于查找的标识符
idObjectIdentifier唯一标识
isEnabledBool本地启用状态
isEnabledInHierarchyBool实际启用状态(考虑父级)
isActiveBool实体是否位于活动场景中
isAnchoredBool是否具有锚定祖先或自身已锚定
sceneRealityKit.Scene?所属场景
parentEntity?父实体
childrenEntity.ChildCollection子实体
componentsEntity.ComponentSet所有附加的组件
anchorHasAnchoring?最近的锚定祖先
实体层级方法
entity.addChild(child)
entity.addChild(child, preservingWorldTransform: true)
entity.removeChild(child)
entity.removeFromParent()
entity.findEntity(named: "name")  // Recursive search
Entity 子类
类用途关键组件
Entity基础容器仅 Transform
ModelEntity可渲染对象ModelComponent
AnchorEntityAR 锚点AnchoringComponent
PerspectiveCamera虚拟摄像头PerspectiveCameraComponent
DirectionalLight太阳光/定向光DirectionalLightComponent
PointLight点光源PointLightComponent
SpotLight聚光灯SpotLightComponent
TriggerVolume不可见碰撞区域CollisionComponent
ViewAttachmentEntity3D 中的 SwiftUI 视图visionOS
BodyTrackedEntity身体跟踪实体BodyTrackingComponent

第 2 部分:组件目录

Transform
// Properties
entity.position                    // SIMD3<Float>, local
entity.orientation                 // simd_quatf
entity.scale                      // SIMD3<Float>
entity.transform                  // Transform struct

// World-space
entity.position(relativeTo: nil)
entity.orientation(relativeTo: nil)
entity.setPosition(pos, relativeTo: nil)

// Utilities
entity.look(at: target, from: position, relativeTo: nil)
ModelComponent
let component = ModelComponent(
    mesh: MeshResource.generateBox(size: 0.1),
    materials: [SimpleMaterial(color: .red, isMetallic: true)]
)
entity.components[ModelComponent.self] = component
MeshResource 内置生成器
方法参数
.generateBox(size:)SIMD3<Float> 或单个 Float
.generateBox(size:cornerRadius:)圆角方盒
.generateSphere(radius:)Float
.generatePlane(width:depth:)Float、Float
.generatePlane(width:height:)垂直平面
.generateCylinder(height:radius:)Float、Float
.generateCone(height:radius:)Float、Float
.generateText(_:)String,带选项
CollisionComponent
let component = CollisionComponent(
    shapes: [
        .generateBox(size: SIMD3(0.1, 0.2, 0.1)),
        .generateSphere(radius: 0.05),
        .generateCapsule(height: 0.3, radius: 0.05),
        .generateConvex(from: meshResource)
    ],
    mode: .default,                    // .default or .trigger
    filter: CollisionFilter(
        group: CollisionGroup(rawValue: 1),
        mask: .all
    )
)
ShapeResource 类型
方法描述性能
.generateBox(size:)轴对齐方盒最快
.generateSphere(radius:)球体快
.generateCapsule(height:radius:)胶囊体快
.generateConvex(from:)从网格生成凸包中等
.generateStaticMesh(from:)精确网格最慢(仅限静态)
PhysicsBodyComponent
let component = PhysicsBodyComponent(
    massProperties: .init(
        mass: 1.0,
        inertia: SIMD3(repeating: 0.1),
        centerOfMass: .zero
    ),
    material: .generate(
        staticFriction: 0.5,
        dynamicFriction: 0.3,
        restitution: 0.4
    ),
    mode: .dynamic                     // .dynamic, .static, .kinematic
)
模式行为
.dynamic物理模拟控制位置
.static不可移动,参与碰撞
.kinematic由代码控制,影响动态刚体
PhysicsMotionComponent
var motion = PhysicsMotionComponent()
motion.linearVelocity = SIMD3(0, 5, 0)
motion.angularVelocity = SIMD3(0, .pi, 0)
entity.components[PhysicsMotionComponent.self] = motion
CharacterControllerComponent
entity.components[CharacterControllerComponent.self] = CharacterControllerComponent(
    radius: 0.3,
    height: 1.8,
    slopeLimit: .pi / 4,
    stepLimit: 0.3
)

// Move character with gravity
entity.moveCharacter(
    by: SIMD3(0.1, -0.01, 0),
    deltaTime: Float(context.deltaTime),
    relativeTo: nil
)
AnchoringComponent
// Plane detection
AnchoringComponent(.plane(.horizontal, classification: .table,
                           minimumBounds: SIMD2(0.2, 0.2)))
AnchoringComponent(.plane(.vertical, classification: .wall,
                           minimumBounds: SIMD2(0.5, 0.5)))

// World position
AnchoringComponent(.world(transform: float4x4(...)))

// Image anchor
AnchoringComponent(.image(group: "AR Resources", name: "poster"))

// Face tracking
AnchoringComponent(.face)

// Body tracking
AnchoringComponent(.body)
平面分类
分类描述
.table水平桌面
.floor地面
.ceiling天花板
.wall垂直墙面
.door门
.window窗户
.seat椅子/沙发
光源组件
// Directional
let light = DirectionalLightComponent(
    color: .white,
    intensity: 1000,
    isRealWorldProxy: false
)
light.shadow = DirectionalLightComponent.Shadow(
    maximumDistance: 10,
    depthBias: 0.01
)

// Point
PointLightComponent(
    color: .white,
    intensity: 1000,
    attenuationRadius: 5
)

// Spot
SpotLightComponent(
    color: .white,
    intensity: 1000,
    innerAngleInDegrees: 30,
    outerAngleInDegrees: 60,
    attenuationRadius: 10
)
无障碍功能
var accessibility = AccessibilityComponent()
accessibility.label = "Red cube"
accessibility.value = "Interactive 3D object"
accessibility.traits = .button
accessibility.isAccessibilityElement = true
entity.components[AccessibilityComponent.self] = accessibility
其他组件
组件用途平台
OpacityComponent淡入或淡出实体全部
GroundingShadowComponent实体下方的接触阴影全部
InputTargetComponent启用手势输入visionOS
HoverEffectComponent注视或悬停时高亮显示visionOS
SynchronizationComponent多人游戏实体同步全部
ImageBasedLightComponent自定义环境光照全部
ImageBasedLightReceiverComponent接收来自光源的 IBL全部

第 3 部分:系统 API

系统协议
protocol System {
    init(scene: RealityKit.Scene)
    func update(context: SceneUpdateContext)
}
SceneUpdateContext
属性类型描述
deltaTimeTimeInterval距上次更新的时间
sceneRealityKit.Scene场景
// Query entities
context.entities(matching: query, updatingSystemWhen: .rendering)
EntityQuery
// Has specific component
EntityQuery(where: .has(HealthComponent.self))

// Has multiple components
EntityQuery(where: .has(HealthComponent.self) && .has(ModelComponent.self))

// Does not have component
EntityQuery(where: .has(EnemyComponent.self) && !.has(DeadComponent.self))
场景事件
事件触发条件
SceneEvents.Update每一帧
SceneEvents.DidAddEntity实体添加到场景
SceneEvents.DidRemoveEntity实体从场景中移除
SceneEvents.AnchoredStateChanged锚点跟踪发生变化
CollisionEvents.Began两个实体开始碰撞
CollisionEvents.Updated碰撞持续
CollisionEvents.Ended碰撞结束
AnimationEvents.PlaybackCompleted动画播放完毕
scene.subscribe(to: CollisionEvents.Began.self, on: entity) { event in
    // event.entityA, event.entityB, event.impulse
}

第 4 部分:RealityView API

初始化器
// Basic (iOS 18+, visionOS 1.0+)
RealityView { content in
    // make: Add entities to content
}

// With update
RealityView { content in
    // make
} update: { content in
    // update: Called when SwiftUI state changes
}

// With placeholder
RealityView { content in
    // make (async loading)
} placeholder: {
    ProgressView()
}

// With attachments (visionOS)
RealityView { content, attachments in
    // make
} update: { content, attachments in
    // update
} attachments: {
    Attachment(id: "label") { Text("Hello") }
}
RealityViewContent
content.add(entity)
content.remove(entity)
content.entities          // EntityCollection

// iOS/macOS — camera content
content.camera            // RealityViewCameraContent (non-visionOS)
RealityView 上的手势
RealityView { content in ... }
    .gesture(TapGesture().targetedToAnyEntity().onEnded { value in
        let entity = value.entity
    })
    .gesture(DragGesture().targetedToAnyEntity().onChanged { value in
        value.entity.position = value.convert(value.location3D,
            from: .local, to: .scene)
    })
    .gesture(RotateGesture().targetedToAnyEntity().onChanged { value in
        // Handle rotation
    })
    .gesture(MagnifyGesture().targetedToAnyEntity().onChanged { value in
        // Handle scale
    })

第 5 部分:Model3D API

// Simple display
Model3D(named: "robot")

// With phases
Model3D(named: "robot") { phase in
    switch phase {
    case .empty:
        ProgressView()
    case .success(let model):
        model.resizable().scaledToFit()
    case .failure(let error):
        Text("Failed: \(error.localizedDescription)")
    @unknown default:
        EmptyView()
    }
}

// From URL
Model3D(url: modelURL)

第 6 部分:材质系统

SimpleMaterial
var material = SimpleMaterial()
material.color = .init(tint: .blue)
material.metallic = .init(floatLiteral: 1.0)
material.roughness = .init(floatLiteral: 0.3)
PhysicallyBasedMaterial
var material = PhysicallyBasedMaterial()
material.baseColor = .init(tint: .white,
    texture: .init(try .load(named: "albedo")))
material.metallic = .init(floatLiteral: 0.0)
material.roughness = .init(floatLiteral: 0.5)
material.normal = .init(texture: .init(try .load(named: "normal")))
material.ambientOcclusion = .init(texture: .init(try .load(named: "ao")))
material.emissiveColor = .init(color: .blue)
material.emissiveIntensity = 2.0
material.clearcoat = .init(floatLiteral: 0.8)
material.clearcoatRoughness = .init(floatLiteral: 0.1)
material.specular = .init(floatLiteral: 0.5)
material.sheen = .init(color: .white)
material.anisotropyLevel = .init(floatLiteral: 0.5)
material.blending = .transparent(opacity: .init(floatLiteral: 0.5))
material.faceCulling = .back            // .none, .front, .back
UnlitMaterial
var material = UnlitMaterial()
material.color = .init(tint: .red,
    texture: .init(try .load(named: "texture")))
material.blending = .transparent(opacity: .init(floatLiteral: 0.8))
特殊材质
// Occlusion — invisible but hides content behind it
let occlusionMaterial = OcclusionMaterial()

// Video
let videoMaterial = VideoMaterial(avPlayer: avPlayer)
TextureResource 加载
// From bundle
let texture = try await TextureResource(named: "texture")

// From URL
let texture = try await TextureResource(contentsOf: url)

// With options
let texture = try await TextureResource(named: "texture",
    options: .init(semantic: .color))  // .color, .raw, .normal, .hdrColor

第 7 部分:动画

Transform 动画
entity.move(
    to: Transform(
        scale: .one,
        rotation: targetRotation,
        translation: targetPosition
    ),
    relativeTo: entity.parent,
    duration: 1.5,
    timingFunction: .easeInOut
)
计时函数
函数曲线
.default系统默认
.linear匀速
.easeIn缓慢开始
.easeOut缓慢结束
.easeInOut缓慢开始和结束
播放已加载的动画
// All animations from USD
for animation in entity.availableAnimations {
    let controller = entity.playAnimation(animation)
}

// With options
let controller = entity.playAnimation(
    animation.repeat(count: 3),
    transitionDuration: 0.3,
    startsPaused: false
)
AnimationPlaybackController
let controller = entity.playAnimation(animation)
controller.pause()
controller.resume()
controller.stop()
controller.speed = 0.5            // Half speed
controller.blendFactor = 1.0      // Full blend
controller.isComplete             // Check completion

第 8 部分:音频

AudioFileResource
// Load
let resource = try AudioFileResource.load(
    named: "sound.wav",
    configuration: .init(
        shouldLoop: true,
        shouldRandomizeStartTime: false,
        mixGroupName: "effects"
    )
)
音频组件
// Spatial (3D positional)
entity.components[SpatialAudioComponent.self] = SpatialAudioComponent(
    directivity: .beam(focus: 0.5),
    distanceAttenuation: .rolloff(factor: 1.0),
    gain: 0                          // dB
)

// Ambient (non-positional, uniform)
entity.components[AmbientAudioComponent.self] = AmbientAudioComponent(
    gain: -6
)

// Channel (multi-channel output)
entity.components[ChannelAudioComponent.self] = ChannelAudioComponent(
    gain: 0
)
播放
let controller = entity.playAudio(resource)
controller.pause()
controller.stop()
controller.gain = -3               // Adjust volume (dB)
controller.speed = 1.5             // Pitch shift

entity.stopAllAudio()

第 9 部分:RealityRenderer(Metal 集成)

// Low-level Metal rendering of RealityKit content
let renderer = try RealityRenderer()
renderer.entities.append(entity)

// Render to Metal texture
let descriptor = RealityRenderer.CameraOutput.Descriptor(
    colorFormat: .bgra8Unorm,
    depthFormat: .depth32Float
)
try renderer.render(
    viewMatrix: viewMatrix,
    projectionMatrix: projectionMatrix,
    size: size,
    colorTexture: colorTexture,
    depthTexture: depthTexture
)

资源

WWDC:2019-603、2019-605、2021-10074、2022-10074、2023-10080、2024-10103、2024-10153

文档:/realitykit、/realitykit/entity、/realitykit/component、/realitykit/system、/realitykit/realityview、/realitykit/model3d、/realitykit/modelentity、/realitykit/anchorentity、/realitykit/physicallybasedmaterial

技能:axiom-realitykit、axiom-realitykit-diag、axiom-scenekit-ref

评论

支持 Markdown