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)
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With