Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying Different Image Texture Material to Each Side of the Box in RealityKit

I am creating a box in RealityKit and want to apply different texture to each side of the box. I am using the following code but it always applies the first texture called "lola" on each of the side as a material. Am I missing something?

cancellable = TextureResource.loadAsync(named: "lola")
            .append(TextureResource.loadAsync(named: "cover"))
            .append(TextureResource.loadAsync(named: "purple_flower"))
            .append(TextureResource.loadAsync(named: "cover"))
            .append(TextureResource.loadAsync(named: "purple_flower"))
            .append(TextureResource.loadAsync(named: "cover"))
            
            .collect()
            .sink { [weak self] completion in
            if case let .failure(error) = completion {
                fatalError("Unable to load texture \(error)")
            }
            
            self?.cancellable?.cancel()
            
        } receiveValue: { textures in
            
            var materials: [UnlitMaterial] = []
            
            textures.forEach { texture in
                print(texture)
                var material = UnlitMaterial()
                material.color = .init(tint: .white, texture: .init(texture))
                materials.append(material)
            }
            
            box.model?.materials = materials
            anchor.addChild(box)
            arView.scene.addAnchor(anchor)
        }
like image 681
Mary Doe Avatar asked Oct 23 '25 22:10

Mary Doe


1 Answers

I found the answer. The generateBox takes an argument called splitFaces. If you pass it true then it will display different material for each side.

   /// The box is centered at the local origin and aligned with the local axes.
    public static func generateBox(width: Float, height: Float, depth: Float, cornerRadius: Float = 0, splitFaces: Bool = false) -> MeshResource
like image 71
Mary Doe Avatar answered Oct 26 '25 10:10

Mary Doe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!