Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save drewolbrich/2fb7d72e4a6ea5c8c0ad1b019e549a7b to your computer and use it in GitHub Desktop.
Save drewolbrich/2fb7d72e4a6ea5c8c0ad1b019e549a7b to your computer and use it in GitHub Desktop.
A RealityKit SceneUpdateContext extension that wraps entities(matching:updateSystemWhen:) but works on both visionOS and iOS
//
// SceneUpdateContext+EntitiesWhenRendering.swift
//
// Created by Drew Olbrich on 7/27/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import RealityKit
extension SceneUpdateContext {
/// Returns all entities which pass the `QueryPredicate` of the query for rendering
/// updates.
///
/// On `visionOS`, this method calls `SceneUpdateContext/entities(matching:when:)`
/// with `.rendering` for the `condition` parameter.
///
/// On `iOS`, this method calls `SceneUpdateContext/Scene/performQuery(_)`.
func entitiesWhenRendering(matching query: EntityQuery) -> QueryResult<Entity> {
#if os(visionOS)
// On visionOS only, this form is recommended.
// See https://developer.apple.com/wwdc23/10080 at 25:35.
// "The rendering condition means that this system will update at an appropriate
// rate for smooth animations".
return entities(matching: query, updatingSystemWhen: .rendering)
#else // !os(visionOS)
return scene.performQuery(query)
#endif // !os(visionOS)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment