Search code examples
realitykitreality-composer

I want to addChild the Entity created by the RealityComposer


I'm adding addChild to a custom class that inherits the Entity that I created with RealityComposer, but the Entity is not placed at the tapped position and is displayed in the center of the screen.

I'm using the official sample by Apple of collaborative session creation and implemented so far I've done it. (It doesn't use RealityComposer.)

For example, tapping on an Entity will place it in that location.

However, when I add an Entity to the scene, such as a container that is an addChild of an Entity created by RealityComposer, it always appears in the middle.

My guess is that this is because Entities created with the RealityComposer are not HasModel compliant.

In this code, the Entity is always in the center of the screen (I've already created a QRScene.rcproject.)

final class QRCardEntity: Entity, HasModel {
    let twitterCard = try! QRScene.loadTwitterCard ()
    var cardContainer: HasModel {
        twitterCard.allChildren().first { $0.name == " CardContainer" }! .children[0] as! HasModel
    }

    required init() {
        super.init()

        addChild(twitterCard) // preservingWorldTransform: true does not change this.
    }
}

However, this code puts it in the right place.

final class QRCardEntity: Entity, HasModel {
    let twitterCard = try! QRScene.loadTwitterCard ()
    var cardContainer: HasModel {
        twitterCard.allChildren().first { $0.name == "CardContainer" }! .children[0] as! HasModel
    }

    required init() {
        super.init()

        model = cardContainer.model
    }
}

Extensions used

private extension Entity {
    func allChildren() -> [Entity] {
        children.reduce([]) { $0 + [$1] + $1.allChildren () }
    }
}

I don't think this is the best way.

AddChild is a better way to add a whole Entity while preserving the hierarchical relationship. This model assignment can only add models from the Top hierarchy, so it doesn't add the other Models needed for display.

How do I get the Entity created by the RealityComposer to appear in the correct position with addChild?


ps 2020/07/03

To be precise, when you tap on Device1, Entity appears in the center and Device2 shows Entity asynchronously centered no matter where the camera is pointed.

Instead of Entity of created by RealityComposer, using code like this, it works.

addChild(
  ModelEntity(
    mesh: .generateBox(size: 0.1),
    materials: [
      SimpleMaterial(color: color ?? .white, isMetallic: false)
    ]
  )
)


Solution

  • I solved by myself.

    The reason is because I added the scene as child.

    This code is to load the scene created by RealityComposer. TwitterCard was scene name.

    let twitterCard = try! QRScene.loadTwitterCard()
    

    Scene name is presented in here.

    enter image description here

    So, correctly ModelEntity's name is here. I needed to use this.

    enter image description here

    This load correctly.

    addChild(twitterCard.cardObject!)
    

    Only write this, it could add child Entity created by RealityComposer.

    I have problem yet.

    I can’t post notification for moving motion created by RealityComposer.

    Because of twitterCard is not added to ARView’s scene. If can’t do this, we must write a lot of animation codes.

    When needed, I create another post, thanks.