Create shader in code using Metal for .generateText on visionOS

207 views Asked by At

I am trying to use Metal shaders to apply text effects to text made using .generateText on visionOS. I am using the following UI code, and I am able to assign SimpleMaterial like this:

 RealityView { content in  
        let font = UIFont.systemFont(ofSize: 1)
        let resource: MeshResource = .generateText("Metal on visionOS", font: font)
        let entity: ModelEntity = ModelEntity(mesh: resource, materials: [SimpleMaterial(color: .blue, isMetallic: false)])
        content.add(entity)
}       

My problem is that I need to be able to create custom shaders using Metal. I know that I can use Reality Composer Pro, but I want to be able to create them in code using Metal shaders. I saw people do this using CustomMaterial, however CustomMaterial is not available on visionOS. Here is a shader in shaders.swift file I am trying to attach to 3D text:

#include <metal_stdlib>
using namespace metal;

[[ stitchable ]] half4 aqua(
    float2 position,
    float width
    ) {
        return half4(60.0/255.0, 238.0/255.0, 227/255.0, 1.0);
    };

Thanks for help.

1

There are 1 answers

0
Jeshua Lacock On BEST ANSWER

As you know CustomMaterial isn't available on visionOS so of course that won't work.

You will need to use a ShaderGraphMaterial with a MaterialX in Reality Composer Pro.

See Explore materials in Reality Composer Pro.