【问题标题】:RealityKit – Material's Alpha transparencyRealityKit – 材质的 Alpha 透明度
【发布时间】:2020-08-20 12:28:39
【问题描述】:

纹理是否可以具有 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)
}

【问题讨论】:

    标签: swift augmented-reality arkit realitykit


    【解决方案1】:

    RealityKit 1.0

    .tintColor.baseColor 的乘数

    如果您有一个带有预乘 alpha (RGB*A) 的 .png 文件。您需要做的就是另外使用一个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)
    }
    

    附言

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

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2015-03-04
      • 2018-02-15
      • 2020-07-12
      • 2019-01-02
      • 2016-03-01
      • 2020-09-20
      • 2021-06-12
      • 1970-01-01
      相关资源
      最近更新 更多