Edoardo
Edoardo

Reputation: 675

RealityKit – Wrong ModelEntity position

I have created a simple scene in a RealityKit file using RealityKit composer:

enter image description here

Then I created an arView and I added to its anchor my scene's anchor:

arView.scene.anchors.append(ARSceneViewModel().sceneAnchor)

I created an ARSceneViewModel class which sets my scene up.

When I run my app this is what I see:

enter image description here

But when I dynamically change entity's text in ARSceneViewModel:

I think there is something of wrong in these lines (maybe because of the hierarchy of the scene):

private func updateScene() {
    self.sceneAnchor.text?.children[0].children[0].components.set(generateModelComponentForText(text: "Hello"))
}

This is the hierarchy of the scene:

enter image description here

Here the function to change the entity's text:

private func generateModelComponentForText(text: String, size: CGFloat = 0.02, extrusion: Float = 0.0003) -> ModelComponent {
    let mesh: MeshResource =  .generateText(text,
                                            extrusionDepth: 0.001,
                                            font: .systemFont(ofSize: 0.06),
                                            containerFrame: CGRect.zero,
                                            alignment: .center,
                                            lineBreakMode: .byWordWrapping)
    
    let material: [Material] = [SimpleMaterial(color: .black, isMetallic: true)]
    
    return ModelComponent(mesh: mesh, materials: material)
}

When I run the app:

enter image description here

Ok, I didn't expect that. I don't understand why it is not text Entity is not centred to the scene. I have changed its text, not its position. Any suggestions?

Here you can download the project. (WeTransfer)

Upvotes: 4

Views: 1275

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58563

You can easily center a new text using the following logic:

enter image description here

import Cocoa
import RealityKit

class ViewController: NSViewController {
    
    @IBOutlet var arView: ARView!

    override func awakeFromNib() {
        arView.environment.background = .color(.black)
        
        let scenePlate = try! Experience.loadPlate()
        arView.scene.anchors.append(scenePlate)
        
        print(scenePlate)
        
        
        // Text replacement and centering
        let text = scenePlate.findEntity(named: "simpBld_text") as! ModelEntity
        
        text.model?.mesh = .generateText("Hello",
                                         extrusionDepth: 0.05,
                                         font: .systemFont(ofSize: 0.35))

        let boundingBox: BoundingBox? = text.model?.mesh.bounds
        let coord = ((boundingBox?.max)! - (boundingBox?.min)!) / 2
        text.position = -1 * [coord.x, coord.y, coord.z]
        text.position.y -= 0.07
    }
}

enter image description here

Here's scene hierarchy:

▿ '' : Plate, children: 1
  ⟐ SynchronizationComponent
  ⟐ Transform
  ⟐ AnchoringComponent
  ▿ '' : AnchorEntity, children: 2
    ⟐ SynchronizationComponent
    ⟐ Transform
    ⟐ AnchoringComponent
    ▿ '' : Entity, children: 1
      ⟐ SynchronizationComponent
      ⟐ Transform
      ▿ 'sign' : Entity, children: 4
        ⟐ SynchronizationComponent
        ⟐ Transform
        ▿ 'squareNoEditBorder_1' : Entity, children: 1
          ⟐ SynchronizationComponent
          ⟐ Transform
          ▿ 'simpBld_root' : ModelEntity
            ⟐ ModelComponent
            ⟐ SynchronizationComponent
            ⟐ Transform
        ▿ 'squareNoEditFace_1' : Entity, children: 1
          ⟐ SynchronizationComponent
          ⟐ Transform
          ▿ 'simpBld_root' : ModelEntity
            ⟐ ModelComponent
            ⟐ SynchronizationComponent
            ⟐ Transform
        ▿ 'squareNoEditGround_1' : Entity, children: 1
          ⟐ SynchronizationComponent
          ⟐ Transform
          ▿ 'simpBld_root' : ModelEntity
            ⟐ ModelComponent
            ⟐ SynchronizationComponent
            ⟐ Transform
        ▿ 'squareText_1' : Entity, children: 1
          ⟐ SynchronizationComponent
          ⟐ Transform
          ▿ 'simpBld_root' : Entity, children: 1
            ⟐ SynchronizationComponent
            ⟐ Transform
            ▿ 'simpBld_text' : ModelEntity
              ⟐ ModelComponent
              ⟐ SynchronizationComponent
              ⟐ Transform
    ▿ 'Ground Plane' : Entity
      ⟐ SynchronizationComponent
      ⟐ CollisionComponent
      ⟐ Transform
      ⟐ PhysicsBodyComponent

Upvotes: 3

Related Questions