Surface shader can't get opacity from the texture

I have a simple material with a texture that has transparency baked in (PNG file), this is the code that I did to load it :

 var triMat = SimpleMaterial(color: .orange, isMetallic: false)
 triMat.color = SimpleMaterial.BaseColor( tint: .white.withAlphaComponent(0.9), texture: MaterialParameters.Texture(try! .load(named: "whity_4x4")))
 self.components[ModelComponent.self] = try! ModelComponent(mesh: .generate(from: [meshDesc]), materials: [triMat])

It's pretty straight foward and it works fine, the geometry is a plane and it shows the texture correctly with transparency.

But when I try to create a surface shader (I want to animate the uv so it looks like it scrolls) the transparency become a black color. This is the part where I sample the texture for color and opacity

 auto color = params.textures().base_color().sample(textureSampler, float2(u1,v1));
 auto opacity = params.textures().opacity().sample(textureSampler, float2(u1,v1)).r;

and I set the surface color and opacity

params.surface().set_base_color(color.rgb);
params.surface().set_opacity(opacity);

upon debugging, it seems the opacity value is wrong (always 1) that's why it doesn't show as transparent. Did I do something wrong with sampling the texture opacity? I looked at the Metal API documentation that's how you supposed to do it?

Thanks for any help

Hi, can you share the Swift code that is creating your CustomMaterial and adding the texture?

Sure, here's the complete code Sure

       var triMat = SimpleMaterial(color: .orange, isMetallic: false)
       triMat.color = SimpleMaterial.BaseColor( tint: .white.withAlphaComponent(0.9), texture: MaterialParameters.Texture(try! .load(named: "whity_4x4")))
        self.components[ModelComponent.self] = try! ModelComponent(mesh: .generate(from: [meshDesc]), materials: [triMat])

        guard let device = MTLCreateSystemDefaultDevice() else {
            fatalError("Error creating default metal device.")
        }
        
        let library = device.makeDefaultLibrary()!

        let geomShader = CustomMaterial.GeometryModifier(named: "billboard", in: library)
        let surfaceShader = CustomMaterial.SurfaceShader(named: "scroll", in: library)
        
        var customMaterials:[RealityKit.Material] = []
        let materials = self.model?.materials
        
        for material in materials! {
            var material = try? CustomMaterial(from: material,
                                                   surfaceShader: surfaceShader,
                                                   geometryModifier: geomShader)
            //these custom values are to define the speed and direction of the scrolling
            material?.custom.value[0] = 100
            material?.custom.value[1] = 4
            material?.custom.value[2] = 4
            
            customMaterials.append(material!)
            
        }
        self.model?.materials = customMaterials

Hmm, are you saying that for opacity there should be another texture like normal texture? and we can't get the alpha value from the RGBA texture?

You might need to set the blendingMode on the CustomMaterial to transparent.

https://developer.apple.com/documentation/realitykit/custommaterial/blending-swift.enum/transparent(opacity:)

Surface shader can't get opacity from the texture
 
 
Q