Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Metal for Mortals

Metal for Mortals

Presentation on Apple's Metal 3D Graphics API for the try! Swift New York Chapter Meetup.

Douglass Turner

December 23, 2016
Tweet

More Decks by Douglass Turner

Other Decks in Technology

Transcript

  1. Apple provides an entire game demo in SceneKit Fox: Building

    a SceneKit Game with the Xcode Scene Editor
  2. Metal is our focus GLKit Matrix & Vector Library Model

    I/O Meshes. Model API SceneKit Model design and layout
  3. Metal GLKit Model I/O SceneKit Elastic Image API I blend

    the essential bits of GLKit, Model I/O and SceneKit with Metal
  4. HelloMetal is a handful of demos each highlighting one aspect

    of Metal. https://github.com/turner/HelloMetal
  5. A single-view app with an attached 3D manipulator called an

    ArcBall that responds to pan gestures. The View delegates to a Renderer where the magic happens. The Demos
  6. The Renderer draws a 3D object on the screen. Lets

    start simple with a quadrilateral. The Demos
  7. We attach a set of attributes to each vertex. •

    position: x, y, z • surface normal: nx, ny, nz, • texture coordinate: s, t • color: r, g, b
  8. • x, y, z • vertex location in 3-space •

    nx, ny, nz • surface normal • s, t • texture coordinate • r, g, b • vertex color
  9. All HelloMetal demos use textured objects. For even simple shapes,

    textures are an easy way to create visual detail.
  10. A shaders defines the look of a shape Shaders come

    in pairs • vertex shader • fragment shader
  11. Respond to View size changes Change viewport aspect-ratio in response

    to view size (aspect-ratio) change Rebuild all transformation matrices in response to ArcBall manipulation Renderer
  12. EIArcball is my implementation of the classic Ken Shoemake Arcball

    paper. Arcball uses Quaternions (4-dimensional vectors) to enable an intuitive mapping between pan gestures and 3D rotations. (See: SceneKit modeler for an example) EIArcball
  13. Our fragment shader named textureFragmentShader pulls “samples” from the Metal

    texture to use as the color source. Create a Metal texture from an image named kids_grid_3x3 in our asset catalog
  14. Position a texture on the far plane of the camera

    to act as a backdrop. Hello CameraPlane
  15. The idea: Rather then render directly to the screen in

    one go, render separate passes and combine them in a final pass. The Render Pass Hello RenderPass
  16. The Render Pass Break a large problem into smaller more

    tractable, decoupled, pieces. Turn a 3D task into a 2D task. Easier to tweak. “Bake” computationally tasks into inexpensive texture lookups. Render like the pros. Hello RenderPass
  17. first pass: Render using a Metal texture we create as

    the destination. Render results are cached in the texture. final pass: Use the texture from the first pass to texture- map a quad aligned to the camera. Hello RenderPass The Game Plan
  18. Render Pass Renderer We now have the results of the

    first pass render stored in the texture of the render pass descriptor we created
  19. This is just the tip of the iceberg… The render

    pass approach is the key to insane visual f/x and hyper real rendering.
  20. Model I/O is a Apple framework with a rich set

    of features for handling and processing 3D models. HelloMetal uses it only for its shape library: • PLANE • SPHERE • CUBE See WWDC 2015 Video: Managing 3D Assets with Model I/O
  21. The SceneKit framework enables you to build highly sophisticated 3D

    apps and games. I provided support for access to the SceneKit modeler and importing them via the SceneKit - Model I/O bridging APIs.
  22. With HelloSceneKit and HelloLight you can go grab a 3D

    model from the Internet - I like 3DOcean - and play with it.
  23. With SceneKit model import we have far more sophisticated models

    to play with. Lets take a closer look at how texture maps and and the s-t texture coordinates interact.
  24. So, what has been missing in these little demos? Lights!

    Specifically, shading that simulates the interaction between a light source and a 3D surface.
  25. Recall the surface normals attached to each vertex. They are

    the fundamental surface attribute responsible for creating a shaded (lit) look.
  26. One equation to rule them all. The fundamental governing equation

    for basic illumination. Cosine is everywhere in computer graphics.