Last active
September 18, 2023 12:57
-
-
Save warpling/32b08421974957eed5365dc9c8c1c7ea to your computer and use it in GitHub Desktop.
Billboarding System/Component for RealityKit (you supply headPosition)
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 | |
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