The custom material’s optional shader function that can manipulate an
entity’s vertex data.
A geometry modifier is an optional Metal function that you can use with
custom materials. Use a geometry modifier to change vertex data, such as
vertex position, color, or texture coordinates. For example, offsetting
a vertex’s position changes the size and shape of the entity for
rendering only. If your custom material has a geometry modifier,
RealityKit’s custom material vertex shader calls it once for each vertex
in the entity. Changes that your geometry modifier makes are transient
and don’t affect the vertex positions of the original ``ModelEntity``.
Here’s a simple example of a geometry modifier that offsets the vertex
positions along the z-axis based on the elapsed time:
```cpp
#include <metal_stdlib>
#include <RealityKit/RealityKit.h>
using namespace metal;
[[visible]] void myGeometryModifier(realitykit::geometry_parameters
params) {
float3 zOffset = float3(0.0, 0.0, params.uniforms().time() / 50.0);
params.geometry().set_world_position_offset(zOffset);
}
```
For more information on creating custom materials and writing shader
functions, see
<doc:modifying-realitykit-rendering-using-custom-materials>.
Members (8)
get · iOS 18.0+
constantValues
TypeScript
get constantValues(): _Metal_MTLFunctionConstantValues
Swift declaration
var constantValues: MTLFunctionConstantValues { get set }
The constant values to use when RealityKit creates your
function. These correspond to constants defined in your metal code.
set · iOS 18.0+
constantValues
TypeScript
set constantValues(value: _Metal_MTLFunctionConstantValues)
Swift declaration
var constantValues: MTLFunctionConstantValues { get set }
The constant values to use when RealityKit creates your
function. These correspond to constants defined in your metal code.
get · iOS 15.0+
name
TypeScript
get name(): string
Swift declaration
var name: String
The name of the geometry modifier function.
This is the name of the Metal function that the custom material uses
as its geometry modifier.
The name needs to match the name of a Metal
function in your Xcode project without parameters or parentheses.
set · iOS 15.0+
name
TypeScript
set name(value: string)
Swift declaration
var name: String
The name of the geometry modifier function.
This is the name of the Metal function that the custom material uses
as its geometry modifier.
The name needs to match the name of a Metal
function in your Xcode project without parameters or parentheses.
init(named name: String, in library: any MTLLibrary, constantValues: MTLFunctionConstantValues)
Creates a geometry modifier with the specified function constant values.
Function constants allow you to better share code or toggle features in your shaders
without runtime performance costs by resolving their values at compile time.
To use function constants, first define them within your metal code:
```cpp
constant bool kEnableVertexAnimation [[function_constant(0)]];
[[stitchable]]
void geometryModifier(realitykit::geometry_parameters params) {
if (kEnableVertexAnimation) {
float currentTime = params.uniforms().time() * 0.5;
params.geometry().set_model_position_offset(sin(currentTime) * 0.1 * params.geometry().normal());
}
}
```
Then initialize a geometry modifier with a ``Metal/MTLFunctionConstantValues`` object that defines values for your constants.
```swift
let constants = MTLFunctionConstantValues()
var yes = true
funcConstants.setConstantValue(&yes, type: .bool, index: 0)
let geom = CustomMaterial.GeometryModifier.init(named: "geometryModifier", in: library, constantValues: constants)
```
init(named name: String, in library: any MTLLibrary)
Creates a geometry modifier object from a named function in a Metal
library.
To create a geometry modifier function for a custom material, create
a Metal file in your Xcode project. Prefix the function with the
keyword `[[visible]]`.
The function needs to take a single parameter of type
`realitykit::geometry_parameters`.
Here’s what a minimal geometry modifier function looks like:
```cpp
#include <metal_stdlib>
#include <RealityKit/RealityKit.h>
using namespace metal;
[[visible]] void myGeometryModifier(realitykit::geometry_parameters
params) {
// Use params.geometry().set_model_position_offset() to move the vertex.
}
```
To create a custom material using this shader, get a reference to
your app’s Metal library. You can do that like this:
```swift
guard let device = MTLCreateSystemDefaultDevice() else {
fatalError("Error creating default metal device.")
}
guard let library = maybeDevice.makeDefaultLibrary() else {
fatalError("Error creating default metal library")
}
```
Once you’ve got a reference to your Metal library, use it to create
the surface shader reference like this:
```swift
let geometryModifier = CustomMaterial.GeometryModifier(
named: "myGeometryModifier",
in: library
)
```
- Important: RealityKit loads surface shader functions by name, so
the name of your surface shader function needs to be unique and
exactly match the `name` parameter.
For more information on creating custom materials and writing shader
functions, see
<doc:modifying-realitykit-rendering-using-custom-materials>.
- Parameters:
- name: The name of the geometry modifier function.
- library: The Metal library that contains the function.
get · iOS 15.0+
library
TypeScript
get library(): SDKHandle
Swift declaration
var library: any MTLLibrary { get set }
The Metal library that contains this surface shader function.
set · iOS 15.0+
library
TypeScript
set library(value: SDKHandle)
Swift declaration
var library: any MTLLibrary { get set }
The Metal library that contains this surface shader function.