9

I'm trying to set a SKScene as content in an SCNPlane inside an ARSCNView. Right now the SKScene is a very simple one defined in an .sks file which consists only of a red background and a green rectangle in the center. Right here's the code: first function creates an ARAnchor ahead the camera when user taps the screen, and the other one creates the SCNNode for the anchor.

When I set the plane's firstMaterial content as the skScene, I get a crash: validateDepthStencilState:3671: failed assertion `MTLDepthStencilDescriptor uses frontFaceStencil but MTLRenderPassDescriptor has a nil stencilAttachment texture'

This only happens running the app from XCode with the device attached. If I unplug it and run the app directly from the device I see the content correctly and I can add plenty of those ugly planes (screenshot at the bottom)

@objc func addAnchor() {
    if let currentFrame = sceneView.session.currentFrame {

        var translation = matrix_identity_float4x4
        translation.columns.3.z = -1.0
        let transform = simd_mul(currentFrame.camera.transform, translation)

        // Add a new anchor to the session
        let anchor = ARAnchor(transform: transform)
        sceneView.session.add(anchor: anchor)
    }
}

// Override to create and configure nodes for anchors added to the view's session.
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

    guard let skScene = SKScene(fileNamed: "MyScene") else {
        print("Could not load skscene!")
        return nil
    }

    let sceneSize = skScene.size
    print("Scene size in points: \(sceneSize)")

    skScene.scaleMode = .aspectFit
    //skScene.backgroundColor = UIColor.red

    let plane = SCNPlane(width: sceneSize.width * 0.001, height: sceneSize.height * 0.001)

    plane.firstMaterial?.isDoubleSided = true
    plane.firstMaterial?.diffuse.contents = skScene //Crash here

    let planeNode = SCNNode(geometry: plane)

    return planeNode

}

App working unattached to xcode

leandrodemarco
  • 1,552
  • 1
  • 15
  • 22

0 Answers0