RealityKit – Material 的 Alpha 透明度

RealityKit – Material's Alpha transparency

是否可以使用纹理实现 alpha 透明度?

我有一个包含 8 位 RGBA 的 png 文件,但由于某种原因,本应透明的部分只是黑色。

我这样分配 material:

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)
}

RealityKit 1.0

.tintColor.baseColor

的乘数

如果您的 .png 文件带有预乘 alpha (RGB*A)。您需要做的就是另外使用一个 tintColor 实例 属性,其 alpha 等于 0.9999.

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


下面是它在真实代码中的样子:

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)
}

P.S.

对我来说,这似乎是 RealityKit 1.0 中的一个错误。我不知道为什么方法 .load(named: "file.png") 没有按预期工作。


RealityKit 2.0

RealityKit 2.0 中关于部分透明纹理的相同故事:

var material = SimpleMaterial()

material.color = try! .init(tint: .white.withAlphaComponent(0.9999),
                         texture: .init(.load(named: "semi.png", in: nil)))

tint 参数也是 texture 的乘数。