Skip to content

Instantly share code, notes, and snippets.

@eospi
Created May 27, 2020 19:43
Show Gist options
  • Save eospi/16526a579a4b154d48ff57453eb1583a to your computer and use it in GitHub Desktop.
Save eospi/16526a579a4b154d48ff57453eb1583a to your computer and use it in GitHub Desktop.
import UIKit
import RealityKit
import Combine
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// Load the "Box" scene from the "Experience" Reality File
let planeAnchor = AnchorEntity(plane: .horizontal)
arView.scene.addAnchor(planeAnchor)
// Gestures do not work for this entity
let sticker = ContainerEntity(name: "ipad_pro.usdz")
planeAnchor.addChild(sticker)
// Gestures do work here
let sticker2 = ContainerEntity2(name: "ipad_pro.usdz")
sticker2.position.z += 1
planeAnchor.addChild(sticker2)
arView.installGestures(.all, for: sticker)
arView.installGestures(.all, for: sticker2)
}
}
class ContainerEntity: Entity, HasModel, HasCollision {
var content: ModelEntity?
// MARK: Initialisers
required init() {
super.init()
}
convenience init(name: String) {
self.init()
var cancellable: AnyCancellable? = nil
cancellable = ModelEntity.loadModelAsync(named: name)
.sink(receiveCompletion: { error in
cancellable?.cancel()
}, receiveValue: { entity in
// entity.generateCollisionShapes(recursive: true)
self.content = entity
self.addChild(self.content!)
self.generateCollisionShapes(recursive: true)
self.collision = entity.collision
cancellable?.cancel()
})
}
}
class ContainerEntity2: Entity, HasModel, HasCollision {
// MARK: Initialisers
required init() {
super.init()
}
convenience init(name: String) {
self.init()
var cancellable: AnyCancellable? = nil
cancellable = ModelEntity.loadModelAsync(named: name)
.sink(receiveCompletion: { error in
cancellable?.cancel()
}, receiveValue: { entity in
// entity.generateCollisionShapes(recursive: true)
self.model = entity.model
self.generateCollisionShapes(recursive: true)
cancellable?.cancel()
})
}
}
@dedoogong
Copy link

dedoogong commented Oct 3, 2020

sorry but I wonder entity.model is actually ModelComponent, not ModelEntity. How it can be converted to ModelEntity automatically?
I found, the type of entity is ModelEntity. So I just used self.model = entity, not entity.model. But second case(class) also didnt work at all. Please give me a hint if you succeeded

@dedoogong
Copy link

Ah IDKW, but it's working on only storyboard, not swiftUI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment