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 there a way to programmatically change the material of an Entity that was created in Reality Composer?
Model entity is stored deeper in RealityKit's hierarchy, and as you said, it's Entity
, not ModelEntity
. So use downcasting to access mesh
and materials
:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let boxScene = try! Experience.loadBox()
print(boxScene)
let modelEntity = boxScene.steelBox?.children[0] as! ModelEntity
let material = SimpleMaterial(color: .green, isMetallic: false)
modelEntity.model?.materials = [material]
let anchor = AnchorEntity()
anchor.scale = [5,5,5]
modelEntity.setParent(anchor)
arView.scene.anchors.append(anchor)
}
}
RealityKit – Rotating an Entity affects its Scale
You need a starting transform "point" and ending transform "point". If a value of referenceEntity
(relativeTo) argument equal to nil
it means relative to world space
. Since the same 4x4 matrix slots are used for rotation values as for scaling, when the model is rotated, its scale also changes at the same time, if there is a difference in scale.
For perpetual transform animation use some of RealityKit 2.0 tricks.
And, of course, there is a Trigonometry that was really conceived for perpetual orbiting.
Here's a correct version of your code:
import UIKit
import RealityKit
import Combine
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var cancellable: Cancellable? = nil
let anchor = AnchorEntity()
override func viewDidLoad() {
super.viewDidLoad()
cancellable = ModelEntity.loadAsync(named: "drummer.usdz").sink { _ in
self.cancellable?.cancel()
} receiveValue: { entity in
self.anchor.addChild(entity)
self.arView.scene.addAnchor(self.anchor)
let rotation = Transform(pitch: 0, yaw: .pi, roll: 0)
entity.move(to: rotation,
relativeTo: entity,
duration: 5.0,
timingFunction: .linear)
}
}
}
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
Apply a custom texture to Plane entity using RealityKit 2.0
You have to implement brand-new parameters instead of deprecated arguments:
func createBoard() {
let planeMesh = MeshResource.generatePlane(width: 1,
height: 1,
cornerRadius: 0.05)
var material = SimpleMaterial()
material.color = try! .init(tint: .white,
texture: .init(.load(named: "img", in: nil)))
material.metallic = .init(floatLiteral: 1.0)
material.roughness = .init(floatLiteral: 0.5)
let modelEntity = ModelEntity(mesh: planeMesh,
materials: [material])
let anchor = AnchorEntity()
anchor.addChild(modelEntity)
arView.scene.anchors.append(anchor)
}
Also, you can use the following syntax:
var material = SimpleMaterial()
material.color.texture = .init(try! .load(named: "img", in: nil))
If you need mode details, read this post.
Related Topics
Make Int Round Off to Nearest Value
Select Filtered Search Results
Swift: How to Assign a Variable by Reference, Not by Value
Convert a Custom Object to Data to Be Saved in Nsuserdefauts
Scenekit Scnscenerendererdelegate - Renderer Function Not Called
Why Can't the Swift Compiler Infer This Closure's Type
Protocol Extending Encodable (Or Codable) Does Not Conform to It
How to Return a First Word from a String in Swift
Swift Ternary Operator Compilation Error
Swift 4.0 Mapview Running Slow
Nstimer Does Not Invoke a Private Func as Selector
Single-Element Parethesized Expressions/Tuples VS Common Use of Parentheses
Swift: Optional Text in Optional Value
How to Clear Alamofireimage Setimagewithurl Cache