I have written code to initialise one of 3 Reality Composer scenes when a button is pressed depending on the day of the month.
That all works fine.
The Reality Composer scenes use an image detection to place the objects within the environment but currently as soon as the image is out of the camera view the objects disappear.
I would like to anchor the scene with the root node being where the image is first detected so that users can look around the scene and the objects are maintained even when the image trigger is not in the camera view.
I tried passing in a func renderer code below but i get errors saying the view controller class doesn't have the .planeNode
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let referenceImage = imageAnchor.referenceImage
// Create a plane to visualize the initial position of the detected image.
let plane = SCNPlane(width: referenceImage.physicalSize.width,
height: referenceImage.physicalSize.height)
plane.materials.first?.diffuse.contents = UIColor.blue.withAlphaComponent(0.20)
self.planeNode = SCNNode(geometry: plane)
self.planeNode?.opacity = 1
/*
`SCNPlane` is vertically oriented in its local coordinate space, but
`ARImageAnchor` assumes the image is horizontal in its local space, so
rotate the plane to match.
*/
self.planeNode?.eulerAngles.x = -.pi / 2
/*
Image anchors are not tracked after initial detection, so create an
animation that limits the duration for which the plane visualization appears.
*/
// Add the plane visualization to the scene.
if let planeNode = self.planeNode {
node.addChildNode(planeNode)
}
if let imageName = referenceImage.name {
plane.materials = [SCNMaterial()]
plane.materials[0].diffuse.contents = UIImage(named: imageName)
}
here's my code
import UIKit
import RealityKit
import ARKit
import SceneKit
class ViewController: UIViewController {
@IBOutlet var move: ARView!
@IBOutlet var arView: ARView!
var ARBorealAnchor3: ARboreal.ArBoreal3!
var ARBorealAnchor2: ARboreal.ArBoreal2!
var ARBorealAnchor: ARboreal.ArBoreal!
var Date1 = 1
override func viewDidLoad() {
super.viewDidLoad()
func getSingle() {
let date = Date()
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
Date1 = day
}
getSingle()
ARBorealAnchor = try! ARboreal.loadArBoreal()
ARBorealAnchor2 = try!
ARboreal.loadArBoreal2()
ARBorealAnchor3 = try!
ARboreal.loadArBoreal3()
if Date1 == 24 {
arView.scene.anchors.append(ARBorealAnchor)
}
if Date1 == 25 {
arView.scene.anchors.append(ARBorealAnchor2)
}
if Date1 == 26 {
arView.scene.anchors.append(ARBorealAnchor3)
}
}
}
Any help would be greatly appreciated.
Cheers, Daniel Savage