iosswiftscenekitmetal

How to provide SCNTechnique with metal shaders in a framework


I am playing with a sample code: https://github.com/laanlabs/SCNTechniqueGlow. This code uses SCNTechnique to make a 3d model glow.

Now I want to move the glow functionality to a framework rather than in main bundle (so that I can reuse it across different apps). So I moved some helper that creates the SCNTechnique, the plist file and the metal file to the framework. Then the glow stopped working (the screen freezes at first frame).

I suspect that this is because I need to do something special about metal files in framework. For example, in this question: Metal file as part of an iOS framework. It seems to suggest that I need to setup metal files in framework. However, in my case, the metal code is invoked by SCNTechnique (created from the plist file), and I don't directly deal with metal files.

Edit:

Added a reproducible project here: https://drive.google.com/file/d/1h_7_gKQGmUX1YOW_tee3SJ7liY80BNtC/view?usp=drive_link

The project doesn't work right now since the metal file is in the framework, but if you move the metal file into the main binary, it will start working again.


Solution

  • Found the solution: I can set the technique's library property:

        let device = MTLCreateSystemDefaultDevice()
        let lib = try! device!.makeDefaultLibrary(bundle: bundle)
        technique.library = lib