Some simple programs to demonstrate shaders written in Metal Shading Language running in a SceneKit environment.
HelloSCNMetal: This is the SceneKit/Metal equivalent of "Hello, World". The texture is generated by a compute kernel shader. The green value of each pixel is inverted because by convention, Metal define the upper-left as the origin for the output texture, not the bottom-left. A set of vertex-fragment functions are run to display a textured plane. Alternatively, the texture can be assigned as the contents object of a material property (an instance of SCNMaterialProperty).
Instead of texturing a plane, the instance of MTLTexture can be assigned as the content object of a scene’s background property.
Tunnel: This demo shows how to bind buffers (containing custom data) to an instance of (Metal) SCNProgram using the method handleBinding(ofBufferNamed:frequency:handler:). All custom input variables passed as arguments (in the form of buffers) to a Metal vertex or fragment function must have an attribute qualifier of 2 or more.
The Metal shaders is an MSL port of the OpenGL shaders (SceneDelegate) written by Apple and included in the source code of WWDC SceneKit 2014.
ArrayTexture: This demo loads an array of 2D graphic images from a ktx file. The instantiated texture2d_array object contains four 2D textures. The user can press a key “0” to ”3” to select the layer to be used to texture the plane.
The method setValue:forKey: on the geometry (or material) is called to pass custom data as input arguments to the fragment function. The custom data is encapsulated in an instance of Data (or NSData).
Requirements:
a) software - XCode 8.x
b) hardware - GPU which supports macOS’ Metal API
c) runtime - macOS 10.11.x