在 RealityKit 中将不同的图像纹理材质应用于盒子的每一侧

Mar*_*Doe 4 swift realitykit

我正在 RealityKit 中创建一个盒子,并希望对盒子的每一侧应用不同的纹理。我使用以下代码,但它总是在每一侧应用名为“lola”的第一个纹理作为材质。我错过了什么吗?

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)
        }
Run Code Online (Sandbox Code Playgroud)

Mar*_*Doe 5

我找到了答案。generateBox 接受一个名为 splitFaces 的参数。如果你通过它 true 那么它会为每一面显示不同的材料。

   /// 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
Run Code Online (Sandbox Code Playgroud)