Andy Fedoroff Andy Fedoroff - 1 month ago 14
iOS Question

Light Types in Metal API

I'd like to know if there are only

Point Light
and
Ambient Light
in Metal API or I can also use
Spot Light
and
Directional Light
in my 3D environment?

struct Light {

var color: (Float, Float, Float)
var ambientIntensity: Float

static func size() -> Int {
return sizeof(Float) * 4
}

func raw() -> [Float] {
let raw = [color.0, color.1, color.2, ambientIntensity]
return raw
}
}



How to implement
Spot Light
and
Directional Light
if they exist in Apple's Metal API?


Picture from SolidAngle User Manual

Answer

The Metal API itself has no concept of anything as high-level as a “light”, in the same way that modern OpenGL doesn’t—the APIs for that kind of thing went the way of the dinosaur with the fixed-function pipeline. With modern low-level graphics APIs, you need to roll your own lighting; the excellent Metal By Example series has an article on doing just that here, though you may want to go through the earlier sections to get a clearer idea of what’s going on. Note that that article only deals with directional lights; spot lights are significantly trickier and you’ll need to do some research to find how those are usually done and then implement that approach in Metal.

As an alternative to using Metal directly, you might want to look into SceneKit, which is quite powerful and has built-in support for many types of light via the SCNLight class.