ApolloZhu / MetalTryout

Try Metal with Swift following tutorial "MetalKit"
MIT License
1 stars 1 forks source link

Integrating Metal Shaders with SceneKit #1

Open ApolloZhu opened 5 years ago

ApolloZhu commented 5 years ago

https://www.raywenderlich.com/5171-integrating-metal-shaders-with-scenekit

Presenter: author of "Metal Programming Guide"

Overview

vertex shader: per vertex, transform geometry fragment shader: per pixel, light/color/fx(e.g. blur) + vision/image processing

SceneKit: abstractions for 3D graphics, control vertex function and fragment function only Metal: working with GPU, complete control of rendering graphics image

Simple Metal Shader by "injection"

func setupSCNView() { }

func setupScene() {
    SCNGeometry().shaderModifiers = [.fragment:
        """
        _output.color.rbg = float3(0.4, 1, 0.8);
        """
    ]
}

func setupCamera() { }

Injection cont. for changing textures

They use a different coordinate system:

image

SCNGeometry().shaderModifiers = [.fragment:
    """
    float2 st = _surface.diffuseTextcoord.xy;
    // transform using normal math operations like max, abs, ...
    // use step() to binarize the colors
    _surface.diffuse = mix(
        float4(1), float4(0), // white and black
        percentage // how much to blend together
    )
    """
]

SCNProgram + Metal Shader

Use SCNTechnique instead for multi pass, such as ray tracing

let program = SCNProgram()
program.vertexFunctionName = "<#vertex shader name#>"
program.fragmentFunctionName = "<#fragment shader name#>"
let material = SCNMaterial()
material.program = program
SCNGeometry().materials = [material]
ApolloZhu commented 5 years ago

What I Learned?

  1. Knowing just MetalKit/SceneKit is not enough. I need to study shader language and math
  2. We don't need to write everything in metal, we can integrate part of it to use with SceneKit