Nick
Nick

Reputation: 932

ARKit - Programmatically 'billboard' (look at camera) elements added in Reality composer

I am using Xcode (11.3.1) to make an AR app using ARKit. I am adding objects, text etc. in Reality Composer. I can then access these objects by loading the scene and objects in ViewController.swift.

I would like to make some of these objects constantly face the camera.

I am aware that I could add a look-at-camera 'behaviour' in Reality Composer but I don't want to do this for 2 reasons;

a) it only allows you to add in such a way that you the effect lasts for a certain amount of time (max 5 mins)

b) I would like to know how to do this programmatically.

I am aware there are a range of solutions to billboarding in ARKit provided on here, most of which are included in the answer to this question but all of these solutions involve using SceneKit.

My question is therefore - is it possible to implement this 'billboarding' effect without using SceneKit? Is there a way to do it in RealityKit only? It would seem that there is since you can include this effect by adding it as a behaviour using Reality Composer (which as far as I can tell doesn't require SceneKit, but I could be wrong). I have tried to dig down into the Reality Composer scene to understand how billboarding is achieved there but you can see in the response to a previous question the 'behaviours' are not exposed so I cannot see how this is achieved.

My 'from scratch' approach would be to understand the position/rotation of the camera but I am too inexperienced to know where to start with that. Essentially I am looking for the equivalent of SCNBillboardConstraint but without using SceneKit, perhaps that's not possible?

Upvotes: 2

Views: 842

Answers (1)

闪电狮
闪电狮

Reputation: 556

extension Entity {
    /// Billboards the entity to the targetPosition which should be provided in world space.
    func billboard(targetPosition: SIMD3<Float>) {
        look(at: targetPosition, from: position(relativeTo: nil), relativeTo: nil)
    }
}

var subscriptions = Set<AnyCancellable>()

  arView.scene.subscribe(to: SceneEvents.Update.self) { [self] _ in
                            箭头.billboard(targetPosition: arView.cameraTransform.translation)
                         }.store(in: &subscriptions)

https://developer.apple.com/forums/thread/656611

Upvotes: 2

Related Questions