swift - RealityKit – Material 的 Alpha 透明度

标签 swift augmented-reality arkit realitykit

是否可以使用纹理实现 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

的乘数

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

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

附言

对我来说,这似乎是 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 的乘数。

关于swift - RealityKit – Material 的 Alpha 透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63505133/

相关文章:

flutter - Flutter 中 ArCore 中的自定义形状

swift - 如何在 ARKit 中使用环境贴图?

java - 如何在Sceneform(ARCORE)中将屏幕触摸坐标添加到现实世界?

swift - 以高性能方式获取 CVPixelBuffer(相机帧和 ar 模型)

swift - ARkit 中未检测到 SceneKit 碰撞

ios - 从 MainController 向 Delegate 发送消息 - Swift

swift - 为什么选择结构而不是类?

ios - 毫无意义的 Xcode/Swift 错误 : Unknown type 'CGFLoat' . 你是说 'CGFloat"吗?

swift - 如何根据用户手势在 SceneKit 中正确实现 throw 弹丸?

swift - 将选定的行标题传回主 TableView Controller