<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/CustomMaterial/SurfaceShader",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "RealityKit"
    ],
    "preciseIdentifier" : "s:17RealityFoundation14CustomMaterialV13SurfaceShaderV"
  },
  "title" : "CustomMaterial.SurfaceShader"
}
-->

# CustomMaterial.SurfaceShader

The custom material’s surface shader function.

```
struct SurfaceShader
```

## Overview

Custom materials work together with a surface shader function to render
entities. The [`CustomMaterial`](/documentation/RealityKit/CustomMaterial) properties related to rendering, such
as [`baseColor`](/documentation/RealityKit/CustomMaterial/baseColor-swift.property) and
[`normal`](/documentation/RealityKit/CustomMaterial/normal-swift.property), are available in the surface
shader function, but RealityKit doesn’t use them directly.

Instead, the material’s surface shader function allows you to calculate
or specify all the material parameters that RealityKit uses to render
your entity, such as [`baseColor`](/documentation/RealityKit/CustomMaterial/baseColor-swift.property),
[`normal`](/documentation/RealityKit/CustomMaterial/normal-swift.property), and
[`roughness`](/documentation/RealityKit/CustomMaterial/roughness-swift.property). RealityKit’s fragment
shader calls your surface shader function once for each pixel it
renders.

Here’s a simple example of a surface shader that sets the entity’s base
color:

```cpp
#include <metal_stdlib>
#include <RealityKit/RealityKit.h>

// Specify the current default namespace as metal so that it's not
// necessary to prefix Metal Standard Library symbols.
using namespace metal;

[[visible]] void mySurfaceShader(realitykit::surface_parameters params)
{
    // Set the base color
    params.surface().set_base_color(half3(1.0, 0.5, 0.5));
}
```

For more information on creating custom materials and writing shader
functions, see
[Modifying RealityKit rendering using custom materials](/documentation/RealityKit/modifying-realitykit-rendering-using-custom-materials).

## Topics

### Creating surface shader objects

[`init(named: String, in: any MTLLibrary)`](/documentation/RealityKit/CustomMaterial/SurfaceShader/init(named:in:))

Creates a surface shader object from a named function in a Metal
library.

### Accessing surface shader properties

[`var name: String`](/documentation/RealityKit/CustomMaterial/SurfaceShader/name)

The name of the surface shader function.

[`var library: any MTLLibrary`](/documentation/RealityKit/CustomMaterial/SurfaceShader/library)

The Metal library that contains this surface shader function.

### Initializers

[`init(named: String, in: any MTLLibrary, constantValues: MTLFunctionConstantValues)`](/documentation/RealityKit/CustomMaterial/SurfaceShader/init(named:in:constantValues:))

Creates a surface shader with the specified function constant values.

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Hashable`](/documentation/Swift/Hashable)

[`MaterialFunction`](/documentation/RealityKit/MaterialFunction)

[`Sendable`](/documentation/Swift/Sendable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)