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)
}
}
Post
Replies
Boosts
Views
Activity
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()
})
}
}
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)
}
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!
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
}
})
}
}
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()
}
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
}
})
How to get Euler angles in RealityKit?
I use Reality Converter to convert GLB to USDZ, the color becomes darker, I don't know why?Does anyone know the reason?Thanks
I use ARImageTrackingConfiguration to load the entity.
In the scene the entity keeps shaking.
Tracking images have been correcting the rootAnchor transform,which should be the cause of this problem.
Is there any optimization plan?
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
guard let anchorCGImage = UIImage(named: "anchor.png")?.cgImage else { return }
let arReferenceImage = ARReferenceImage(anchorCGImage, orientation: .up, physicalWidth: CGFloat(0.1))
let arImages: Set<ARReferenceImage> = [arReferenceImage]
imageConfiguration.trackingImages = arImages
imageConfiguration.maximumNumberOfTrackedImages = 1
arView.session.run(imageConfiguration)
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor])
{
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
do {
let ey = try Entity.load(named: "c5nj3ibu_l1a", in: .main)
ey.scale = [0.4, 0.4, 0.4]
rootAnchor = AnchorEntity(anchor: $0)
rootAnchor.addChild(ey)
arView.scene.addAnchor(rootAnchor)
} catch {
}
}
}
When isAutoFocusEnabled is set to true, the entity in the scene keeps shaking.
No focus when isAutoFocusEnabled is set to false.
How to set up to solve this problem.
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
guard let arCGImage = UIImage(named: "111", in: .main, with: .none)?.cgImage else { return }
let arReferenceImage = ARReferenceImage(arCGImage, orientation: .up, physicalWidth: CGFloat(0.1))
let arImages: Set<ARReferenceImage> = [arReferenceImage]
let configuration = ARImageTrackingConfiguration()
configuration.trackingImages = arImages
configuration.maximumNumberOfTrackedImages = 1
configuration.isAutoFocusEnabled = false
arView.session.run(configuration)
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
let anchor = AnchorEntity(anchor: $0)
let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.005)
let material = SimpleMaterial(color: .gray, roughness: 0.15, isMetallic: true)
let model = ModelEntity(mesh: mesh, materials: [material])
model.transform.translation.y = 0.05
anchor.children.append(model)
arView.scene.addAnchor(anchor)
}
}