0

In am iOS app I am trying to load a SceneKit .scn file with a single quad in it and generate an MTKMesh from it. No joy.

I load the SCNScene file:

guard let scene = SCNScene(named:"scenes.scnassets/quad.scn") else {
    fatalError("Error: Can not create SCNScene with \(sceneName)")
}

I traverse the scene graph to locate the quad:

guard let sceneNode = scene.rootNode.childNode(withName:"quadIdentity", recursively:true) else {
    fatalError("Error: Can not create sceneNode")
}

I extract the quad geometry:

guard let sceneGeometry = sceneNode.geometry else {
    fatalError("Error: Can not create sceneGeometry")
}

I create a Model I/O mesh from the geometry:

let modelIOMesh = MDLMesh(scnGeometry:sceneGeometry, bufferAllocator:MTKMeshBufferAllocator(device:device))

I force the vertex description to be Metal format:

let metalVertexDescriptor = MTLVertexDescriptor.xyz_n_st_vertexDescriptor()

let modelIOVertexDescriptor = MTKModelIOVertexDescriptorFromMetal(metalVertexDescriptor)
(modelIOVertexDescriptor.attributes[ 0 ] as! MDLVertexAttribute).name = MDLVertexAttributePosition
(modelIOVertexDescriptor.attributes[ 1 ] as! MDLVertexAttribute).name = MDLVertexAttributeNormal
(modelIOVertexDescriptor.attributes[ 2 ] as! MDLVertexAttribute).name = MDLVertexAttributeTextureCoordinate

modelIOMesh.vertexDescriptor = modelIOVertexDescriptor

This always fails with a fatal error:

do {
    mesh = try MTKMesh(mesh:modelIOMesh, device:device)
} catch {
    fatalError("Error: Can not create Metal mesh")
}

What am I missing?

UPDATE 0

I changes the try/catch to print the error:

do {

    mesh = try MTKMesh(mesh:modelIOMesh, device:device)

} catch let error as NSError {

    print(error.localizedDescription)

}

The error is:

fatal error: unexpectedly found nil while unwrapping an Optional value

Joe
  • 8,868
  • 8
  • 37
  • 59
dugla
  • 12,774
  • 26
  • 88
  • 136
  • fatal error: unexpectedly found nil while unwrapping an Optional value – dugla Nov 12 '16 at 14:43
  • I don't know if this will help you but you can use SCNRenderer to render your SCNScene into a Metal rendering. That might be an easier approach: https://developer.apple.com/reference/scenekit/scnrenderer – Bjorn Nov 12 '16 at 19:22
  • Actually all I want from SceneKit is the modeler to position geometry/lights/camera. I'm after a nice little workflow from SCNScene -> Metal. – dugla Nov 12 '16 at 23:22
  • Why even use Metal if you want to use SceneKit to get all that? Why not just use SceneKit? It gives you all those things already. – Bjorn Nov 13 '16 at 10:58
  • My focus is Metal. I am developing custom shaders, custom lights, exploring various multi-renderpass approaches, live-video effects. SceneKit is awesome. Just way to high level. I want to be very granular and experimental. The SceneKit modeler is a "good enough" source for what I need to get on and hack Metal. – dugla Nov 13 '16 at 14:33
  • Positioning lights, camera and geometry is pretty trivial tbh. Might as well just do that yourself. For example my lookAt function in swift is a trivial number of lines of code: https://github.com/btipling/blockens-perspective/blob/master/blockens-perspective/src/transforms.swift#L169-L208 – Bjorn Nov 13 '16 at 19:17
  • Bjorn, you can take a look at my little Metal lab here: https://github.com/turner/HelloMetal – dugla Nov 14 '16 at 01:31
  • What did you want me to look at? I don't really know Model IO too well. I'd probably use SIMD instead of GLKit for your math. Check out the transforms.mm in the sample code, you can use simd types in swift, I do it. https://developer.apple.com/library/content/samplecode/MetalDeferredLighting/Listings/MetalDeferredLighting_AAPLTransforms_mm.html#//apple_ref/doc/uid/TP40014630-MetalDeferredLighting_AAPLTransforms_mm-DontLinkElementID_11 – Bjorn Nov 14 '16 at 02:03
  • 1
    The HelloModel_IO target of your project works fine if I set some default values on the properties of the camera class. The crash that's happening results from several properties unnecessarily being implicitly-unwrapped optionals whose values don't get set before you try to render. – warrenm Nov 15 '16 at 02:35
  • Warren, just to be clear the target I am using to develop building meshes from a SCNScene is HelloSceneKit not HelloModel_IO. HelloModel_IO is used to consume and display the canned MDLMesh shapes like Plane/Box/Sphere. I am specifically referring to the EISceneKitMesh extension in EIMesh. Thanks for finding the Optional unwrapping issue. – dugla Nov 15 '16 at 18:44
  • I just realized this myself and came to delete my comment. I'll leave it for posterity :) – warrenm Nov 15 '16 at 18:46
  • Ah, but I think you are definitely on to something Warren. I have no idea why I have a zillion unwrapped optionals everywhere. You may on to the root cause. I'll go and clean that up. – dugla Nov 15 '16 at 19:10

0 Answers0