Skip to content

Instantly share code, notes, and snippets.

@warpling
Last active September 18, 2023 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warpling/32b08421974957eed5365dc9c8c1c7ea to your computer and use it in GitHub Desktop.
Save warpling/32b08421974957eed5365dc9c8c1c7ea to your computer and use it in GitHub Desktop.
Billboarding System/Component for RealityKit (you supply headPosition)
import RealityKit
struct BillboardSystem: System {
static let query = EntityQuery(where: .has(BillboardComponent.self))
init(scene: RealityKit.Scene) {}
func update(context: SceneUpdateContext) {
context.scene.performQuery(Self.query).forEach { entity in
guard let billboard = entity.components[BillboardComponent.self] else { return }
let headPosition = ARData.shared.headEntity.transform.translation
let entityPosition = entity.position(relativeTo: nil)
let target = entityPosition - (headPosition - entityPosition)
if let upVector = billboard.upVector {
entity.look(at: target, from: entityPosition, upVector: upVector, relativeTo: nil)
} else {
entity.look(at: target, from: entityPosition, relativeTo: nil)
}
}
}
}
struct BillboardComponent: Component {
var upVector: Float3?
init(upVector: Float3? = nil) {
self.upVector = upVector
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment