RealityKit - Transparencia alfa del material
Aug 20 2020
¿Es posible tener transparencia alfa con texturas?
Tengo un archivo png que contiene RGBA de 8 bits, pero por alguna razón, las partes supuestamente transparentes son simplemente negras.
Asigno el material así:
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)
}
Respuestas
1 AndyFedoroff Aug 24 2020 at 20:18
.tintColor
es un multiplicador de .baseColor
Si tiene un .png
archivo con un alfa premultiplicado ( RGB
* A
). todo lo que necesita hacer es usar adicionalmente una tintColor
propiedad de instancia con alfa igual a 0.9999
.
material.tintColor = UIColor(white: 1.0, alpha: 0.9999)
Así es como se ve en un 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)
}
PD
Para mí, parece un error en RealityKit. No sé por qué el método .load(named: "file.png")
no funciona como se esperaba.