Post not yet marked as solved
Although the model disappeared, the memory did not decrease.
How to delete memory occupied by the model?
// MARK: === viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
arView.renderOptions = [.disableMotionBlur, .disableDepthOfField, .disableCameraGrain, .disableHDR]
arView.environment.sceneUnderstanding.options.insert(.occlusion)
rootAnchor = AnchorEntity(plane: .horizontal)
arView.scene.addAnchor(rootAnchor)
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadAsync(contentsOf: Bundle.main.url(forResource: "vyygabbj_afr", withExtension: "usdz")!)
.sink(receiveCompletion: { error in
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
}, receiveValue: { [weak self] ey in
guard let self = self else { return }
self.modelEy = ModelEntity()
self.modelEy.addChild(ey)
self.rootAnchor.addChild(self.modelEy)
ey.availableAnimations.forEach {
ey.playAnimation($0.repeat())
}
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
})
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
modelEy.removeFromParent()
}
Post not yet marked as solved
I use Entity.loadAsync to load the USDZ.
The camera is stuck for a moment when loading the model.
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadAsync(contentsOf: Bundle.main.url(forResource: "vyygabbj_afr", withExtension: "usdz")!)
.sink(receiveCompletion: { error in
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
}, receiveValue: { [weak self] ey in
guard let self = self else { return }
self.modelEy = ModelEntity()
self.modelEy.addChild(ey)
self.rootAnchor.addChild(self.modelEy)
ey.availableAnimations.forEach {
ey.playAnimation($0.repeat())
}
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
})
Post not yet marked as solved
Xcode:13.1
iOS:15.1
USDZ:https://sketchfab.com/3d-models/spartan-armour-mkv-halo-reach-57070b2fd9ff472c8988e76d8c5cbe66
The animation can be played in iOS14, but after I update to iOS15, the animation cannot be played.
Thanks
import UIKit
import ARKit
import RealityKit
import Combine
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var rootAnchor: AnchorEntity!
override func viewDidLoad() {
super.viewDidLoad()
rootAnchor = AnchorEntity(plane: .horizontal)
arView.scene.addAnchor(rootAnchor)
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadAsync(contentsOf: Bundle.main.url(forResource: "Spartan_Armour_MKV_-_Halo_Reach", withExtension: "usdz")!)
.sink(receiveCompletion: { error in
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
}, receiveValue: { [weak self] ey in
guard let self = self else { return }
self.rootAnchor.addChild(ey)
ey.availableAnimations.forEach {
ey.playAnimation($0.repeat())
}
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
})
}
}
Post not yet marked as solved
init(_ path: String)
{
super.init()
self.name = "modelEy"
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadAsync(contentsOf: URL(fileURLWithPath: path))
.sink(receiveCompletion: { error in
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
}, receiveValue: { [weak self] ey in
guard let `self` = self else { return }
ey.name = "animationEy"
self.addChild(ey)
let entityBounds = ey.visualBounds(relativeTo: ey.parent)
let extents: SIMD3<Float> = [entityBounds.extents.x, entityBounds.extents.y, entityBounds.extents.z]
self.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: extents).offsetBy(translation: entityBounds.center)])
DispatchQueue.main.async {
cancellable?.cancel()
cancellable = nil
}
})
}
ey.availableAnimations.forEach {
if #available(iOS 15.0, *)
{
ey.playAnimation($0.repeat())
}
}
The animation does not play.
Skeletal animation!
Post not yet marked as solved
Xcode:12.5
iOS:14.5
I updated the latest version.
In the previous version. I blocked the anchor image with my hand, audioEntity has been kept in the AR scene.
But now, I blocked the anchor image with my hand, audioEntity will disappear. Why?
How do I achieve the previous effect?
func session(_ session: ARSession, didAdd anchors: [ARAnchor])
{
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
rootAnchor = AnchorEntity(anchor: $0)
arView.scene.addAnchor(rootAnchor)
add()
}
}
fileprivate func add()
{
let audioPlane = MeshResource.generatePlane(width: 0.2, height: 0.2, cornerRadius: 0)
var audioMtl = SimpleMaterial()
do {
audioMtl.baseColor = try MaterialColorParameter.texture(TextureResource.load(named: "audio_play.png"))
} catch {
}
let audioEntity = ModelEntity(mesh: audioPlane, materials: [audioMtl])
audioEntity.position = [0, 0.1, 0]
rootAnchor.addChild(audioEntity)
}
The first time you install the APP, the camera will get stuck a moment during loading ModelEntity.
Thank you in advance for your help!
@IBOutlet var arView: ARView!
fileprivate var configuration: ARWorldTrackingConfiguration!
fileprivate var rootAnchor: AnchorEntity!
// MARK: === viewWillAppear
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal]
arView.session.run(configuration)
}
// MARK: === viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
}
}
// MARK: === ARSessionDelegate
extension ViewController: ARSessionDelegate
{
func session(_ session: ARSession, didAdd anchors: [ARAnchor])
{
anchors.compactMap { $0 as? ARPlaneAnchor }.forEach {
rootAnchor = AnchorEntity()
rootAnchor.transform.matrix = $0.transform
arView.scene.addAnchor(rootAnchor)
add()
}
}
// MARK: === add
fileprivate func add()
{
guard let path = Bundle.main.path(forResource: "cupandsaucer", ofType: "usdz") else { return }
var cancellable: AnyCancellable? = nil
cancellable = ModelEntity.loadModelAsync(contentsOf: URL(fileURLWithPath: path)).sink(receiveCompletion: { error in
cancellable?.cancel()
}, receiveValue: { [self] modelEy in
rootAnchor.addChild(modelEy)
cancellable?.cancel()
})
}
}
I used automaticImageScaleEstimationEnabled, but ModelEntity is not displayed correctly.
The tested device is iPhone7 plus.
@IBOutlet var arView: ARView!
fileprivate var configuration: ARWorldTrackingConfiguration!
fileprivate var rootAnchor: AnchorEntity!
// MARK: === viewWillAppear
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
configuration = ARWorldTrackingConfiguration()
configuration.automaticImageScaleEstimationEnabled = true
configuration.detectionImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil)
arView.session.run(configuration)
}
// MARK: === viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
}
}
// MARK: === ARSessionDelegate
extension ViewController: ARSessionDelegate
{
func session(_ session: ARSession, didAdd anchors: [ARAnchor])
{
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
rootAnchor = AnchorEntity()
rootAnchor.transform.matrix = $0.transform
arView.scene.addAnchor(rootAnchor)
add()
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor])
{
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
rootAnchor.transform.matrix = $0.transform
}
}
// MARK: === add
fileprivate func add()
{
let audioPlane = MeshResource.generatePlane(width: 0.2, height: 0.2, cornerRadius: 0)
var audioMtl = SimpleMaterial()
do {
audioMtl.baseColor = try MaterialColorParameter.texture(TextureResource.load(named: "audio_play.png"))
} catch {
}
let audioEy = ModelEntity(mesh: audioPlane, materials: [audioMtl])
audioEy.position = [0, 0.1, 0]
rootAnchor.addChild(audioEy)
}
}