RealityKit - Transparência Alfa do Material

Aug 20 2020

É possível ter transparência alfa com texturas?

Eu tenho um arquivo png que contém RGBA de 8 bits, mas por algum motivo, as partes que deveriam ser transparentes são simplesmente pretas.

Eu atribuo o material assim:

private func setupLightMeshes(_ scene: Entity) {
    let lightEntity = scene.findEntity(named: "LightWindow_Plane")!
    var lightMaterial = UnlitMaterial()
    
    lightMaterial.baseColor = try! MaterialColorParameter.texture(
    TextureResource.load(named: "light.png")) // this is 8bpc RGBA
    var modelComponent = lightEntity.components[ModelComponent] as! ModelComponent
    modelComponent = ModelComponent(mesh: modelComponent.mesh, materials: [lightMaterial])
    lightEntity.components.set(modelComponent)
}

Respostas

1 AndyFedoroff Aug 24 2020 at 20:18

.tintColor é um multiplicador para .baseColor

Se você tiver um .pngarquivo com um alfa pré-multiplicado ( RGB* A). tudo que você precisa fazer é usar adicionalmente uma tintColorpropriedade de instância com alfa igual a 0.9999.

material.tintColor = UIColor(white: 1.0, alpha: 0.9999)


Veja como é em um código real:

fileprivate func material() -> UnlitMaterial {

    var material = UnlitMaterial()
    material.baseColor = try! .texture(.load(named: "transparent.png"))
    material.tintColor = UIColor(white: 1.0, alpha: 0.9999)
    return material
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    let sphere: MeshResource = .generateSphere(radius: 0.5)

    let entity = ModelEntity(mesh: sphere,
                        materials: [material()])

    let anchor = AnchorEntity()
    anchor.orientation = simd_quatf(angle: .pi, axis: [0, 1, 0])

    anchor.addChild(entity)
    arView.scene.anchors.append(anchor)
}

PS

Para mim, parece um bug no RealityKit. Não sei por que o método .load(named: "file.png")não funciona conforme o esperado.