Realitykit - Adding Modelentity to an Argeoanchor

RealityKit – Why ModelEntity doesn't change dynamically?

Fix the following errors and it will do the trick:

  1. Use $ for timeAccumulate property wrapper in ContentView struct to get a binding struct.

    ARViewContainer(timeAccumulate: $timeAccumulate)
  2. Then use @Binding attribute for timeAccumulate property in ARViewContainer struct.

    @Binding var timeAccumulate: Int
  3. baseColor is still working in iOS 15 but it'll be irrelevant on iOS 16. So, use color instead:

    var material = SimpleMaterial()

    material.color = .init(tint: .white,
    texture: .init(try! .load(named: "texture.png")))

Is it possible to have a location based anchor for an object in RealityKit?

For geo location Apple made ARGeoTrackingConfiguration with corresponding ARGeoAnchors.

let location = CLLocationCoordinate2D(latitude: -18.9137, longitude: 47.5361)
let geoAnchor = ARGeoAnchor(name: "Tana", coordinate: location, altitude: 1250)
arView.session.add(anchor: geoAnchor)
let realityKitAnchor = AnchorEntity(anchor: geoAnchor)
arView.scene.anchors.append(realityKitAnchor)

At the moment it's working in the current cities and areas.

You can also use getGeoLocation(forPoint:completionHandler:) instance method that converts a position in the framework’s local coordinate system to GPS latitude, longitude and altitude.

arView.session.getGeoLocation(forPoint: xyzWorld) { (coord, alt, error) in
let anchor = ARGeoAnchor(coordinate: coord, altitude: alt)
}

RealityKit – Wrong ModelEntity position

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

Sample Image

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
}
}

Sample Image

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

Adding a Point Light in RealityKit

There's a couple of things here, the most noticeable is that your material is set to .blue, and you're trying to light it using a .red light. The material's made from the colour contains zero red (in rgb form), so the light will have no effect on it. If you're using glasses with a red filter on them, green and blue will just appear black, only the reds will shine through.

Even if you change it to a .white light, it won't look much different though. This is just what it looks like with the default SimpleMaterial with isMetallic set to true; all you'll see is reflections of light, rather than see a light hitting it.

This is because the roughness of the material is set to 0, increase it just a tiny bit you'll see the cube light up with your point light.

var material = SimpleMaterial(color: .blue, isMetallic: true)
material.roughness = 0.1

Also worth noting, your light intensity is quite high, I assume this is just because you weren't seeing an effect before!

Apple RealityKit: How to draw a polygon plane using ModelEntity?

RealityKit does not have APIs to create custom geometry at run time.


Edit: RealityKit 2 introduced support for dynamic meshes. See Explore advanced rendering with RealityKit 2 from WWDC21.



Related Topics



Leave a reply



Submit