RealityKit Blend Modes

I have 2 planes with textures on. I want these planes to intersect [ –|– ], and I want the blend mode to be additive. Currently I get z fighting on the planes, and I can't see how to set blend modes.

I've done this before in Unity and Godot in a fairly straight forward manner.

How do I accomplish this with RealityKit, preferably using code only (my scene is quite dynamic)?

Do I need to do it with a shader manually? How can I stop the z fighting?

Answered by kemalenver in 855805022

Just found this which works a treat:

let surfaceShader = CustomMaterial.SurfaceShader(named: "textureAndTint", in: library)
        
var descriptor = CustomMaterial.Program.Descriptor()
descriptor.lightingModel = .unlit
descriptor.blendMode = .add
        
let program = try! await CustomMaterial.Program(surfaceShader: surfaceShader, descriptor: descriptor)
            
var customMaterial = CustomMaterial(program: program)

My shader so far, I'm not sure how to to get the source colours or set the blend modes.

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

using namespace metal;

constexpr sampler textureSampler(address::clamp_to_edge, filter::bicubic);


[[visible]]
void additiveShader(realitykit::surface_parameters params)
{
    float2 uv = params.geometry().uv0();

    auto tex = params.textures();
    half3 color = (half3)tex.base_color().sample(textureSampler, uv).rgb;

    params.surface().set_emissive_color(color);
}

In RealityKit

try customMaterial = CustomMaterial(surfaceShader: surfaceShader,
                                                lightingModel: .unlit)
customMaterial.baseColor.texture = .init(MaterialParameters.Texture(tex))
Accepted Answer

Just found this which works a treat:

let surfaceShader = CustomMaterial.SurfaceShader(named: "textureAndTint", in: library)
        
var descriptor = CustomMaterial.Program.Descriptor()
descriptor.lightingModel = .unlit
descriptor.blendMode = .add
        
let program = try! await CustomMaterial.Program(surfaceShader: surfaceShader, descriptor: descriptor)
            
var customMaterial = CustomMaterial(program: program)
RealityKit Blend Modes
 
 
Q