RealityKit – Why ModelEntity doesn't change dynamically?
Fix the following errors and it will do the trick:
Use
$
fortimeAccumulate
property wrapper inContentView
struct to get a binding struct.ARViewContainer(timeAccumulate: $timeAccumulate)
Then use
@Binding
attribute fortimeAccumulate
property inARViewContainer
struct.@Binding var timeAccumulate: Int
baseColor
is still working in iOS 15 but it'll be irrelevant on iOS 16. So, usecolor
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:
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
}
}
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
Swift Access to Variable Length Array
Use Multiple Codingkeys for a Single Property
How to Make Cells Have a Gradient Background
Use of Undeclared Type 'Attributedstring'
Wkwebview Auto Fill Login Form Swift 2
How to Avoid That My Swift Async Method Runs on the Main Thread in Swiftui
Why Is It Legal to Mutate an Actor's Nonsendable Property
Swift (iOS 8 Sdk) Convert Unmanaged<Abmultivalueref> to Abmultivalueref
Continuously Train Coreml Model After Shipping
Why Swift Closure Not Capture Self
Why Does Swiftui Uihostingcontroller Have Extra Spacing
Avcapturevideodataoutput Captureoutput Not Being Called
For-In Loop and Type Casting Only for Objects Which Match Type
What Is Trailing Closure Syntax in Swift
Extension for Generic Type 'Unsafemutablepointer<Uint8>'
In Swift, Does Int Have a Hidden Initializer That Takes a String
Why Is Generic Specialization Lost Inside a Generic Function