I am working on an Augmented Reality iOS app for which i am using ARKIT of iOS 11 beta. I am using ARSCNView(SceneKit) to render my .scn object. I have followed the sample given by apple in order to create an ARSession(https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip). Is it possible to place the 3D model with custom background instead of camera?
Like user can select the background color or can provide custom 2D photos as background?
Your view has a scene
property where you can set the background
to a color or an image:
view.scene.background.contents = UIColor.red
let myImage: UIImage = ...
view.scene.background.contents = myImage
I use shake motion callback of UIKit
to change background contents. As @yaali said, CameraContents
is used to store camera contents. Notice that we can't get background.contents
if the camera has not launched, for example on viewWillAppear
. I guess ARKit
set the AVCaptureVideoPreviewLayer
to ARSCNView.scene.background.cocntents
.
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if CameraContents == nil {
CameraContents = sceneView.scene.background.contents;
return
}
if cameraBackGround == true {
sceneView.scene.background.contents = "Background_sky.png"
}else{
sceneView.scene.background.contents = CameraContents
}
cameraBackGround = !cameraBackGround
}