RealityKit – 材质的 Alpha 透明度

Rai*_*dex 4 augmented-reality swift arkit realitykit

纹理是否可以具有 alpha 透明度?

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

我这样分配材料:

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

ARG*_*Geo 10

iOS RealityKit 1.0

\n

.tintColor是一个乘数.baseColor

\n

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

\n
material.tintColor = UIColor(white: 1.0, alpha: 0.9999)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
fileprivate func material() -> UnlitMaterial {\n\n    var material = UnlitMaterial()\n    material.baseColor = try! .texture(.load(named: "transparent.png"))\n    material.tintColor = UIColor(white: 1.0, alpha: 0.9999)\n    return material\n}\n\noverride func viewDidLoad() {\n    super.viewDidLoad()\n    \n    let sphere: MeshResource = .generateSphere(radius: 0.5)\n\n    let entity = ModelEntity(mesh: sphere,\n                        materials: [material()])\n\n    let anchor = AnchorEntity()\n    anchor.orientation = simd_quatf(angle: .pi, axis: [0, 1, 0])\n\n    anchor.addChild(entity)\n    arView.scene.anchors.append(anchor)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

似乎是 iOS \xe2\x80\x93 的 RealityKit 中的一个错误,为什么png透明度不能按预期工作?!

\n


\n

iOS RealityKit 2.0

\n

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

\n
var material = SimpleMaterial()\n\nmaterial.color = try! .init(tint: .white.withAlphaComponent(0.9999),\n                         texture: .init(.load(named: "semi.png", in: nil)))\n
Run Code Online (Sandbox Code Playgroud)\n

tint参数也是一个乘数texture

\n


\n

VisionOS 的 RealityKit

\n

在visionOS应用程序中,创建透明材质变得更加容易OpacityComponent

\n
modelEntity.components[OpacityComponent.self] = .init(opacity: 0.25)\n
Run Code Online (Sandbox Code Playgroud)\n