RealityKit - Độ trong suốt Alpha của Material

Aug 20 2020

Có thể có độ trong suốt alpha với các kết cấu?

Tôi có tệp png chứa RGBA 8 bit, nhưng vì một số lý do, các phần được cho là trong suốt chỉ đơn giản là màu đen.

Tôi gán tài liệu như thế này:

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

Trả lời

1 AndyFedoroff Aug 24 2020 at 20:18

.tintColor là một hệ số cho .baseColor

Nếu bạn có một .pngtệp có alpha ( RGB* A). tất cả những gì bạn cần làm là sử dụng thêm một thuộc tính tintColorcá thể có alpha bằng 0.9999.

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


Đây là cách nó trông giống như trong một mã thực:

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

PS

Đối với tôi, nó có vẻ như là một lỗi trong RealityKit. Tôi không biết tại sao phương pháp .load(named: "file.png")không hoạt động như mong đợi.