How to precompile PBR shaders for SceneKit?

Viewed 556

I've noticed that if you have an empty scene and then load a 3D model in it with physically based lighting, there is a small bit of jank as the object appears. If I then add a different object in after that, that stuttering does not occur.

Looking at the profiler, it appears that the default PBR shaders are compiling during that first slow frame. Is it possible to precompile these shaders so that it doesn't happen when I go to render the model for the first time?

Similar issues have been brought up in other frameworks, like Three.js. Seems like a smart thing to prevent.

1 Answers

If you want to precompile a Physically Based Rendering (PBR) or any other regular SceneKit's shader you must use a SCNProgram class that is a complete Metal shader program that replaces SceneKit's rendering of a geometry or material.

Here's a page with code snippets and useful links showing you how you could work with a vertex or fragment precompiled Metal shaders.

Related