Shader

A reference to a function in a Metal shader library, along with its bound uniform argument values.

StructureiOS 17.0+macOS 14.0+tvOS 17.0+Shader in SwiftUI

Declaration

React
type Shader = { function: ShaderFunction; arguments: ShaderArgument[]; };
Swift
struct Shader

Properties

  • The shader function called by the shader.

  • arguments*ShaderArgument[]

    The uniform argument values passed to the shader function.

Overview

Shader values can be used as filter effects on views, see the View.colorEffect(_:isEnabled:), View.distortionEffect(_:maxSampleOffset:isEnabled:), and View.layerEffect(_:maxSampleOffset:isEnabled:) functions.

Shaders also conform to the ShapeStyle protocol, letting their MSL shader function provide per-pixel color to fill any shape or text view. For a shader function to act as a fill pattern it must have a function signature matching:

[[ stitchable ]] half4 name(float2 position, args...)

where position is the user-space coordinates of the pixel applied to the shader, and args... should be compatible with the uniform arguments bound to shader. The function should return the premultiplied color value in the color space of the destination (typically extended sRGB).

From Apple’s SwiftUI documentation. Code samples there are Swift.

Used by

See also

More in Drawing and shapes.